diff --git a/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/Java2DRenderer.java b/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/Java2DRenderer.java index 673bec482..a754600e0 100644 --- a/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/Java2DRenderer.java +++ b/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/Java2DRenderer.java @@ -63,6 +63,8 @@ public class Java2DRenderer implements IJava2DRenderer { private static final int DEFAULT_DOTS_PER_PIXEL = 1; private static final int DEFAULT_DPI = 72; + + private final int _initialPageNo; /** * Subject to change. Not public API. Used exclusively by the Java2DRendererBuilder class. @@ -79,10 +81,11 @@ public Java2DRenderer( String replacementText, boolean testMode, FSPageProcessor pageProcessor, - Graphics2D layoutGraphics) { + Graphics2D layoutGraphics, + int initialPageNumber) { _pageProcessor = pageProcessor; - + _initialPageNo = initialPageNumber; _svgImpl = svgImpl; _outputDevice = new Java2DOutputDevice(layoutGraphics); @@ -276,11 +279,11 @@ private LayoutContext newLayoutContext() { return result; } - public void writePages(int initialPageNo) throws IOException { + public void writePages() throws IOException { List pages = _root.getLayer().getPages(); RenderingContext c = newRenderingContext(); - c.setInitialPageNo(initialPageNo); + c.setInitialPageNo(_initialPageNo); PageBox firstPage = pages.get(0); Rectangle2D firstPageSize = new Rectangle2D.Float(0, 0, @@ -290,6 +293,41 @@ public void writePages(int initialPageNo) throws IOException { writePageImages(pages, c, firstPageSize); } + public void writePage(int zeroBasedPageNumber) throws IOException { + List pages = _root.getLayer().getPages(); + + if (zeroBasedPageNumber >= pages.size()) { + throw new IndexOutOfBoundsException(); + } + + RenderingContext c = newRenderingContext(); + c.setInitialPageNo(_initialPageNo); + + PageBox page = pages.get(zeroBasedPageNumber); + + Rectangle2D pageSize = new Rectangle2D.Float(0, 0, + page.getWidth(c) / DEFAULT_DOTS_PER_PIXEL, + page.getHeight(c) / DEFAULT_DOTS_PER_PIXEL); + + _outputDevice.setRoot(_root); + + FSPage pg = _pageProcessor.createPage(zeroBasedPageNumber, (int) pageSize.getWidth(), (int) pageSize.getHeight()); + + _outputDevice.initializePage(pg.getGraphics()); + _root.getLayer().assignPagePaintingPositions(c, Layer.PAGED_MODE_PRINT); + + c.setPageCount(pages.size()); + c.setPage(zeroBasedPageNumber, page); + paintPage(c, page); + _pageProcessor.finishPage(pg); + + _outputDevice.finish(c, _root); + } + + public int getPageCount() { + return _root.getLayer().getPages().size(); + } + private void writePageImages(List pages, RenderingContext c, Rectangle2D firstPageSize) throws IOException { _outputDevice.setRoot(_root); diff --git a/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/api/Java2DRendererBuilder.java b/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/api/Java2DRendererBuilder.java index 68ba5263a..432bf2e37 100644 --- a/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/api/Java2DRendererBuilder.java +++ b/openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/api/Java2DRendererBuilder.java @@ -14,6 +14,7 @@ import java.awt.Graphics2D; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; @@ -47,6 +48,7 @@ public class Java2DRendererBuilder { private File _file; private boolean _testMode = false; private Graphics2D _layoutGraphics; + private int _initialPageNumber; public static enum TextDirection { RTL, LTR; } public static enum PageSizeUnits { MM, INCHES } @@ -327,6 +329,16 @@ public Java2DRendererBuilder useDefaultPageSize(float pageWidth, float pageHeigh this._isPageSizeInches = (units == PageSizeUnits.INCHES); return this; } + + /** + * Used to set an initial page number for use with page counters, etc. + * @param pageNumberInitial + * @return + */ + public Java2DRendererBuilder useInitialPageNumber(int pageNumberInitial) { + this._initialPageNumber = pageNumberInitial; + return this; + } /** * Output the document in paged format. The user can use the DefaultPageProcessor or use its source @@ -339,6 +351,32 @@ public Java2DRendererBuilder toPageProcessor(FSPageProcessor pageProcessor) { return this; } + /** + * useLayoutGraphics and toPageProcessor MUST have been called. + * Also a document MUST have been set with one of the with* methods. + * This will build the renderer and output each page of the document to the specified page + * processor. + * @throws Exception + */ + public void runPaged() throws Exception { + Java2DRenderer renderer = this.buildJava2DRenderer(); + renderer.layout(); + renderer.writePages(); + } + + /** + * useLayoutGraphics and toPageProcessor MUST have been called. + * Also a document MUST have been set with one of the with* methods. + * This will build the renderer and output the first page of the document to the specified page + * processor. + * @throws Exception + */ + public void runFirstPage() throws Exception { + Java2DRenderer renderer = this.buildJava2DRenderer(); + renderer.layout(); + renderer.writePage(0); + } + public Java2DRenderer buildJava2DRenderer() { UnicodeImplementation unicode = new UnicodeImplementation(_reorderer, _splitter, _lineBreaker, _unicodeToLowerTransformer, _unicodeToUpperTransformer, _unicodeToTitleTransformer, _textDirection, _charBreaker); @@ -347,7 +385,7 @@ public Java2DRenderer buildJava2DRenderer() { BaseDocument doc = new BaseDocument(_baseUri, _html, _document, _file, _uri); - return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics); + return new Java2DRenderer(doc, unicode, _httpStreamFactory, _resolver, _cache, _svgImpl, pageSize, _replacementText, _testMode, _pageProcessor, _layoutGraphics, _initialPageNumber); } public static abstract class Graphics2DPaintingReplacedElement extends EmptyReplacedElement {