/** * Creates a PdfDocumentEvent. * * @param type type of the event that fired this event * @param page page that fired this event */ public PdfDocumentEvent(String type, PdfPage page) { super(type); this.page = page; this.document = page.getDocument(); }
private void tryFlushPageTags() { try { if (!getDocument().isClosing) { getDocument().getTagStructureContext().flushPageTags(this); } getDocument().getStructTreeRoot().savePageStructParentIndexIfNeeded(this); } catch (Exception ex) { throw new PdfException(PdfException.TagStructureFlushingFailedItMightBeCorrupted, ex); } }
/** * This method gets outlines of a current page * * @return return all outlines of a current page */ public List<PdfOutline> getOutlines(boolean updateOutlines) { getDocument().getOutlines(updateOutlines); return getDocument().getCatalog().getPagesWithOutlines().get(getPdfObject()); }
/** * Checks page before adding. * * @param page {@link PdfPage} to add. */ protected void checkAndAddPage(PdfPage page) { if (page.isFlushed()) throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page); if (page.getDocument() != null && this != page.getDocument()) throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, this, page.getDocument()); catalog.getPageTree().addPage(page); }
private void flushMustBeIndirectObject(PdfObject obj) { // TODO DEVSIX-744 obj.makeIndirect(getDocument()).flush(); }
/** * Checks page before adding and add. * * @param index one-base index of the page. * @param page {@link PdfPage} to add. */ protected void checkAndAddPage(int index, PdfPage page) { if (page.isFlushed()) { throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page); } if (page.getDocument() != null && this != page.getDocument()) { throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, this, page.getDocument()); } catalog.getPageTree().addPage(index, page); }
/** * Calculates and returns next available MCID reference. * * @return calculated MCID reference. * @throws PdfException in case of not tagged document. */ public int getNextMcid() { if (!getDocument().isTagged()) { throw new PdfException(PdfException.MustBeATaggedDocument); } if (mcid == -1) { PdfStructTreeRoot structTreeRoot = getDocument().getStructTreeRoot(); mcid = structTreeRoot.getNextMcidForPage(this); } return mcid++; }
/** * Creates a new Canvas to manipulate a specific page content stream. The given page shall not be flushed: * drawing on flushed pages is impossible because their content is already written to the output stream. * Use this constructor to be able to add {@link com.itextpdf.layout.element.Link} elements on it * (using any other constructor would result in inability to add PDF annotations, based on which, for example, links work). * <p> * If the {@link PdfDocument#isTagged()} is true, using this constructor would automatically enable * the tagging for the content. Regarding tagging the effect is the same as using {@link #enableAutoTagging(PdfPage)}. * * @param page the page on which this canvas will be rendered, shall not be flushed (see {@link PdfPage#isFlushed()}). * @param rootArea the maximum area that the Canvas may write upon */ public Canvas(PdfPage page, Rectangle rootArea) { this(initPdfCanvasOrThrowIfPageIsFlushed(page), page.getDocument(), rootArea); this.enableAutoTagging(page); this.isCanvasOfPage = true; }
/** * Creates a new Canvas to manipulate a specific page content stream. The given page shall not be flushed: * drawing on flushed pages is impossible because their content is already written to the output stream. * Use this constructor to be able to add {@link com.itextpdf.layout.element.Link} elements on it * (using any other constructor would result in inability to add PDF annotations, based on which, for example, links work). * <p> * If the {@link PdfDocument#isTagged()} is true, using this constructor would automatically enable * the tagging for the content. Regarding tagging the effect is the same as using {@link #enableAutoTagging(PdfPage)}. * * @param page the page on which this canvas will be rendered, shall not be flushed (see {@link PdfPage#isFlushed()}). * @param rootArea the maximum area that the Canvas may write upon */ public Canvas(PdfPage page, Rectangle rootArea) { this(initPdfCanvasOrThrowIfPageIsFlushed(page), page.getDocument(), rootArea); this.enableAutoTagging(page); this.isCanvasOfPage = true; }
/** * Convenience method for fast PdfCanvas creation by a certain page. * * @param page page to create canvas from. */ public PdfCanvas(PdfPage page) { this(page, (page.getDocument().getReader() != null && page.getDocument().getWriter() != null && page.getContentStreamCount() > 0 && page.getLastContentStream().getLength() > 0) || (page.getRotation() != 0 && page.isIgnorePageRotationForContent())); }
@Override public void copy(PdfPage fromPage, PdfPage toPage) { if (documentFrom != fromPage.getDocument()) { documentFrom = fromPage.getDocument(); formFrom = PdfAcroForm.getAcroForm(documentFrom, false); if (documentTo != toPage.getDocument()) { documentTo = toPage.getDocument(); formTo = PdfAcroForm.getAcroForm(documentTo, true);
private void addWidgetAnnotationToPage(PdfPage page, PdfAnnotation annot) { if (page.containsAnnotation(annot)) { return; } TagTreePointer tagPointer = null; boolean tagged = page.getDocument().isTagged(); if (tagged) { tagPointer = page.getDocument().getTagStructureContext().getAutoTaggingPointer(); //TODO attributes? tagPointer.addTag(StandardRoles.FORM); } page.addAnnotation(annot); if (tagged) { tagPointer.moveToParent(); } }
private void addWidgetAnnotationToPage(PdfPage page, PdfAnnotation annot) { if (page.containsAnnotation(annot)) { return; } TagTreePointer tagPointer = null; boolean tagged = page.getDocument().isTagged(); if (tagged) { tagPointer = page.getDocument().getTagStructureContext().getAutoTaggingPointer(); //TODO attributes? tagPointer.addTag(StandardRoles.FORM); } page.addAnnotation(annot); if (tagged) { tagPointer.moveToParent(); } }
private int getOrCreatePageStructParentIndex(PdfPage page) { int structParentIndex = page.getStructParentIndex(); if (structParentIndex < 0) { structParentIndex = page.getDocument().getNextStructParentIndex(); page.getPdfObject().put(PdfName.StructParents, new PdfNumber(structParentIndex)); } return structParentIndex; } }
private PdfObject getInheritedValue(PdfName pdfName, int type) { if (this.parentPages == null) { this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this); } PdfObject val = getInheritedValue(this.parentPages, pdfName); return val != null && val.getType() == type ? val : null; }
/** * Sets the XMP Metadata. * * @param xmpMetadata the {@code byte[]} of XMP Metadata to set. * @return this {@link PdfPage} instance. * @throws IOException in case of writing error. */ public PdfPage setXmpMetadata(byte[] xmpMetadata) throws IOException { PdfStream xmp = (PdfStream) new PdfStream().makeIndirect(getDocument()); xmp.getOutputStream().write(xmpMetadata); xmp.put(PdfName.Type, PdfName.Metadata); xmp.put(PdfName.Subtype, PdfName.XML); put(PdfName.Metadata, xmp); return this; }
getDocument().getCatalog().addNameToNameTree(description, fs.getPdfObject(), PdfName.EmbeddedFiles);
private void copyExistingField(PdfPage toPage, PdfAnnotation currentAnnot) { PdfFormField field = mergeFieldsWithTheSameName(PdfFormField.makeFormField(currentAnnot.getPdfObject(), toPage.getDocument())); PdfArray kids = field.getKids(); if (kids != null) { field.getPdfObject().remove(PdfName.Kids); formTo.addField(field, toPage); field.getPdfObject().put(PdfName.Kids, kids); } else { formTo.addField(field, toPage); } }
private void copyExistingField(PdfPage toPage, PdfAnnotation currentAnnot) { PdfFormField field = mergeFieldsWithTheSameName(PdfFormField.makeFormField(currentAnnot.getPdfObject(), toPage.getDocument())); PdfArray kids = field.getKids(); if (kids != null) { field.getPdfObject().remove(PdfName.Kids); formTo.addField(field, toPage); field.getPdfObject().put(PdfName.Kids, kids); } else { formTo.addField(field, toPage); } }
/** * Convenience method for fast PdfCanvas creation by a certain page. * * @param page page to create canvas from. * @param wrapOldContent true to wrap all old content streams into q/Q operators so that the state of old * content streams would not affect the new one */ public PdfCanvas(PdfPage page, boolean wrapOldContent) { this(getPageStream(page), page.getResources(), page.getDocument()); if (wrapOldContent) { // Wrap old content in q/Q in order not to get unexpected results because of the CTM page.newContentStreamBefore().getOutputStream().writeBytes(ByteUtils.getIsoBytes("q\n")); contentStream.getOutputStream().writeBytes(ByteUtils.getIsoBytes("Q\n")); } if (page.getRotation() != 0 && page.isIgnorePageRotationForContent() && (wrapOldContent || !page.isPageRotationInverseMatrixWritten())) { applyRotation(page); page.setPageRotationInverseMatrixWritten(); } }