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(); } } }
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); } }
reader = new PdfReader(fis, new ReaderProperties().setPassword(password)); permissions.setEncrypted(true); permissions.setCryptoMode(reader.getCryptoMode()); permissions.setPermissions((int) reader.getPermissions()); if (reader.isOpenedWithFullPermission()) { permissions.setOwnerPassword(password); permissions.setUserPassword(reader.computeUserPassword()); } else { JOptionPane.showMessageDialog(null, "You opened the document using the user password instead of the owner password."); reader = new PdfReader(fis); permissions.setEncrypted(false);
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(); } } }
if (reader != null) { reader.pdfDocument = this; reader.readPdf(); for (ICounter counter : getCounters()) { counter.onDocumentRead(reader.getFileLength()); tryInitTagStructure(str); if (properties.appendMode && (reader.hasRebuiltXref() || reader.hasFixedXref())) throw new PdfException(PdfException.AppendModeRequiresADocumentWithoutErrorsEvenIfRecoveryWasPossible); if (reader != null && reader.hasXrefStm() && writer.properties.isFullCompression == null) { writer.properties.isFullCompression = true; if (reader != null && !reader.isOpenedWithFullPermission()) { throw new BadPasswordException(BadPasswordException.PdfReaderNotOpenedWithOwnerPassword); writer.write((byte) '\n'); writer.properties.isFullCompression = reader.hasXrefStm();
if (sorter.size() > 0) { try { if (((int[]) sorter.get(sorter.size() - 1)[1])[0] == document.getReader().getFileLength()) totalRevisions = sorter.size(); else
/** * Copies object to a specified document. * <br><br> * NOTE: Works only for objects that are read from document opened in reading mode, otherwise an exception is thrown. * * @param document document to copy object to. * @param allowDuplicating indicates if to allow copy objects which already have been copied. * If object is associated with any indirect reference and allowDuplicating is false then already existing reference will be returned instead of copying object. * If allowDuplicating is true then object will be copied and new indirect reference will be assigned. * @return copied object. */ public PdfObject copyTo(PdfDocument document, boolean allowDuplicating) { if (document == null) throw new PdfException(PdfException.DocumentForCopyToCannotBeNull); if (indirectReference != null) { // TODO checkState(MUST_BE_INDIRECT) now is always false, because indirectReference != null. See also DEVSIX-602 if (indirectReference.getWriter() != null || checkState(MUST_BE_INDIRECT)) { throw new PdfException(PdfException.CannotCopyIndirectObjectFromTheDocumentThatIsBeingWritten); } if (!indirectReference.getReader().isOpenedWithFullPermission()) { throw new BadPasswordException(BadPasswordException.PdfReaderNotOpenedWithOwnerPassword); } } return processCopying(document, allowDuplicating); }
reader.close(); } catch (Exception e) { Logger logger = LoggerFactory.getLogger(PdfDocument.class);
PdfName type = stream.getAsName(PdfName.Type); if (!PdfName.XRefStm.equals(type) && !PdfName.ObjStm.equals(type)) checkPdfStreamLength(stream); long offset = stream.getOffset(); if (offset <= 0)
if (sorter.size() > 0) { try { if (((int[]) sorter.get(sorter.size() - 1)[1])[0] == document.getReader().getFileLength()) totalRevisions = sorter.size(); else
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; } }
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(); }
if (document != null && document.getReader() != null && document.getReader().getPdfAConformanceLevel() != null) { field.pdfAConformanceLevel = document.getReader().getPdfAConformanceLevel();
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);
if (document != null && document.getReader() != null && document.getReader().getPdfAConformanceLevel() != null) { field.pdfAConformanceLevel = document.getReader().getPdfAConformanceLevel();
@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(); }
/** * 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; } }
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());