if (isFlushed()) { return; getDocument().dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, this)); if (getDocument().isTagged() && !getDocument().getStructTreeRoot().isFlushed()) { tryFlushPageTags(); getResources(); if (resources != null && resources.isModified() && !resources.isReadOnly()) { getPdfObject().put(PdfName.Resources, resources.getPdfObject()); getDocument().checkIsoConformance(this, IsoKey.PAGE); flushResourcesContentStreams(); int contentStreamCount = getContentStreamCount(); for (int i = 0; i < contentStreamCount; i++) { getContentStream(i).flush(false);
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(); } }
@Override public void handleEvent(Event event) { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfDocument pdfDoc = docEvent.getDocument(); PdfPage page = docEvent.getPage(); PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); Rectangle area = page.getPageSize(); new Canvas(canvas, pdfDoc, area) .add(images.get(offset)); offset++; } }
/** * Returns the Contents object if it is {@link PdfStream}, or first stream in the array if it is {@link PdfArray}. * * @return first {@link PdfStream} in Contents object, or {@code null} if Contents is empty. */ public PdfStream getFirstContentStream() { if (getContentStreamCount() > 0) return getContentStream(0); return null; }
/** * 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())); }
/** * Gets the {@link Rectangle} object specified by page's BleedBox, that define the region to which the * contents of the page shall be clipped when output in a production environment. * * @return the {@link Rectangle} object specified by page's BleedBox, expressed in default user space units. * CropBox by default. */ public Rectangle getBleedBox() { Rectangle bleedBox = getPdfObject().getAsRectangle(PdfName.BleedBox); return bleedBox == null ? getCropBox() : bleedBox; }
private void checkPage(PdfPage page) { PdfDictionary pageDict = page.getPdfObject(); if (isAlreadyChecked(pageDict)) return; checkPageObject(pageDict, page.getResources().getPdfObject()); PdfDictionary pageResources = page.getResources().getPdfObject(); checkResources(pageResources); checkAnnotations(pageDict); checkPageSize(pageDict); int contentStreamCount = page.getContentStreamCount(); for (int j = 0; j < contentStreamCount; ++j) { checkedObjects.add(page.getContentStream(j)); } }
PdfObject resources = document.getPage(i).getPdfObject().getAsDictionary(PdfName.Resources); initialPageResourceClones.put(i, resources == null ? null : resources.clone()); if (page.isFlushed()) { throw new PdfException(PdfException.PageAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing); PdfObject pageResources = page.getResources().getPdfObject(); if (xObjectResources != null && pageResources != null && xObjectResources == pageResources) { fFields.remove(fieldObject); if (annotation != null) { page.removeAnnotation(annotation);
/** * Gets a list of marked content references on page. */ public Map<Integer, PdfMcr> getPageMarkedContentReferences(PdfPage page) { return pageToPageMcrs.get(page.getPdfObject().getIndirectReference()); }
/** * 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(); } }
PdfDictionary dictionary = getPdfObject().copyTo(toDocument, excludedKeys, true); PdfPage page = new PdfPage(dictionary); copyInheritedProperties(page, toDocument); for (PdfAnnotation annot : getAnnotations()) { if (annot.getSubtype().equals(PdfName.Link)) { getDocument().storeLinkAnnotation(page, (PdfLinkAnnotation) annot); } else { PdfAnnotation newAnnot = PdfAnnotation.makeAnnotation( ); if (PdfName.Widget.equals(annot.getSubtype())) { rebuildFormFieldParent(annot.getPdfObject(), newAnnot.getPdfObject(), toDocument); page.addAnnotation(-1, newAnnot, false); copier.copy(this, page); } else { if (!toDocument.getWriter().isUserWarnedAboutAcroFormCopying && getDocument().getCatalog().getPdfObject().containsKey(PdfName.AcroForm)) { Logger logger = LoggerFactory.getLogger(PdfPage.class); logger.warn(LogMessageConstant.SOURCE_DOCUMENT_HAS_ACROFORM_DICTIONARY);
private void copyInheritedProperties(PdfPage copyPdfPage, PdfDocument pdfDocument) { if (copyPdfPage.getPdfObject().get(PdfName.Resources) == null) { PdfObject copyResource = pdfDocument.getWriter().copyObject(getResources().getPdfObject(), pdfDocument, false); copyPdfPage.getPdfObject().put(PdfName.Resources, copyResource); } if (copyPdfPage.getPdfObject().get(PdfName.MediaBox) == null) { //media box shall be in any case copyPdfPage.setMediaBox(getMediaBox()); } if (copyPdfPage.getPdfObject().get(PdfName.CropBox) == null) { //original pdfObject don't have CropBox, otherwise copyPdfPage will contain it PdfArray cropBox = (PdfArray) getInheritedValue(PdfName.CropBox, PdfObject.ARRAY); //crop box is optional, we shall not set default value. if (cropBox != null) { copyPdfPage.put(PdfName.CropBox, cropBox.copyTo(pdfDocument)); } } if (copyPdfPage.getPdfObject().get(PdfName.Rotate) == null) { //original pdfObject don't have Rotate, otherwise copyPdfPage will contain it PdfNumber rotate = (PdfNumber) getInheritedValue(PdfName.Rotate, PdfObject.NUMBER); //rotate is optional, we shall not set default value. if (rotate != null) { copyPdfPage.put(PdfName.Rotate, rotate.copyTo(pdfDocument)); } } }
public void savePageStructParentIndexIfNeeded(PdfPage page) { PdfIndirectReference indRef = page.getPdfObject().getIndirectReference(); if (page.isFlushed() || pageToPageMcrs.get(indRef) == null) { return; } boolean hasNonObjRefMcr = false; for (Integer key : pageToPageMcrs.get(indRef).keySet()) { if (key < 0) { continue; } hasNonObjRefMcr = true; break; } if (hasNonObjRefMcr) { pageToStructParentsInd.put(indRef, (Integer) getOrCreatePageStructParentIndex(page)); } }
protected void flushSingleRenderer(IRenderer resultRenderer) { Transform transformProp = resultRenderer.<Transform>getProperty(Property.TRANSFORM); if (!waitingDrawingElements.contains(resultRenderer)) { processWaitingDrawing(resultRenderer, transformProp, waitingDrawingElements); if (FloatingHelper.isRendererFloating(resultRenderer) || transformProp != null) return; } if (!resultRenderer.isFlushed() && null != resultRenderer.getOccupiedArea()) { // TODO Remove checking occupied area to be not null when DEVSIX-1001 is resolved. int pageNum = resultRenderer.getOccupiedArea().getPageNumber(); PdfDocument pdfDocument = document.getPdfDocument(); ensureDocumentHasNPages(pageNum, null); PdfPage correspondingPage = pdfDocument.getPage(pageNum); if (correspondingPage.isFlushed()) { throw new PdfException(PdfException.CannotDrawElementsOnAlreadyFlushedPages); } boolean wrapOldContent = pdfDocument.getReader() != null && pdfDocument.getWriter() != null && correspondingPage.getContentStreamCount() > 0 && correspondingPage.getLastContentStream().getLength() > 0 && !wrappedContentPage.contains(pageNum) && pdfDocument.getNumberOfPages() >= pageNum; wrappedContentPage.add(pageNum); if (pdfDocument.isTagged()) { pdfDocument.getTagStructureContext().getAutoTaggingPointer().setPageForTagging(correspondingPage); } resultRenderer.draw(new DrawContext(pdfDocument, new PdfCanvas(correspondingPage, wrapOldContent), pdfDocument.isTagged())); } }
/** * Copies page as FormXObject to the specified document. * * @param toDocument a document to copy to. * @return copied {@link PdfFormXObject} object. */ public PdfFormXObject copyAsFormXObject(PdfDocument toDocument) throws IOException { PdfFormXObject xObject = new PdfFormXObject(getCropBox()); List<PdfName> excludedKeys = new ArrayList<>(Arrays.asList(PdfName.MediaBox, PdfName.CropBox, PdfName.Contents) ); excludedKeys.addAll(this.excludedKeys); PdfDictionary dictionary = getPdfObject().copyTo(toDocument, excludedKeys, true); xObject.getPdfObject().getOutputStream().write(getContentBytes()); xObject.getPdfObject().mergeDifferent(dictionary); //Copy inherited resources if (!xObject.getPdfObject().containsKey(PdfName.Resources)) { PdfObject copyResource = getResources().getPdfObject().copyTo(toDocument, true); xObject.getPdfObject().put(PdfName.Resources, copyResource); } return xObject; }
private PdfPage getFieldPage(PdfDictionary annotDic) { PdfDictionary pageDic = annotDic.getAsDictionary(PdfName.P); if (pageDic != null) { return document.getPage(pageDic); } for (int i = 1; i <= document.getNumberOfPages(); i++) { PdfPage page = document.getPage(i); if (!page.isFlushed()) { PdfAnnotation annotation = PdfAnnotation.makeAnnotation(annotDic); if (annotation != null && page.containsAnnotation(annotation)) { return page; } } } return null; }
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 PdfStream newContentStream(boolean before) { PdfObject contents = getPdfObject().get(PdfName.Contents); PdfArray array; if (contents instanceof PdfStream) { array = new PdfArray(); array.add(contents); put(PdfName.Contents, array); } else if (contents instanceof PdfArray) { array = (PdfArray) contents; } else { array = null; } PdfStream contentStream = (PdfStream) new PdfStream().makeIndirect(getDocument()); if (array != null) { if (before) { array.add(0, contentStream); } else { array.add(contentStream); } if (array.getIndirectReference() != null) { array.setModified(); } else { setModified(); } } else { put(PdfName.Contents, contentStream); } return contentStream; }
PdfArray annots = getAnnots(false); if (annots != null) { annots.remove(annotation.getPdfObject()); getPdfObject().remove(PdfName.Annots); setModified(); } else if (annots.getIndirectReference() == null) { setModified(); if (getDocument().isTagged()) { TagTreePointer tagPointer = getDocument().getTagStructureContext().removeAnnotationTag(annotation); if (tagPointer != null) { boolean standardAnnotTagRole = tagPointer.getRole().equals(StandardRoles.ANNOT)
/** * 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()); }