/** * Rotates PageSize clockwise. */ public PageSize rotate() { return new PageSize(height, width); }
private Rectangle getCurrentPageEffectiveArea(PageSize pageSize) { float leftMargin = (float) getPropertyAsFloat(Property.MARGIN_LEFT); float bottomMargin = (float) getPropertyAsFloat(Property.MARGIN_BOTTOM); float topMargin = (float) getPropertyAsFloat(Property.MARGIN_TOP); float rightMargin = (float) getPropertyAsFloat(Property.MARGIN_RIGHT); return new Rectangle(pageSize.getLeft() + leftMargin, pageSize.getBottom() + bottomMargin, pageSize.getWidth() - leftMargin - rightMargin, pageSize.getHeight() - bottomMargin - topMargin); }
/** * Gets page size, considering page rotation. * * @return {@link Rectangle} that specify size of rotated page. */ public Rectangle getPageSizeWithRotation() { PageSize rect = new PageSize(getPageSize()); int rotation = getRotation(); while (rotation > 0) { rect = rect.rotate(); rotation -= 90; } return rect; }
@Override public Rectangle clone() { return new PageSize(this); } }
private Rectangle getCurrentPageEffectiveArea(PageSize pageSize) { float leftMargin = (float) getPropertyAsFloat(Property.MARGIN_LEFT); float bottomMargin = (float) getPropertyAsFloat(Property.MARGIN_BOTTOM); float topMargin = (float) getPropertyAsFloat(Property.MARGIN_TOP); float rightMargin = (float) getPropertyAsFloat(Property.MARGIN_RIGHT); return new Rectangle(pageSize.getLeft() + leftMargin, pageSize.getBottom() + bottomMargin, pageSize.getWidth() - leftMargin - rightMargin, pageSize.getHeight() - bottomMargin - topMargin); }
/** * 将图片合成为一个PDF * @param inputImage 图片,格式为图片格式 * @param outputPDF 输出文件 * @throws FileNotFoundException * @throws MalformedURLException */ public static void generatePDFFromImage(File[] inputImage,File outputPDF) throws FileNotFoundException, MalformedURLException { List<Image>images=new LinkedList<>(); for(File file:inputImage){ images.add(new Image(ImageDataFactory.create(file.getPath()))); } PdfDocument pdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath())); images.forEach(image -> pdfDoc.addNewPage(new PageSize(new Rectangle(image.getImageScaledWidth(), image.getImageScaledHeight())))); BackgroundEventHandler handler = new BackgroundEventHandler(images); pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler); pdfDoc.close(); }
/** * Returns the area that will actually be used to write on the page, given * the current margins. Does not have any side effects on the document. * * @param pageSize the size of the page to * @return a {@link Rectangle} with the required dimensions and origin point */ public Rectangle getPageEffectiveArea(PageSize pageSize) { float x = pageSize.getLeft() + getLeftMargin(); float y = pageSize.getBottom() + getBottomMargin(); float width = pageSize.getWidth() - getLeftMargin() - getRightMargin(); float height = pageSize.getHeight() - getBottomMargin() - getTopMargin(); return new Rectangle(x, y, width, height); }
images.forEach(image -> pdfDoc.addNewPage(new PageSize(new Rectangle(image.getImageScaledWidth(), image.getImageScaledHeight())))); BackgroundEventHandler handler = new BackgroundEventHandler(images); pdfDoc.addEventHandler(PdfDocumentEvent.END_PAGE, handler);
/** * Returns the area that will actually be used to write on the page, given * the current margins. Does not have any side effects on the document. * * @param pageSize the size of the page to * @return a {@link Rectangle} with the required dimensions and origin point */ public Rectangle getPageEffectiveArea(PageSize pageSize) { float x = pageSize.getLeft() + getLeftMargin(); float y = pageSize.getBottom() + getBottomMargin(); float width = pageSize.getWidth() - getLeftMargin() - getRightMargin(); float height = pageSize.getHeight() - getBottomMargin() - getTopMargin(); return new Rectangle(x, y, width, height); }
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))); }
pdfDocument.setDefaultPageSize(new PageSize(width, height)); PdfPage page = pdfDocument.addNewPage(); PdfCanvas pageCanvas = new PdfCanvas(page);