/** * Returns the page tree. * * @return the page tree */ public PDPageTree getPages() { return getDocumentCatalog().getPages(); }
PDDocument doc = PDDocument.load("C:/mydoc3.pdf"); List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); for(PDPage page:pages){ Map<String,PDFont> pageFonts=page.getResources().getFonts(); }
private boolean shouldHandleXFAOnly(PDDocument pdDocument, PDFParserConfig config) { if (config.getIfXFAExtractOnlyXFA() && pdDocument.getDocumentCatalog() != null && pdDocument.getDocumentCatalog().getAcroForm() != null && pdDocument.getDocumentCatalog().getAcroForm().getXFA() != null) { return true; } return false; }
/** * This will return the total page count of the PDF document. * * @return The total number of pages in the PDF document. */ public int getNumberOfPages() { return getDocumentCatalog().getPages().getCount(); }
/** * Indicates whether an optional content group is enabled. * @param group the group * @return true if the group is enabled */ public boolean isGroupEnabled(PDOptionalContentGroup group) { PDOptionalContentProperties ocProperties = document.getDocumentCatalog().getOCProperties(); return ocProperties == null || ocProperties.isGroupEnabled(group); }
void extractBookmarkText() throws SAXException, IOException, TikaException { PDDocumentOutline outline = document.getDocumentCatalog().getDocumentOutline(); if (outline != null) { extractBookmarkText(outline); } }
/** * Retrieve all signature fields from the document. * * @return a <code>List</code> of <code>PDSignatureField</code>s */ public List<PDSignatureField> getSignatureFields() { List<PDSignatureField> fields = new ArrayList<>(); PDAcroForm acroForm = getDocumentCatalog().getAcroForm(); if (acroForm != null) { for (PDField field : acroForm.getFieldTree()) { if (field instanceof PDSignatureField) { fields.add((PDSignatureField)field); } } } return fields; }
/** * Returns the page at the given 0-based index. * <p> * This method is too slow to get all the pages from a large PDF document * (1000 pages or more). For such documents, use the iterator of * {@link PDDocument#getPages()} instead. * * @param pageIndex the 0-based page index * @return the page at the given index. */ public PDPage getPage(int pageIndex) // todo: REPLACE most calls to this method with BELOW method { return getDocumentCatalog().getPages().get(pageIndex); }
@Override public void createAcroForm(PDDocument template) { PDAcroForm theAcroForm = new PDAcroForm(template); template.getDocumentCatalog().setAcroForm(theAcroForm); pdfStructure.setAcroForm(theAcroForm); LOG.info("AcroForm has been created"); }
/** * Check if metadata dictionary has no stream filter * * @param doc the document to check. * @return the list of validation errors. */ protected List<ValidationError> checkStreamFilterUsage(PDDocument doc) { List<ValidationError> ve = new ArrayList<>(); List<?> filters = doc.getDocumentCatalog().getMetadata().getFilters(); if (filters != null && !filters.isEmpty()) { ve.add(new ValidationError(PreflightConstants.ERROR_METADATA_MAIN, "Using stream filter on metadata dictionary is forbidden")); } return ve; } }
/** * This will print all the fields from the document. * * @param pdfDocument The PDF to get the fields from. * * @throws IOException If there is an error getting the fields. */ public void printFields(PDDocument pdfDocument) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); List<PDField> fields = acroForm.getFields(); System.out.println(fields.size() + " top-level fields were found on the form"); for (PDField field : fields) { processField(field, "|--", field.getPartialName()); } }
/** * This will takes the values from the fdf document and import them into the * PDF document. * * @param pdfDocument The document to put the fdf data into. * @param fdfDocument The FDF document to get the data from. * * @throws IOException If there is an error setting the data in the field. */ public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); acroForm.setCacheFields( true ); acroForm.importFDF( fdfDocument ); }
//Using PDFBox library available from http://pdfbox.apache.org/ //Writes pdf document of specific pages as a new pdf file //Reads in pdf document PDDocument pdDoc = PDDocument.load(file); //Creates a new pdf document PDDocument document = null; //Adds specific page "i" where "i" is the page number and then saves the new pdf document try { document = new PDDocument(); document.addPage((PDPage) pdDoc.getDocumentCatalog().getAllPages().get(i)); document.save("file path"+"new document title"+".pdf"); document.close(); }catch(Exception e){}
@Override protected void startDocument(PDDocument pdf) throws IOException { try { xhtml.startDocument(); try { handleDestinationOrAction(pdf.getDocumentCatalog().getOpenAction(), ActionTrigger.DOCUMENT_OPEN); } catch (IOException e) { //See PDFBOX-3773 //swallow -- no need to report this } } catch (TikaException|SAXException e) { throw new IOExceptionWithCause("Unable to start a document", e); } }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { // Note that the JavaScript will depend on the reader application. // The classes and methods available to Adobe Reader and Adobe Acrobat // are documented in the Acrobat SDK. String javaScript = "var now = util.printd('yyyy-mm-dd', new Date());" + "var oField = this.getField('SampleField');" + "oField.value = now;"; // Create an action as JavaScript action PDActionJavaScript jsAction = new PDActionJavaScript(); jsAction.setAction(javaScript); // Set the action to be executed when the document is opened document.getDocumentCatalog().setOpenAction(jsAction); document.save("target/UpdateFieldOnDocumentOpen.pdf"); } } }
private void initRectMap() { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); if (acroForm == null) { return; } for (PDField field : acroForm.getFieldTree()) { String fullyQualifiedName = field.getFullyQualifiedName(); for (PDAnnotationWidget widget : field.getWidgets()) { if (page.equals(widget.getPage())) { rectMap.put(widget.getRectangle(), fullyQualifiedName); } } } }
private void handleXFAOnly(PDDocument pdDocument, ContentHandler handler, Metadata metadata, ParseContext context) throws SAXException, IOException, TikaException { XFAExtractor ex = new XFAExtractor(); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); try (InputStream is = new ByteArrayInputStream( pdDocument.getDocumentCatalog().getAcroForm().getXFA().getBytes())) { ex.extract(is, xhtml, metadata, context); } catch (XMLStreamException e) { throw new TikaException("XML error in XFA", e); } xhtml.endDocument(); }
/** * Create a new document to write the split contents to. * * @return the newly created PDDocument. * @throws IOException If there is an problem creating the new document. */ protected PDDocument createNewDocument() throws IOException { PDDocument document = memoryUsageSetting == null ? new PDDocument() : new PDDocument(memoryUsageSetting); document.getDocument().setVersion(getSourceDocument().getVersion()); document.setDocumentInformation(getSourceDocument().getDocumentInformation()); document.getDocumentCatalog().setViewerPreferences( getSourceDocument().getDocumentCatalog().getViewerPreferences()); return document; }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); // Get the field and the widget associated to it. // Note: there might be multiple widgets PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); // Create the definition for a green border PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); fieldAppearance.setBorderColour(green); // Set the information to be used by the widget which is responsible // for the visual style of the form field. widget.setAppearanceCharacteristics(fieldAppearance); document.save("target/AddBorderToField.pdf"); } } }
@Override public void validate(PreflightContext ctx) throws ValidationException { PDDocument pdfbox = ctx.getDocument(); this.catalog = pdfbox.getDocumentCatalog(); if (this.catalog == null) { ctx.addValidationError(new ValidationError(ERROR_SYNTAX_NOCATALOG, "There are no Catalog entry in the Document")); } else { validateActions(ctx); validateLang(ctx); validateNames(ctx); validateOCProperties(ctx); validateOutputIntent(ctx); } }