Java tutorial
/** * Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org] * * This file is part of Redbasin OpenDocShare community project. * * Redbasin OpenDocShare is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package util; import com.lowagie.text.pdf.*; import com.lowagie.text.DocumentException; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; /** * * @author tanisha */ public class PdfUtil { public String processText(String pdfFile) throws RedbasinException { try { PdfReader reader = new PdfReader(pdfFile); return processText(reader); } catch (Exception e) { throw new RedbasinException("Some PdfReader error occured", e); } } public String processText(byte[] pdfBytes) throws RedbasinException { try { PdfReader reader = new PdfReader(pdfBytes); return processText(reader); } catch (Exception e) { throw new RedbasinException("Come PdfReader bytes error", e); } } public String processText(PdfReader reader) throws RedbasinException { //Reads in the pdf Template StringBuffer sb = new StringBuffer(); try { //System.out.println("Number of pages = " + reader.getNumberOfPages()); int numPages = reader.getNumberOfPages(); for (int i = 1; i <= numPages; i++) { byte[] b = reader.getPageContent(i); PRTokeniser token = new PRTokeniser(b); //System.out.println("Page " + i); while (token.nextToken()) { if (token.getTokenType() == 2) { sb.append(token.getStringValue()); //System.out.print(token.getStringValue() + " "); } } //System.out.println(); } /* PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("aNewPDF.pdf")); AcroFields form = stamp.getAcroFields(); */ /* Map fields = reader.getAcroFields().getFields(); Iterator iter = fields.keySet().iterator(); System.out.println("Printing fields" + fields.size()); while (iter.hasNext()) { Object fobj = iter.next(); Object fval = fields.get(fobj); System.out.println("Field = " + fobj.toString() + "Value = " + fobj.toString()); } */ //set the field values in the pdf form /* form.setField("fieldName", "aValue"); stamp.setFormFlattening(true); stamp.close(); * */ } catch (Exception e) { throw new RedbasinException("Some pdf error occurred", e); } return sb.toString(); } public static void main(String[] args) { String s = new PdfUtil().processText("some.pdf"); System.out.println(s); } }