List of usage examples for com.itextpdf.text.pdf PdfAWriter getInstance
public static PdfAWriter getInstance(final Document document, final OutputStream os, final PdfAConformanceLevel conformanceLevel) throws DocumentException
PdfWriter
. From source file:com.github.ossdevs.jhocr.converter.HocrToPdf.java
License:Open Source License
/** * @param pdfConformanceLevel determines into which format the PDF-A&/B will be converted. * @return true if the conversion was successful. *///from w ww .j a v a2 s.co m private boolean convertToPDFA(PdfAConformanceLevel pdfConformanceLevel) { boolean result = false; Document document = new Document(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = Class.class.getClassLoader(); } try { PdfAWriter writer = PdfAWriter.getInstance(document, getOutputStream(), pdfConformanceLevel); writer.createXmpMetadata(); document.open(); document.addHeader(KEY_JHOCR_INFO, KEY_JHOCR_INFO_VALUE); document.setMargins(0, 0, 0, 0); /** * TODO add documentation */ for (HocrDocumentItem item : getItems()) { HocrParser parser = new HocrParser(item.getHocrInputStream()); HocrDocument hocrDocument = parser.parse(); /** * TODO add documentation * TODO add multipage image support */ if (hocrDocument.getPages().size() > 1) { throw new UnsupportedOperationException( "Multipage tif are not yet implemented, please report: http://code.google.com/p/jhocr/issues/list"); } /** * TODO add documentation */ for (HocrPage hocrPage : hocrDocument.getPages()) { HocrPageProcessor pageProcessor = new HocrPageProcessor(hocrPage, item.getImageInputStream(), isUseImageDpi()); if (pageProcessor.isInitialized()) { pageProcessor.process(document, writer); } } } if (!outlines.isEmpty()) { writer.setOutlines(outlines); } InputStream is = this.getClass().getResourceAsStream("/sRGB.profile"); ICC_Profile icc = ICC_Profile.getInstance(is); writer.setOutputIntents(KEY_JHOCR_INFO, KEY_JHOCR_INFO_VALUE, "http://www.color.org", "sRGB IEC61966-2.1", icc); /** * Closing the document body stream. */ document.close(); getOutputStream().close(); result = true; } catch (UnsupportedOperationException e) { document.close(); logger.error("This operation is not yet implemented.", e); result = false; } catch (DocumentException e) { document.close(); logger.error("exception while genrating the PDF.", e); result = false; } catch (IOException e) { document.close(); logger.error("FileSystem I/O Exception, please check the log and file system persmissions.", e); result = false; } return result; }
From source file:pdfcreator.PDFCreator.java
License:Open Source License
@SuppressWarnings("static-access") protected void createPdf(String filename, String[] images) throws Exception { Document doc = new Document(); PdfWriter writer;//from w ww . j a v a 2 s. c o m if (pdfxConformance.equals("PDFA1A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1A); } else if (pdfxConformance.equals("PDFA1B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_1B); } else if (pdfxConformance.equals("PDFA2A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_2A); } else if (pdfxConformance.equals("PDFA2B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_2B); } else if (pdfxConformance.equals("PDFA3A")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_3A); } else if (pdfxConformance.equals("PDFA3B")) { writer = PdfAWriter.getInstance(doc, new FileOutputStream(filename), PdfAConformanceLevel.PDF_A_3B); } else { writer = PdfWriter.getInstance(doc, new FileOutputStream(filename)); } if (pdfVersion.equals("1.4")) { writer.setPdfVersion(PdfWriter.VERSION_1_4); } else if (pdfVersion.equals("1.5")) { writer.setPdfVersion(PdfWriter.VERSION_1_5); } else if (pdfVersion.equals("1.6")) { writer.setPdfVersion(PdfWriter.VERSION_1_6); } else if (pdfVersion.equals("1.7")) { writer.setPdfVersion(PdfWriter.VERSION_1_7); } else { writer.setPdfVersion(PdfWriter.VERSION_1_4); } verbose(filename + ": open"); doc.addCreationDate(); doc.addCreator(creator); if (title != null) { doc.addTitle(title); } for (int i = 0; i < images.length; i++) { verbose(" +" + images[i]); Image img = Image.getInstance(images[i]); doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight())); doc.setMargins(0, 0, 0, 0); if (doc.isOpen()) { doc.newPage(); } else { doc.open(); } doc.add(img); doc.newPage(); } ICC_Profile icc = getImageColorProfile(images[0]); if (icc == null) { System.err.println("warning: no color profile available in " + images[0] + " using " + profileName); icc = getDefaultColorProfile(); } writer.setOutputIntents("Custom", "", null, null, icc); writer.createXmpMetadata(); doc.close(); verbose(filename + ": close"); }