protected PdfDocument initDocument(PdfReader reader, PdfWriter writer, StampingProperties properties) { PdfAConformanceLevel conformanceLevel = reader.getPdfAConformanceLevel(); if (null == conformanceLevel) { return new PdfDocument(reader, writer, properties); } else { return new PdfADocument(reader, writer, properties); } }
protected PdfDocument initDocument(PdfReader reader, PdfWriter writer, StampingProperties properties) { PdfAConformanceLevel conformanceLevel = reader.getPdfAConformanceLevel(); if (null == conformanceLevel) { return new PdfDocument(reader, writer, properties); } else { return new PdfADocument(reader, writer, properties); } }
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 void write() { this.createOutputStream(); try (PdfWriter writer = new PdfWriter(this.outputStream)) { PdfDocument pdfDoc = new PdfDocument(writer); Object o = this.deal.deal(); convertToPdf(o, pdfDoc); } catch (IOException e) { throw new RuntimeException("PDF写入错误", e); } }
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(); } } }
System.out.print("[itext] INFO Comparing link annotations...."); String message = null; PdfDocument outDocument = new PdfDocument(new PdfReader(outPdf), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); PdfDocument cmpDocument = new PdfDocument(new PdfReader(cmpPdf), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); for (int i = 0; i < outDocument.getNumberOfPages() && i < cmpDocument.getNumberOfPages(); i++) { List<PdfLinkAnnotation> outLinks = getLinkAnnotations(i + 1, outDocument);
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; } }
/** * 将图片合成为一个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(); }
/** * Switches to the previous revision. * @throws IOException * @throws GeneralSecurityException */ public void switchToPreviousRevision() throws IOException, GeneralSecurityException { LOGGER.info("Switching to previous revision."); latestRevision = false; dss = document.getCatalog().getPdfObject().getAsDictionary(PdfName.DSS); Calendar cal = pkcs7.getTimeStampDate(); if (cal == SignUtils.UNDEFINED_TIMESTAMP_DATE) cal = pkcs7.getSignDate(); // TODO: get date from signature signDate = cal.getTime(); List<String> names = sgnUtil.getSignatureNames(); if (names.size() > 1) { signatureName = names.get(names.size() - 2); document = new PdfDocument(new PdfReader(sgnUtil.extractRevision(signatureName)), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); this.acroForm = PdfAcroForm.getAcroForm(document, true); this.sgnUtil = new SignatureUtil(document); names = sgnUtil.getSignatureNames(); signatureName = names.get(names.size() - 1); pkcs7 = coversWholeDocument(); LOGGER.info(MessageFormatUtil.format("Checking {0}signature {1}", pkcs7.isTsp() ? "document-level timestamp " : "", signatureName)); } else { LOGGER.info("No signatures in revision"); pkcs7 = null; } }
/** * Switches to the previous revision. * @throws IOException * @throws GeneralSecurityException */ public void switchToPreviousRevision() throws IOException, GeneralSecurityException { LOGGER.info("Switching to previous revision."); latestRevision = false; dss = document.getCatalog().getPdfObject().getAsDictionary(PdfName.DSS); Calendar cal = pkcs7.getTimeStampDate(); if (cal == SignUtils.UNDEFINED_TIMESTAMP_DATE) cal = pkcs7.getSignDate(); // TODO: get date from signature signDate = cal.getTime(); List<String> names = sgnUtil.getSignatureNames(); if (names.size() > 1) { signatureName = names.get(names.size() - 2); document = new PdfDocument(new PdfReader(sgnUtil.extractRevision(signatureName)), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); this.acroForm = PdfAcroForm.getAcroForm(document, true); this.sgnUtil = new SignatureUtil(document); names = sgnUtil.getSignatureNames(); signatureName = names.get(names.size() - 1); pkcs7 = coversWholeDocument(); LOGGER.info(MessageFormatUtil.format("Checking {0}signature {1}", pkcs7.isTsp() ? "document-level timestamp " : "", signatureName)); } else { LOGGER.info("No signatures in revision"); pkcs7 = null; } }
private PdfDocument createPdfDocument(PageRange currentPageRange) { PdfDocument newDocument = new PdfDocument(getNextPdfWriter(currentPageRange), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); if (pdfDocument.isTagged() && preserveTagged) newDocument.setTagged(); if (pdfDocument.hasOutlines() && preserveOutlines) newDocument.initializeOutlines(); return newDocument; }
String message = null; setPassword(outPass, cmpPass); PdfDocument outDocument = new PdfDocument(new PdfReader(outPdf, getOutReaderProperties()), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); PdfDocument cmpDocument = new PdfDocument(new PdfReader(cmpPdf, getCmpReaderProperties()), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); String[] cmpInfo = convertInfo(cmpDocument.getDocumentInfo()); String[] outInfo = convertInfo(outDocument.getDocumentInfo());
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(); } }
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(); } }
PdfDocument outDocument = null; try { cmpDocument = new PdfDocument(new PdfReader(this.cmpPdf), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); outDocument = new PdfDocument(new PdfReader(this.outPdf), new DocumentProperties().setEventCountingMetaInfo(metaInfo)); byte[] cmpBytes = cmpDocument.getXmpMetadata(), outBytes = outDocument.getXmpMetadata(); if (ignoreDateAndProducerProperties) {