You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExampleStamp.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id: ExampleDOM2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */
  18. package embedding.intermediate;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.sax.SAXResult;
  27. import javax.xml.transform.stream.StreamSource;
  28. import org.apache.fop.apps.FOUserAgent;
  29. import org.apache.fop.apps.FopFactory;
  30. import org.apache.fop.apps.MimeConstants;
  31. import org.apache.fop.area.AreaTreeModel;
  32. import org.apache.fop.area.AreaTreeParser;
  33. import org.apache.fop.area.RenderPagesModel;
  34. import org.apache.fop.fonts.FontInfo;
  35. import org.xml.sax.SAXException;
  36. import embedding.ExampleObj2XML;
  37. import embedding.model.ProjectTeam;
  38. /**
  39. * Example for the intermediate format that demonstrates the stamping of a document with some
  40. * kind of watermark. The resulting document is then rendered to a PDF file.
  41. */
  42. public class ExampleStamp {
  43. // configure fopFactory as desired
  44. private FopFactory fopFactory = FopFactory.newInstance();
  45. /**
  46. * Stamps an intermediate file and renders it to a PDF file.
  47. * @param atfile the intermediate file (area tree XML)
  48. * @param stampSheet the stylesheet that does the stamping
  49. * @param pdffile the target PDF file
  50. * @throws IOException In case of an I/O problem
  51. * @throws TransformerException In case of a XSL transformation problem
  52. * @throws SAXException In case of an XML-related problem
  53. */
  54. public void stampToPDF(File atfile, File stampSheet, File pdffile)
  55. throws IOException, TransformerException, SAXException {
  56. // Setup output
  57. OutputStream out = new java.io.FileOutputStream(pdffile);
  58. out = new java.io.BufferedOutputStream(out);
  59. try {
  60. //Setup fonts and user agent
  61. FontInfo fontInfo = new FontInfo();
  62. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  63. //Construct the AreaTreeModel that will received the individual pages
  64. AreaTreeModel treeModel = new RenderPagesModel(userAgent,
  65. MimeConstants.MIME_PDF, fontInfo, out);
  66. //Iterate over all intermediate files
  67. AreaTreeParser parser = new AreaTreeParser();
  68. Source src = new StreamSource(atfile);
  69. Source xslt = new StreamSource(stampSheet);
  70. //Setup Transformer for XSLT processing
  71. TransformerFactory tFactory = TransformerFactory.newInstance();
  72. Transformer transformer = tFactory.newTransformer(xslt);
  73. //Send XSLT result to AreaTreeParser
  74. SAXResult res = new SAXResult(parser.getContentHandler(treeModel, userAgent));
  75. //Start XSLT transformation and area tree parsing
  76. transformer.transform(src, res);
  77. //Signal the end of the processing. The renderer can finalize the target document.
  78. treeModel.endDocument();
  79. } finally {
  80. out.close();
  81. }
  82. }
  83. /**
  84. * Main method.
  85. * @param args command-line arguments
  86. */
  87. public static void main(String[] args) {
  88. try {
  89. System.out.println("FOP ExampleConcat\n");
  90. //Setup directories
  91. File baseDir = new File(".");
  92. File outDir = new File(baseDir, "out");
  93. outDir.mkdirs();
  94. //Setup output file
  95. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  96. File atfile = new File(outDir, "team.at.xml");
  97. File stampxsltfile = new File(baseDir, "xml/xslt/atstamp.xsl");
  98. File pdffile = new File(outDir, "ResultStamped.pdf");
  99. System.out.println("Intermediate file : " + atfile.getCanonicalPath());
  100. System.out.println("Stamp XSLT: " + stampxsltfile.getCanonicalPath());
  101. System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
  102. System.out.println();
  103. ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
  104. //Create intermediate file
  105. ExampleConcat concatapp = new ExampleConcat();
  106. concatapp.convertToIntermediate(
  107. team1.getSourceForProjectTeam(),
  108. new StreamSource(xsltfile), atfile);
  109. //Stamp document and produce a PDF from the intermediate format
  110. ExampleStamp app = new ExampleStamp();
  111. app.stampToPDF(atfile, stampxsltfile, pdffile);
  112. System.out.println("Success!");
  113. } catch (Exception e) {
  114. e.printStackTrace(System.err);
  115. System.exit(-1);
  116. }
  117. }
  118. }