/** * Closes the current document. It is a complete equivalent of calling {@code PdfDocument#close} on the PdfDocument * passed to the constructor of this PdfMerger instance. This means that it is enough to call <i>close</i> either on * passed PdfDocument or on this PdfMerger instance, but there is no need to call them both. */ public void close() { pdfDocument.close(); } }
/** * Closes the document and associated PdfDocument. */ @Override public void close() { if (rootRenderer != null) { rootRenderer.close(); } pdfDocument.close(); }
/** * Closes the document and associated PdfDocument. */ @Override public void close() { if (rootRenderer != null) { rootRenderer.close(); } pdfDocument.close(); }
public static void compressPDF(Path originPDF, Path outfilePath) throws IOException { PdfReader pdfReader = new PdfReader(originPDF.toString()); PdfDocument inputPdfDoc = new PdfDocument(pdfReader); File outputPDF = new File(outfilePath.toString()); PdfDocument outPdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath() ).setSmartMode(true)); int size = inputPdfDoc.getNumberOfPages(); inputPdfDoc.copyPagesTo(1, size, outPdfDoc); outPdfDoc.close(); inputPdfDoc.close(); }
public CompareTool.CompareResult compareWithFile(File file) { PdfDocument cmpDocument = null; try { cmpDocument = new PdfDocument(new PdfReader(file.getAbsolutePath())); return compareWithDocument(cmpDocument); } catch (IOException e) { LoggerHelper.warn(LoggerMessages.CREATE_COMPARE_DOC_ERROR, e, getClass()); return null; } finally { if (cmpDocument != null) { cmpDocument.close(); } } }
public CompareTool.CompareResult compareWithStream(InputStream is) { PdfDocument cmpDocument = null; try { PdfReader reader = new PdfReader(is); reader.setCloseStream(false); cmpDocument = new PdfDocument(reader); return compareWithDocument(cmpDocument); } catch (IOException e) { LoggerHelper.warn(LoggerMessages.CREATE_COMPARE_DOC_ERROR, e, getClass()); return null; } finally { if (cmpDocument != null) { cmpDocument.close(); } } }
public static String getTitle(String src){ try { PdfDocument pdfDoc = new PdfDocument(new PdfReader(src)); PdfDocumentInfo info = pdfDoc.getDocumentInfo(); String title=info.getTitle(); pdfDoc.close(); return title; } catch (Exception e) { return null; } }
FileOutputStream xmlOut = new FileOutputStream(outXmlPath); new TaggedPdfReaderTool(docOut).setRootTag("root").convertToXml(xmlOut); docOut.close(); xmlOut.close(); FileOutputStream xmlCmp = new FileOutputStream(cmpXmlPath); new TaggedPdfReaderTool(docCmp).setRootTag("root").convertToXml(xmlCmp); docCmp.close(); xmlCmp.close();
/** * 将图片合成为一个PDF * @param inputImage 图片,格式为图片格式 * @param outputPDF 输出文件 * @throws FileNotFoundException * @throws MalformedURLException */ public static void generatePDFFromImage(File[] inputImage,File outputPDF) throws FileNotFoundException, MalformedURLException { List<Image>images=new LinkedList<>(); for(File file:inputImage){ images.add(new Image(ImageDataFactory.create(file.getPath()))); } PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath())); images.forEach(image -> pdfDoc.addNewPage(new PageSize(new Rectangle(image.getImageScaledWidth(), image.getImageScaledHeight())))); BackgroundEventHandler handler = new BackgroundEventHandler(images); pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler); pdfDoc.close(); }
/** * This method merges pages from the source document to the current one. * <br><br> * If <i>closeSourceDocuments</i> flag is set to <i>true</i> (see {@link #setCloseSourceDocuments(boolean)}), * passed {@code PdfDocument} will be closed after pages are merged. * @param from - document, from which pages will be copied. * @param pages - List of numbers of pages which will be copied. * @return this {@code PdfMerger} instance. */ public PdfMerger merge(PdfDocument from, List<Integer> pages) { if (mergeTags && from.isTagged()) { pdfDocument.setTagged(); } if (mergeOutlines && from.hasOutlines()) { pdfDocument.initializeOutlines(); } from.copyPagesTo(pages, pdfDocument); if (closeSrcDocuments) { from.close(); } return this; }
private void createIgnoredAreasPdfs(String outPath, Map<Integer, List<Rectangle>> ignoredAreas) throws IOException { PdfWriter outWriter = new PdfWriter(outPath + ignoredAreasPrefix + outPdfName); PdfWriter cmpWriter = new PdfWriter(outPath + ignoredAreasPrefix + cmpPdfName); StampingProperties properties = new StampingProperties(); properties.setEventCountingMetaInfo(metaInfo); PdfDocument pdfOutDoc = new PdfDocument(new PdfReader(outPdf), outWriter, properties); PdfDocument pdfCmpDoc = new PdfDocument(new PdfReader(cmpPdf), cmpWriter, properties); for (Map.Entry<Integer, List<Rectangle>> entry : ignoredAreas.entrySet()) { int pageNumber = entry.getKey(); List<Rectangle> rectangles = entry.getValue(); if (rectangles != null && !rectangles.isEmpty()) { PdfCanvas outCanvas = new PdfCanvas(pdfOutDoc.getPage(pageNumber)); PdfCanvas cmpCanvas = new PdfCanvas(pdfCmpDoc.getPage(pageNumber)); outCanvas.saveState(); cmpCanvas.saveState(); for (Rectangle rect : rectangles) { outCanvas.rectangle(rect).fill(); cmpCanvas.rectangle(rect).fill(); } outCanvas.restoreState(); cmpCanvas.restoreState(); } } pdfOutDoc.close(); pdfCmpDoc.close(); init(outPath + ignoredAreasPrefix + outPdfName, outPath + ignoredAreasPrefix + cmpPdfName); }
/** * Entry point to encrypt a PDF document. * * @param reader the read PDF * @param os the output destination * @param newInfo an optional {@code String} map to add or change * the info dictionary. Entries with {@code null} * values delete the key in the original info dictionary */ public void encrypt(PdfReader reader, OutputStream os, Map<String, String> newInfo) { WriterProperties writerProperties = new WriterProperties(); writerProperties.encryptionProperties = properties; PdfWriter writer = new PdfWriter(os, writerProperties); StampingProperties stampingProperties = new StampingProperties(); stampingProperties.setEventCountingMetaInfo(metaInfo); PdfDocument document = new PdfDocument(reader, writer, stampingProperties); document.getDocumentInfo().setMoreInfo(newInfo); document.close(); }
@Test public void manipulatePdf() throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC)); Rectangle rect = new Rectangle(36, 750, 523, 56); FontFilter fontFilter = new FontFilter(rect); FilteredEventListener listener = new FilteredEventListener(); LocationTextExtractionStrategy extractionStrategy = listener.attachEventListener(new LocationTextExtractionStrategy(), fontFilter); new PdfCanvasProcessor(listener).processPageContent(pdfDoc.getFirstPage()); String actualText = extractionStrategy.getResultantText(); System.out.println(actualText); pdfDoc.close(); }
public static void addBookMark(Book book,String src,String dest){ PdfDocument pdfDoc = null; try { pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); PdfOutline root = pdfDoc.getOutlines(false); PdfDocumentInfo info=pdfDoc.getDocumentInfo(); info.setTitle(book.getName()); info.setAuthor(book.getAuthor()); List<Node> nodes = book.getOutline(); addOutline(nodes, root, pdfDoc); pdfDoc.close(); } catch (IOException e) { e.printStackTrace(); } }
public static void addBookMark(Book book,String src,String dest){ PdfDocument pdfDoc = null; try { pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); PdfOutline root = pdfDoc.getOutlines(false); PdfDocumentInfo info=pdfDoc.getDocumentInfo(); info.setTitle(book.getName()); info.setAuthor(CoreService.baseUrl+"/book/"+book.getId()); List<Node> nodes = book.getOutline(); addOutline(nodes, root, pdfDoc); pdfDoc.close(); } catch (IOException e) { e.printStackTrace(); } }
public void closeRoutine() { loader = null; PdfDocument docToClose = null; if (pdfFile != null && pdfFile.getPdfDocument() != null) docToClose = pdfFile.getPdfDocument(); pdfFile = null; setChanged(); super.notifyObservers(new CloseDocumentEvent()); if (docToClose != null) { docToClose.close(); } if (!pluginMode) { ownedFrame.setTitle("iText RUPS " + Version.getInstance().getVersion()); } readerController.getParser().setDocument(null); }
protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage()); Barcode128 code128 = new Barcode128(pdfDoc); code128.setCode("123456789"); code128.setCodeType(Barcode128.CODE128); PdfFormXObject xObject = code128.createFormXObject(Color.BLACK, Color.BLACK, pdfDoc); float x = 36; float y = 750; float w = xObject.getWidth(); float h = xObject.getHeight(); canvas.saveState(); canvas.setFillColor(Color.LIGHT_GRAY); canvas.rectangle(x, y, w, h); canvas.fill(); canvas.restoreState(); canvas.addXObject(xObject, 36, 750); pdfDoc.close(); } }