From 42efdd9b247d07833edbbd4796178677762d8910 Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Sun, 18 Nov 2007 10:48:53 +0000 Subject: [PATCH] ApacheCon US is over. OSSSummit was cancelled/postponed. Added example for total page count using XSL 1.1 Adjusted total page count example to new FOP API. (Thanks to Miroslav Gregan for the hint) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@596072 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/fo.xml | 72 ++++++++++++++++++-------- src/documentation/skinconf.xml | 14 ----- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/src/documentation/content/xdocs/fo.xml b/src/documentation/content/xdocs/fo.xml index 5ab95fe8d..dc7d45ab0 100644 --- a/src/documentation/content/xdocs/fo.xml +++ b/src/documentation/content/xdocs/fo.xml @@ -362,9 +362,14 @@ Omit your normal headers and footers, and use (for example) an extended header t
Total Document Pages

- It is frequently desirable to know the total number of pages in a document and to use that number within the document. -For example, you might wish to show the page number on the first page as being "page 1 of 12". -To accomplish this, place an empty block with an id at the end of the flow: + It is frequently desirable to know the total number of pages in a + document and to use that number within the document. For example, you + might wish to show the page number on the first page as being + "page 1 of 12". +

+

+ To accomplish this in XSL 1.0, place an empty block + with an id at the end of the flow:

... @@ -373,10 +378,20 @@ To accomplish this, place an empty block with an id at the end of the flow:

Get the number of the last page as follows:

- ]]> + ]]>

This does not work in certain situations: multiple page sequences, an initial page number other than 1, or forcing a certain page count, thereby producing blank pages at the end.

+

+ In XSL 1.1, you get another option to do this: make + sure an "id" is set on the page-sequence and reference it using + fo:page-number-citation-last. First, the page-sequence: +

+ +

+ After that, reference the last page the page-sequence generates: +

+ ]]> There is no reliable way to get the real total page count with FO mechanisms. You can only get page numbers. @@ -394,27 +409,42 @@ import javax.xml.transform.stream.*; class rendtest { + private static FopFactory fopFactory = FopFactory.newInstance(); + private static TransformerFactory tFactory = TransformerFactory.newInstance(); + public static void main(String args[]) { + OutputStream out; try { - Driver driver=new Driver(); - driver.setOutputStream(new FileOutputStream(args[2])); - driver.setRenderer(Driver.RENDER_PDF); - Transformer transformer=TransformerFactory.newInstance() - .newTransformer(new StreamSource(new File(args[1]))); - transformer.setParameter("page-count","#"); + //Load the stylesheet + Templates templates = tFactory.newTemplates( + new StreamSource(new File(args[1]))); + + //First run (to /dev/null) + out = new org.apache.commons.io.output.NullOutputStream(); + FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); + Transformer transformer = templates.newTransformer(); + transformer.setParameter("page-count", "#"); transformer.transform(new StreamSource(new File(args[0])), - new SAXResult(driver.getContentHandler())); - String pageCount=Integer.toString(driver.getResults().getPageCount()); - driver=new Driver(); - driver.setOutputStream(new FileOutputStream(args[2])); - driver.setRenderer(Driver.RENDER_PDF); - transformer=TransformerFactory.newInstance() - .newTransformer(new StreamSource(new File(args[1]))); - transformer.setParameter("page-count",pageCount); + new SAXResult(fop.getDefaultHandler())); + + //Get total page count + String pageCount = Integer.toString(driver.getResults().getPageCount()); + + //Second run (the real thing) + out = new java.io.FileOutputStream(args[2]); + out = new java.io.BufferedOutputStream(out); + try { + foUserAgent = fopFactory.newFOUserAgent(); + fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); + transformer = templates.newTransformer(); + transformer.setParameter("page-count", pageCount); transformer.transform(new StreamSource(new File(args[0])), - new SAXResult(driver.getContentHandler())); - } - catch( Exception e) { + new SAXResult(fop.getDefaultHandler())); + } finally { + out.close(); + } + } catch( Exception e) { e.printStackTrace(); } } diff --git a/src/documentation/skinconf.xml b/src/documentation/skinconf.xml index 1211d4de6..65a271a64 100644 --- a/src/documentation/skinconf.xml +++ b/src/documentation/skinconf.xml @@ -368,20 +368,6 @@ which will be used to configure the chosen Forrest skin. 88 31 - - ApacheCon US 2007 (Atlanta) - http://apachecon.com/2007/US/ - http://www.apache.org/ads/ApacheCon/2007-usa-125x125.png - 125 - 125 - - - OS Summit Asia 2007 - http://www.ossummit.com/ - http://www.ossummit.com/ads/ossummit_button_3.jpg - 125 - 125 - ApacheCon Europe 2008 http://eu.apachecon.com/ -- 2.39.5