List of usage examples for com.lowagie.text.pdf PdfReader PdfReader
public PdfReader(PdfReader reader)
From source file:papertoolkit.paper.sheets.PDFSheet.java
License:BSD License
/** * @param pdfFile/* w w w.j a v a 2 s .com*/ * @param pageNumber * Which page should we create this sheet out of? */ public PDFSheet(File pdfFile, int pageNumber) { file = pdfFile; pageNum = pageNumber; try { pdfReader = new PdfReader(new FileInputStream(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // PDF pages count from 1, not 0 final int numberOfPages = pdfReader.getNumberOfPages(); if (pageNum < 1) { System.out.println( "WARNING: pageNum: " + pageNum + " is an invalid page. Setting it to 1. (PDFSheet.java)"); pageNum = 1; } if (pageNum > numberOfPages) { System.out.println("WARNING: pageNum: " + pageNum + " is an invalid page. Setting it to " + numberOfPages + ". (PDFSheet.java)"); pageNum = numberOfPages; } // get the size of page final Rectangle pageSize = pdfReader.getPageSize(pageNum); setSize(new Points(pageSize.width()), new Points(pageSize.height())); // hmm... how should we handle rotations? // DebugUtils.println("The Rotation of the PDFSheet is: " + pdfReader.getPageRotation(pageNumber)); }
From source file:pdflicense.PdfLicenseManager.java
License:Open Source License
/** * Update the XMP info with the insertion of a CC licence * /* w ww . j ava 2s.co m*/ * @param args input pdf file, output pdf file, xmp file */ public void mainTextual(String[] args) { if (args.length < 2) { usage(); System.exit(1); } else { op = args[0]; if (op.equals("show") || op.equals("showXMP") || op.equals("showToc")) { if (args.length != 2) { System.out.print("\n\nERROR: incorrect number of parameters"); usage(); System.exit(1); } fNameIn = args[1]; } else if (op.equals("put") || op.equals("putXMP") || op.equals("putforce")) { if (args.length != 4) { System.out.print("\n\nERROR: incorrect number of parameters"); usage(); System.exit(1); } fNameIn = args[1]; fNameOut = args[2]; licenseShortName = args[3]; } else { System.out.print("ERROR: operation not known (" + op + ")\n\n"); usage(); System.exit(1); } } try { PdfReader reader = new PdfReader(fNameIn); XmpManager xm = new XmpManager(); byte[] xmpdata = reader.getMetadata(); if (op.equals("showXMP") || op.equals("show")) { if (xmpdata == null) { System.out.print("No XMP Licensing info is present\n"); } else { if (op.equals("showXMP")) { String s = new String(xmpdata); System.out.print(s); } else if (op.equals("show")) { String s = new String(xmpdata); xm.parseXmp(s); //System.out.println(xm.getXmpString()); License currLic = xm.getLicenseInfo(); System.out.print("***************************\n"); System.out.print("XMP Licensing info:\n"); System.out.print("***************************\n"); System.out.print(currLic.toString()); System.out.print("\n"); } } } else if (op.equals("showToc")) { PdfDictionary cat = reader.getCatalog(); Set k = cat.getKeys(); Object[] arr = k.toArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } //PdfObject p = cat.get(PdfName.METADATA); //System.out.println(p.getName()); //System.out.println("/Metadata type="+p.type()); //PRIndirectReference iref = (PRIndirectReference)p; //findAllObjects(reader, PdfReader.getPdfObject(obj), hits); /* HashMap map = reader.getInfo(); Set keys = map.keySet(); Object []arr = keys.toArray(); for (int i=0; i<arr.length; i++) { System.out.println(arr[i]); } */ } else if (op.equals("put")) { CCLicense lic = new CCLicense(licenseShortName); if (xmpdata == null) { xm.createLicense(lic); } else { String s = new String(xmpdata); xm.parseXmp(s); xm.updateLicense(lic); } String xmpString = xm.getXmpString(); FileOutputStream fos = new FileOutputStream(fNameOut); writeXmp(reader, fos, xmpString.getBytes()); } else if (op.equals("putforce")) { CCLicense lic = new CCLicense(licenseShortName); xm.createLicense(lic); String xmpString = xm.getXmpString(); FileOutputStream fos = new FileOutputStream(fNameOut); writeXmp(reader, fos, xmpString.getBytes()); } else if (op.equals("putXMP")) { File f = new File(licenseShortName); int length = (int) f.length(); FileInputStream fin = new FileInputStream(f); BufferedInputStream data = new BufferedInputStream(fin); byte[] xmp = new byte[length]; data.read(xmp, 0, length); fin.close(); FileOutputStream fos = new FileOutputStream(fNameOut); writeXmp(reader, fos, xmp); } } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } catch (LicenseException e) { System.err.println(e.getMessage()); } }
From source file:pdflicense.PdfLicenseManager.java
License:Open Source License
public void readFileInfo() throws IOException, LicenseException { reader = new PdfReader(new FileInputStream(fileIn)); byte[] xmpdata = reader.getMetadata(); // reset current license info currLic = null;/* ww w . j av a 2s .c om*/ if (xmpdata != null) { xmpText = new String(xmpdata); xm.parseXmp(xmpText); //System.out.println(xm.getXmpString()); currLic = xm.getLicenseInfo(); // System.out.print("***************************\n"); // System.out.print("XMP Licensing info:\n"); // System.out.print("***************************\n"); // System.out.print(currLic.toString()); // System.out.print("\n"); } else { xmpText = null; } }
From source file:questions.forms.AddActionToField.java
public static void main(String[] args) { try {/* w ww.j a v a 2 s . c om*/ PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); Item fd = form.getFieldItem("Who"); PdfDictionary dict = (PdfDictionary) PdfReader.getPdfObject((PdfObject) fd.getWidgetRef(0)); PdfDictionary aa = dict.getAsDict(PdfName.AA); if (aa == null) aa = new PdfDictionary(); aa.put(new PdfName("Fo"), PdfAction.javaScript("app.alert('Who has got the focus!?');", stamper.getWriter())); dict.put(PdfName.AA, aa); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.AddFieldToExistingForm.java
public static void main(String[] args) { PdfReader reader;// w w w .j a va2 s . c o m try { reader = new PdfReader(FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); TextField tf = new TextField(stamper.getWriter(), new Rectangle(100, 760, 400, 785), "added_field"); tf.setText("\u00e4\u00f4\u00df"); tf.setOptions(TextField.READ_ONLY); stamper.addAnnotation(tf.getTextField(), 1); AcroFields form = stamper.getAcroFields(); form.setField("Who", "\u00e4\u00f3\u00df\u00f4"); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.ChangeTextFieldAlignment.java
public static void main(String[] args) { try {// ww w. j a v a 2 s . c om PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); PdfDictionary dict = form.getFieldItem("Who").getMerged(0); dict.put(PdfName.Q, new PdfNumber(1)); form.setField("Who", "Center of the World"); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillDynamicXfa.java
public static void main(String[] args) { try {// w w w .jav a2s .com PdfReader reader = new PdfReader(RESOURCE_PDF); File file = new File(RESOURCE_DATA); byte[] data = new byte[(int) file.length()]; FileInputStream is = new FileInputStream(file); int offset = 0; int numRead = 0; int datalength = data.length; while (offset < datalength && (numRead = is.read(data, offset, datalength - offset)) >= 0) { offset += numRead; } PdfDictionary root = reader.getCatalog(); PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM); PdfArray xfa = acroform.getAsArray(PdfName.XFA); for (int i = 0; i < xfa.size(); i += 2) { if ("datasets".equals(xfa.getAsString(i).toString())) { PRStream s = (PRStream) xfa.getAsStream(i + 1); s.setData(data); } } PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillDynamicXfa2.java
public static void main(String[] args) { try {//from w w w.j a v a 2s . c o m // getting new data from a "datasets" XML snippet File file = new File(RESOURCE_DATA); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document newdoc = db.parse(file); Element element = newdoc.getDocumentElement(); NodeList nodelist = element.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "data"); Node newdata = nodelist.item(0); // replacing the XFA in an existing document PdfReader reader = new PdfReader(RESOURCE_PDF); XfaForm xfa = new XfaForm(reader); Document doc = xfa.getDomDocument(); NodeList list = doc.getElementsByTagNameNS("http://www.xfa.org/schema/xfa-data/1.0/", "datasets"); list.item(0).replaceChild(doc.importNode(newdata, true), list.item(0).getFirstChild()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); xfa.setDomDocument(doc); xfa.setChanged(true); XfaForm.setXfa(xfa, stamper.getReader(), stamper.getWriter()); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } }
From source file:questions.forms.FillEnabledForm.java
public static void main(String[] args) { try {/* w w w. j a v a2s .c om*/ PdfReader reader = new PdfReader(ENABLED_FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT), '\0', true); AcroFields form = stamper.getAcroFields(); form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie"); form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121"); form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040"); form.setField("form1[0].#subform[0].Body[0].Comments[0]", "The example FillEnabledForm shows how to prefill a Reader Enabled form preserving the user permissions."); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.forms.FillEnabledFormBreakEnabling.java
public static void main(String[] args) { try {// www .j a v a 2s . c o m PdfReader reader = new PdfReader(ENABLED_FORM); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); AcroFields form = stamper.getAcroFields(); form.setField("form1[0].#subform[0].Body[0].EmployeeName[0]", "Bruno Lowagie"); form.setField("form1[0].#subform[0].Body[0].Address[0]", "Ad. Baeyensstraat 121"); form.setField("form1[0].#subform[0].Body[0].ZipCode[0]", "9040"); form.setField("form1[0].#subform[0].Body[0].Comments[0]", "The example FillEnabledForm shows how NOT to prefill a Reader Enabled form. " + "Using this code snippet will lead to a Reader warning saying: \"This document " + "contained certain rights to enable special features in Adobe Reader. The document " + "has been changed since it was created and these rights are no longer valid. Please " + "contact the author for the original version of this document.\" You could also " + "search the examples on http://1t3xt.info/examples/ to find out the correct way " + "to fill a Reader Enabled form using iText."); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }