List of usage examples for com.lowagie.text.pdf PdfReader getMetadata
public byte[] getMetadata() throws IOException
From source file:org.alchemy.core.AlcSession.java
License:Open Source License
/** Adds a pdfReadPage to an existing pdf file * /* www.j av a 2s . c om*/ * @param mainPdf The main pdf with multiple pages. * Also used as the destination file. * @param tempPdf The 'new' pdf with one pdfReadPage to be added to the main pdf * @return */ boolean addPageToPdf(File mainPdf, File tempPdf) { try { // Destination file created in the temp dir then we will move it File dest = new File(DIR_TEMP, "Alchemy.pdf"); OutputStream output = new FileOutputStream(dest); PdfReader reader = new PdfReader(mainPdf.getPath()); PdfReader newPdf = new PdfReader(tempPdf.getPath()); // See if the size of the canvas has increased // Size of the most recent temp PDF com.lowagie.text.Rectangle currentSize = newPdf.getPageSizeWithRotation(1); // Size of the session pdf at present com.lowagie.text.Rectangle oldSize = reader.getPageSizeWithRotation(1); // Sizes to be used from now on float pdfWidth = oldSize.getWidth(); float pdfHeight = oldSize.getHeight(); if (currentSize.getWidth() > pdfWidth) { pdfWidth = currentSize.getWidth(); } if (currentSize.getHeight() > pdfHeight) { pdfHeight = currentSize.getHeight(); } // Use the new bigger canvas size if required com.lowagie.text.Document document = new com.lowagie.text.Document( new com.lowagie.text.Rectangle(pdfWidth, pdfHeight), 0, 0, 0, 0); PdfCopy copy = new PdfCopy(document, output); // Copy the meta data document.addTitle("Alchemy Session"); document.addAuthor(USER_NAME); document.addCreator("Alchemy <http://al.chemy.org>"); copy.setXmpMetadata(reader.getMetadata()); document.open(); // Holds the PDF PdfContentByte cb = copy.getDirectContent(); // Add each page from the main PDF for (int i = 0; i < reader.getNumberOfPages();) { ++i; document.newPage(); cb.setDefaultColorspace(PdfName.CS, PdfName.DEVICERGB); PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); } // Add the last (new) page document.newPage(); PdfImportedPage lastPage = copy.getImportedPage(newPdf, 1); copy.addPage(lastPage); output.flush(); document.close(); output.close(); if (dest.exists()) { // Save the location of the main pdf String mainPdfPath = mainPdf.getPath(); // Delete the old file if (mainPdf.exists()) { mainPdf.delete(); } // The final joined up pdf file File joinPdf = new File(mainPdfPath); // Rename the file boolean success = dest.renameTo(joinPdf); if (!success) { System.err.println("Error moving Pdf"); return false; } } else { System.err.println("File does not exist?!: " + dest.getAbsolutePath()); return false; } return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:org.alchemy.core.AlcSession.java
License:Open Source License
/** Load a session file to draw on top of */ boolean loadSessionFile(File file) { try {// w w w . jav a 2s .c o m // Check this is a pdf file String mime = MimeUtil.getMimeType(file.getAbsoluteFile()); if (!mime.equals("application/pdf")) { AlcUtil.showConfirmDialogFromBundle("notPDFDialogTitle", "notPDFDialogMessage"); return false; } // First make sure we are not loading the current session file if (file.equals(pdfWriteFile)) { boolean result = AlcUtil.showConfirmDialogFromBundle("loadSessionPDFDialogTitle", "loadSessionPDFDialogMessage"); if (result) { restartSession(); } else { return false; } } // Secondly check the meta data to see if this is an Alchemy session PdfReader reader = new PdfReader(file.getPath()); String metaData = new String(reader.getMetadata()); // If the pdf is not an Alchemy pdf if (!metaData.contains("Alchemy") && !file.getName().startsWith("Alchemy")) { boolean result = AlcUtil.showConfirmDialogFromBundle("loadForeignPDFDialogTitle", "loadForeignPDFDialogMessage"); if (!result) { return false; } } // set up the PDF reading RandomAccessFile raf = new RandomAccessFile(file, "r"); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); pdfReadFile = new PDFFile(buf); currentPdfReadPage = 1; maxPdfReadPage = pdfReadFile.getNumPages(); pdfReadPage = pdfReadFile.getPage(currentPdfReadPage); Alchemy.canvas.redraw(true); return true; } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return false; }
From source file:pdflicense.PdfLicenseManager.java
License:Open Source License
/** * Update the XMP info with the insertion of a CC licence * /* w ww. j av a2 s . c om*/ * @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:questions.metadata.ReplaceXMP.java
public static void alterXmp2() { try {/*from w w w . ja v a 2s . c o m*/ PdfReader reader = new PdfReader(ORIGINAL); String metadata = new String(reader.getMetadata()); metadata = metadata.replaceAll("Hello World", "Hello Universe"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2)); stamper.setXmpMetadata(metadata.getBytes()); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }