/** * Sets a page which content will be tagged with this instance of {@code TagTreePointer}. * To tag page content: * <ol> * <li>Set pointer position to the tag which will be the parent of the page content item;</li> * <li>Call {@link #getTagReference()} to obtain the reference to the current tag;</li> * <li>Pass {@link TagReference} to the {@link PdfCanvas#openTag(TagReference)} method of the page's {@link PdfCanvas} to start marked content item;</li> * <li>Draw content on {@code PdfCanvas};</li> * <li>Use {@link PdfCanvas#closeTag()} to finish marked content item.</li> * </ol> * * @param page the page which content will be tagged with this instance of {@code TagTreePointer}. * @return this {@link TagTreePointer} instance. */ public TagTreePointer setPageForTagging(PdfPage page) { if (page.isFlushed()) { throw new PdfException(PdfException.PageAlreadyFlushed); } this.currentPage = page; return this; }
private static PdfCanvas initPdfCanvasOrThrowIfPageIsFlushed(PdfPage page) { if (page.isFlushed()) { throw new PdfException(PdfException.CannotDrawElementsOnAlreadyFlushedPages); } return new PdfCanvas(page); }
private static PdfCanvas initPdfCanvasOrThrowIfPageIsFlushed(PdfPage page) { if (page.isFlushed()) { throw new PdfException(PdfException.CannotDrawElementsOnAlreadyFlushedPages); } return new PdfCanvas(page); }
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)); } }
/** * Removes the page at the specified position in this tree. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param pageNum the one-based index of the PdfPage to be removed * @return the page that was removed from the list */ public PdfPage removePage(int pageNum) { PdfPage pdfPage = getPage(pageNum); if (pdfPage.isFlushed()) { Logger logger = LoggerFactory.getLogger(PdfPage.class); logger.warn(LogMessageConstant.REMOVING_PAGE_HAS_ALREADY_BEEN_FLUSHED); } if (internalRemovePage(--pageNum)) { return pdfPage; } else { return null; } }
/** * This method removes all annotation entries from form fields associated with a given page. * * @param page to remove from. */ private void removeUnusedWidgetsFromFields(PdfPage page) { if (page.isFlushed()) { return; } List<PdfAnnotation> annots = page.getAnnotations(); for (PdfAnnotation annot : annots) { if (annot.getSubtype().equals(PdfName.Widget)) { ((PdfWidgetAnnotation) annot).releaseFormFieldFromWidgetAnnotation(); } } }
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 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; }
/** * 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); }
/** * Get the page number associated to the provided widget. * * @param widget PdfWidgetAnnotation from which to extract the page number * @return page number */ protected int getWidgetPageNumber(PdfWidgetAnnotation widget) { int pageNumber = 0; PdfDictionary pageDict = widget.getPdfObject().getAsDictionary(PdfName.P); if (pageDict != null) { pageNumber = document.getPageNumber(pageDict); } else { for (int i = 1; i <= document.getNumberOfPages(); i++) { PdfPage page = document.getPage(i); if (!page.isFlushed()) { if (page.containsAnnotation(widget)) { pageNumber = i; break; } } } } return pageNumber; }
/** * Get the page number associated to the provided widget. * * @param widget PdfWidgetAnnotation from which to extract the page number * @return page number */ protected int getWidgetPageNumber(PdfWidgetAnnotation widget) { int pageNumber = 0; PdfDictionary pageDict = widget.getPdfObject().getAsDictionary(PdfName.P); if (pageDict != null) { pageNumber = document.getPageNumber(pageDict); } else { for (int i = 1; i <= document.getNumberOfPages(); i++) { PdfPage page = document.getPage(i); if (!page.isFlushed()) { if (page.containsAnnotation(widget)) { pageNumber = i; break; } } } } return pageNumber; }
/** * 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); }
/** * Moves structure associated with specified page and insert it in a specified position in the document. * <br/><br/> * NOTE: Works only for document with not flushed pages. * * @param fromPage page which tag structure will be moved * @param insertBeforePage indicates before tags of which page tag structure will be moved to */ public void move(PdfPage fromPage, int insertBeforePage) { for (int i = 1; i <= getDocument().getNumberOfPages(); ++i) { if (getDocument().getPage(i).isFlushed()) { throw new PdfException(MessageFormatUtil.format(PdfException.CannotMovePagesInPartlyFlushedDocument, i)); } } StructureTreeCopier.move(getDocument(), fromPage, insertBeforePage); }
if (page.isFlushed()) { PdfIndirectReference pageRef = page.getPdfObject().getIndirectReference(); if (!pageToStructParentsInd.containsKey(pageRef)) {
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())); } }
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())); } }
/** * Gets a {@link PdfPage} on which annotation is placed. * * @return {@link PdfPage} on which annotation is placed or null if annotation is not placed yet. */ public PdfPage getPage() { PdfIndirectReference annotationIndirectReference; if (page == null && (annotationIndirectReference = getPdfObject().getIndirectReference()) != null) { PdfDocument doc = annotationIndirectReference.getDocument(); PdfDictionary pageDictionary = getPageObject(); if (pageDictionary != null) { page = doc.getPage(pageDictionary); } else { for (int i = 1; i <= doc.getNumberOfPages(); i++) { PdfPage docPage = doc.getPage(i); if (!docPage.isFlushed()) { for (PdfAnnotation annot : docPage.getAnnotations()) { if (annotationIndirectReference.equals(annot.getPdfObject().getIndirectReference())) { page = docPage; break; } } } } } } return page; }
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) { flushWaitingDrawingElements(); LayoutTaggingHelper taggingHelper = this.<LayoutTaggingHelper>getProperty(Property.TAGGING_HELPER); if (taggingHelper != null) { taggingHelper.releaseFinishedHints(); } AreaBreak areaBreak = overflowResult != null && overflowResult.getAreaBreak() != null ? overflowResult.getAreaBreak() : null; if (areaBreak != null && areaBreak.getType() == AreaBreakType.LAST_PAGE) { while (currentPageNumber < document.getPdfDocument().getNumberOfPages()) { moveToNextPage(); } } else { moveToNextPage(); } PageSize customPageSize = areaBreak != null ? areaBreak.getPageSize() : null; while (document.getPdfDocument().getNumberOfPages() >= currentPageNumber && document.getPdfDocument().getPage(currentPageNumber).isFlushed()) { currentPageNumber++; } PageSize lastPageSize = ensureDocumentHasNPages(currentPageNumber, customPageSize); if (lastPageSize == null) { lastPageSize = new PageSize(document.getPdfDocument().getPage(currentPageNumber).getTrimBox()); } return (currentArea = new RootLayoutArea(currentPageNumber, getCurrentPageEffectiveArea(lastPageSize))); }
protected LayoutArea updateCurrentArea(LayoutResult overflowResult) { flushWaitingDrawingElements(); LayoutTaggingHelper taggingHelper = this.<LayoutTaggingHelper>getProperty(Property.TAGGING_HELPER); if (taggingHelper != null) { taggingHelper.releaseFinishedHints(); } AreaBreak areaBreak = overflowResult != null && overflowResult.getAreaBreak() != null ? overflowResult.getAreaBreak() : null; if (areaBreak != null && areaBreak.getType() == AreaBreakType.LAST_PAGE) { while (currentPageNumber < document.getPdfDocument().getNumberOfPages()) { moveToNextPage(); } } else { moveToNextPage(); } PageSize customPageSize = areaBreak != null ? areaBreak.getPageSize() : null; while (document.getPdfDocument().getNumberOfPages() >= currentPageNumber && document.getPdfDocument().getPage(currentPageNumber).isFlushed()) { currentPageNumber++; } PageSize lastPageSize = ensureDocumentHasNPages(currentPageNumber, customPageSize); if (lastPageSize == null) { lastPageSize = new PageSize(document.getPdfDocument().getPage(currentPageNumber).getTrimBox()); } return (currentArea = new RootLayoutArea(currentPageNumber, getCurrentPageEffectiveArea(lastPageSize))); }
if (isFlushed()) { return;