List of usage examples for org.apache.pdfbox.pdmodel PDDocument getDocumentCatalog
public PDDocumentCatalog getDocumentCatalog()
From source file:editorframework.pdfbox.testes.TestesComPDFBox.java
public static void exibirPDFemImagemNoJComponent() throws IOException { PDDocument document = PDDocument.load(new File("./simple.pdf")); List<PDPage> allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = allPages.get(0);//from www . j a v a 2 s . c o m BufferedImage bi = firstPage.convertToImage(); JFrame testFrame = new JFrame(); testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollPane scrollPane = new JScrollPane(); JLabel picLabel = new JLabel(new ImageIcon(bi)); scrollPane.getViewport().add(picLabel); testFrame.add(scrollPane); testFrame.setVisible(true); testFrame.pack(); }
From source file:edu.uwm.jiaoduan.lab.ExtractTextByArea.java
License:Apache License
/** * This will print the documents text in a certain area. * * @param args The command line arguments. * * @throws Exception If there is an error parsing the document. *//*from www. ja va2 s .c om*/ public static void main(String[] args) throws Exception { args = new String[] { "test.pdf" }; if (args.length != 1) { usage(); } else { PDDocument document = null; try { document = PDDocument.load(args[0]); if (document.isEncrypted()) { try { document.decrypt(""); } catch (InvalidPasswordException e) { System.err.println("Error: Document is encrypted with a password."); System.exit(1); } } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); //Rectangle rect = new Rectangle( 99,219,80,15 ); //convert xfdf coordinate to rectangle Rectangle2D.Double rect = new Rectangle2D.Double(); List allPages = document.getDocumentCatalog().getAllPages(); PDPage firstPage = (PDPage) allPages.get(0); double pageHeight = firstPage.getMediaBox().getHeight(); //125.824906,672.39502,390.577109,694.679017 double[] coords = new double[] { 58.50615, 500.847504, 302.919073, 552.419312 }; //rect.height = 694.679017 - 672.39502; rect.height = coords[3] - coords[1]; //rect.width = 390.577109 - 125.824906; rect.width = coords[2] - coords[0]; ; //rect.x = 125.824906; rect.x = coords[0]; //rect.y = pageHeight -672.39502 - rect.height; rect.y = pageHeight - coords[1] - rect.height; System.out.println(rect); stripper.addRegion("class1", rect); stripper.extractRegions(firstPage); System.out.println("Text in the area:" + rect); System.out.println(stripper.getTextForRegion("class1")); } finally { if (document != null) { document.close(); } } } }
From source file:edworld.pdfreader4humans.PDFReader.java
License:Apache License
protected void readAllPages(PDDocument doc, PDFComponentLocator componentLocator, BoxDetector boxDetector, MarginDetector marginDetector) throws IOException { for (Object page : doc.getDocumentCatalog().getAllPages()) firstLevel.add(readPage((PDPage) page, componentLocator, boxDetector, marginDetector)); }
From source file:edworld.pdfreader4humans.PDFReader.java
License:Apache License
private PDRectangle getPageCropBox(int pageIndex) throws IOException { PDDocument doc = PDDocument.load(url); try {/*from www . j a va 2 s.c o m*/ return ((PDPage) doc.getDocumentCatalog().getAllPages().get(pageIndex - 1)).findCropBox(); } finally { doc.close(); } }
From source file:es.jscan.Pantallas.PantallaPrincipal.java
License:Apache License
public void importarDePdf(final File archivo) { final PantallaBarra pantbarra = new PantallaBarra(PantallaPrincipal.this, false); new Thread() { @Override/* w w w . j a v a 2s .c o m*/ public void run() { org.apache.pdfbox.pdmodel.PDDocument documento = null; try { documento = org.apache.pdfbox.pdmodel.PDDocument.load(archivo); } catch (IOException ex) { Utilidades.escribeLog("Error -importarDePdf- al importar del archivo PDF " + archivo.getAbsolutePath() + " - " + ex.getMessage()); return; } int startpage = 1; int endpage = Integer.MAX_VALUE; List pages = documento.getDocumentCatalog().getAllPages(); String titulo = pantbarra.getTitle(); pantbarra.barra.setMinimum(startpage); pantbarra.barra.setMaximum(pages.size()); pantbarra.barra.setValue(1); for (int i = startpage - 1; i < endpage && i < pages.size(); i++) { PDPage page = (PDPage) pages.get(i); java.awt.image.BufferedImage imagenpdf = null; try { imagenpdf = page.convertToImage(BufferedImage.TYPE_BYTE_GRAY, 200); } catch (IOException ex) { Utilidades.escribeLog("Error -importarDePdf- al convertir Imagen de PDF a Escala de Grises " + " - " + ex.getMessage()); } // Utilidades.escribeLog("Numero bits por pixel :"+imagenpdf.getColorModel().getPixelSize()); // imagenpdf = pantutil.convertirAGris((java.awt.image.BufferedImage) imagenpdf); guardarImagen(imagenpdf, contimagen); cargarMiniaturas(contimagen); JScrollBar bar = panelVisorMini.getVerticalScrollBar(); bar.setValue(bar.getMaximum()); pantbarra.barra.setValue(i); pantbarra.setTitle(titulo + " " + (i + 1) + " de " + pages.size()); contimagen++; doLayout(); if (pantbarra.PARAR) { break; } } PDPage page = (PDPage) pages.get(pages.size() - 1); java.awt.image.BufferedImage imagenpdf = null; try { imagenpdf = page.convertToImage(); imagenpdf = pantutil.convertirAGris((java.awt.image.BufferedImage) imagenpdf); documento.close(); } catch (IOException ex) { Utilidades.escribeLog("Error -importarDePdf- al convertir Imagen de PDF a Escala de Grises " + " - " + ex.getMessage()); } JScrollBar bar = panelVisorMini.getVerticalScrollBar(); if (pantbarra.PARAR) { pintarImagenPorIndice(0); ponerBordeBoton(0); bar.setValue(bar.getMinimum()); } else { pintarImagen(imagenpdf); ponerBordeBoton(contimagen - 1); bar.setValue(bar.getMaximum()); } pantbarra.dispose(); } }.start(); pantbarra.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); pantbarra.setVisible(false); pantbarra.setVisible(true); }
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxCMSInfo.java
License:Open Source License
/** * @param validationCertPool// w w w. ja v a2s . co m * @param outerCatalog the PDF Dict of the outer document, if the PDFDocument in a enclosed revision. Can be null. * @param document the signed PDFDocument * @param cms the CMS bytes (CAdES signature) * @param inputStream the stream of the whole signed document * @throws IOException */ PdfBoxCMSInfo(CertificatePool validationCertPool, PdfDict outerCatalog, PDDocument document, PDSignature signature, byte[] cms, InputStream inputStream) throws DSSException, IOException { this.validationCertPool = validationCertPool; this.outerCatalog = PdfDssDict.build(outerCatalog); this.cms = cms; this.location = signature.getLocation(); this.signingDate = signature.getSignDate() != null ? signature.getSignDate().getTime() : null; this.signatureByteRange = signature.getByteRange(); final COSDictionary cosDictionary = document.getDocumentCatalog().getCOSDictionary(); final PdfBoxDict documentDict = new PdfBoxDict(cosDictionary, document); documentDictionary = PdfDssDict.build(documentDict); try { if (cms == null) { // due to not very good revision extracting throw new DSSPadesNoSignatureFound(); } signedBytes = signature.getSignedContent(inputStream); } catch (IOException e) { throw new DSSException(e); } }
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxDocTimestampInfo.java
License:Open Source License
/** * @param validationCertPool/*from w w w .j a va 2s. com*/ * @param outerCatalog the PDF Dict of the outer document, if the PDFDocument in a enclosed revision. Can be null. * @param document the signed PDFDocument * @param cms the CMS (CAdES) bytes * @param inputStream the stream of the whole signed document * @throws IOException */ PdfBoxDocTimestampInfo(CertificatePool validationCertPool, PdfDict outerCatalog, PDDocument document, PDSignature signature, byte[] cms, InputStream inputStream) throws DSSException, IOException { super(validationCertPool, outerCatalog, document, signature, cms, inputStream); try { TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(cms)); TimestampType timestampType = TimestampType.SIGNATURE_TIMESTAMP; if (document.getDocumentCatalog().getCOSDictionary().containsKey("DSS")) { timestampType = TimestampType.ARCHIVE_TIMESTAMP; } timestampToken = new TimestampToken(timeStampToken, timestampType, validationCertPool); if (LOG.isDebugEnabled()) { LOG.debug("Created PdfBoxDocTimestampInfo {}: {}", timestampType, uniqueId()); } } catch (CMSException e) { throw new DSSException(e); } catch (TSPException e) { throw new DSSException(e); } catch (IOException e) { throw new DSSException(e); } }
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxSignatureService.java
License:Open Source License
private void addExtraDictionaries(final PDDocument doc, final Map.Entry<String, PdfDict>[] extraDictionariesToAddBeforeSign) { final COSDictionary cosDictionary = doc.getDocumentCatalog().getCOSDictionary(); for (final Map.Entry<String, PdfDict> pdfDictEntry : extraDictionariesToAddBeforeSign) { final String key = pdfDictEntry.getKey(); final PdfBoxDict value = (PdfBoxDict) pdfDictEntry.getValue(); final COSDictionary wrapped = value.getWrapped(); cosDictionary.setItem(key, wrapped); }//from w ww. j a v a 2 s . co m }
From source file:eu.europa.ec.markt.dss.signature.pdf.pdfbox.PdfBoxSignatureService.java
License:Open Source License
/** * @param validationCertPool/* www. j ava2s. c om*/ * @param byteRangeMap * @param outerCatalog the PdfDictionary of the document that enclose the document stored in the input InputStream * @param input the Pdf bytes to open as a PDF * @return * @throws DSSException */ private Set<PdfSignatureOrDocTimestampInfo> validateSignatures(CertificatePool validationCertPool, Map<String, Set<PdfSignatureOrDocTimestampInfo>> byteRangeMap, PdfDict outerCatalog, InputStream input) throws DSSException { Set<PdfSignatureOrDocTimestampInfo> signaturesFound = new LinkedHashSet<PdfSignatureOrDocTimestampInfo>(); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); PDDocument doc = null; try { DSSUtils.copy(input, buffer); doc = PDDocument.load(new ByteArrayInputStream(buffer.toByteArray())); final PdfDict catalog = new PdfBoxDict(doc.getDocumentCatalog().getCOSDictionary(), doc); final List<PDSignature> signatureDictionaries = doc.getSignatureDictionaries(); if (LOG.isDebugEnabled()) { LOG.debug("Found {} signatures in PDF dictionary of PDF sized {} bytes", signatureDictionaries.size(), buffer.size()); } for (int i = 0; i < signatureDictionaries.size(); i++) { final PDSignature signature = signatureDictionaries.get(i); /** * SubFilter Name (Required) The value of SubFilter identifies the format of the data contained in the stream. * A conforming reader may use any conforming signature handler that supports the specified format. * When the value of Type is DocTimestamp, the value of SubFilter shall be ETSI.RFC3161. */ final String subFilter = signature.getSubFilter(); byte[] cms = new PdfBoxDict(signature.getDictionary(), doc).get("Contents"); PdfSignatureOrDocTimestampInfo signatureInfo; try { if (PdfBoxDocTimeStampService.SUB_FILTER_ETSI_RFC3161.getName().equals(subFilter)) { signatureInfo = PdfSignatureFactory.createPdfTimestampInfo(validationCertPool, outerCatalog, doc, signature, cms, buffer); } else { signatureInfo = PdfSignatureFactory.createPdfSignatureInfo(validationCertPool, outerCatalog, doc, signature, cms, buffer); } } catch (PdfSignatureOrDocTimestampInfo.DSSPadesNoSignatureFound e) { LOG.debug("No signature found in signature Dictionary:Content", e); continue; } signatureInfo = signatureAlreadyInListOrSelf(signaturesFound, signatureInfo); // should store in memory this byte range with a list of signature found there final String byteRange = Arrays.toString(signature.getByteRange()); Set<PdfSignatureOrDocTimestampInfo> innerSignaturesFound = byteRangeMap.get(byteRange); if (innerSignaturesFound == null) { // Recursive call to find inner signatures in the byte range covered by this signature. Deep first search. final byte[] originalBytes = signatureInfo.getOriginalBytes(); if (LOG.isDebugEnabled()) { LOG.debug( "Searching signature in the previous revision of the document, size of revision is {} bytes", originalBytes.length); } innerSignaturesFound = validateSignatures(validationCertPool, byteRangeMap, catalog, new ByteArrayInputStream(originalBytes)); byteRangeMap.put(byteRange, innerSignaturesFound); } // need to mark a signature as included inside another one. It's needed to link timestamp signature with the signatures covered by the timestamp. for (PdfSignatureOrDocTimestampInfo innerSignature : innerSignaturesFound) { innerSignature = signatureAlreadyInListOrSelf(signaturesFound, innerSignature); signaturesFound.add(innerSignature); innerSignature.addOuterSignature(signatureInfo); } signaturesFound.add(signatureInfo); } return signaturesFound; } catch (IOException up) { LOG.error("Error loading buffer of size {}", buffer.size(), up); // ignore error when loading signatures return signaturesFound; } finally { DSSPDFUtils.close(doc); } }
From source file:eu.europa.ejusticeportal.dss.controller.signature.PdfUtils.java
License:EUPL
/** * Adds an attachment to a PDF//from w w w. ja v a 2 s . c o m * * @param pdf the PDF to attach to * @param attachment to attachment * @param name the name given to the attachment * @return the PDF with attachment */ public static byte[] attach(final byte[] pdf, final byte[] attachment, final String name) { PDDocument doc = null; try { PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode(); InputStream isDoc = new ByteArrayInputStream(pdf); doc = PDDocument.load(isDoc); PDComplexFileSpecification fs = new PDComplexFileSpecification(); fs.setFile("Test.txt"); InputStream isAttach = new ByteArrayInputStream(attachment); PDEmbeddedFile ef = new PDEmbeddedFile(doc, isAttach); ef.setSize(attachment.length); ef.setCreationDate(Calendar.getInstance()); fs.setEmbeddedFile(ef); Map<String, PDComplexFileSpecification> efMap = new HashMap<String, PDComplexFileSpecification>(); efMap.put(name, fs); efTree.setNames(efMap); PDDocumentNameDictionary names = new PDDocumentNameDictionary(doc.getDocumentCatalog()); names.setEmbeddedFiles(efTree); doc.getDocumentCatalog().setNames(names); return toByteArray(doc); } catch (Exception e) { LOGGER.error("Error attaching.", e); throw new SigningException(e); } finally { closeQuietly(doc); } }