Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ExampleStamp.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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$ */
  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.StreamResult;
  28. import javax.xml.transform.stream.StreamSource;
  29. import org.xml.sax.SAXException;
  30. import org.apache.fop.apps.FOUserAgent;
  31. import org.apache.fop.apps.FopFactory;
  32. import org.apache.fop.apps.MimeConstants;
  33. import org.apache.fop.render.intermediate.IFDocumentHandler;
  34. import org.apache.fop.render.intermediate.IFException;
  35. import org.apache.fop.render.intermediate.IFParser;
  36. import org.apache.fop.render.intermediate.IFUtil;
  37. import embedding.ExampleObj2XML;
  38. import embedding.model.ProjectTeam;
  39. /**
  40. * Example for the intermediate format that demonstrates the stamping of a document with some
  41. * kind of watermark. The resulting document is then rendered to a PDF file.
  42. */
  43. public class ExampleStamp {
  44. // configure fopFactory as desired
  45. private final FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
  46. /**
  47. * Stamps an intermediate file and renders it to a PDF file.
  48. * @param iffile the intermediate file (area tree XML)
  49. * @param stampSheet the stylesheet that does the stamping
  50. * @param pdffile the target PDF file
  51. * @throws IOException In case of an I/O problem
  52. * @throws TransformerException In case of a XSL transformation problem
  53. * @throws SAXException In case of an XML-related problem
  54. * @throws IFException if there was an IF-related error while creating the output file
  55. */
  56. public void stampToPDF(File iffile, File stampSheet, File pdffile)
  57. throws IOException, TransformerException, SAXException, IFException {
  58. // Setup output
  59. OutputStream out = new java.io.FileOutputStream(pdffile);
  60. out = new java.io.BufferedOutputStream(out);
  61. try {
  62. //user agent
  63. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  64. //Setup target handler
  65. String mime = MimeConstants.MIME_PDF;
  66. IFDocumentHandler targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
  67. userAgent, mime);
  68. //Setup fonts
  69. IFUtil.setupFonts(targetHandler);
  70. targetHandler.setResult(new StreamResult(pdffile));
  71. IFParser parser = new IFParser();
  72. Source src = new StreamSource(iffile);
  73. Source xslt = new StreamSource(stampSheet);
  74. //Setup Transformer for XSLT processing
  75. TransformerFactory tFactory = TransformerFactory.newInstance();
  76. Transformer transformer = tFactory.newTransformer(xslt);
  77. //Send XSLT result to AreaTreeParser
  78. SAXResult res = new SAXResult(parser.getContentHandler(targetHandler, userAgent));
  79. //Start XSLT transformation and area tree parsing
  80. transformer.transform(src, res);
  81. } finally {
  82. out.close();
  83. }
  84. }
  85. /**
  86. * Main method.
  87. * @param args command-line arguments
  88. */
  89. public static void main(String[] args) {
  90. try {
  91. System.out.println("FOP ExampleConcat (for the Intermediate Format)\n");
  92. //Setup directories
  93. File baseDir = new File(".");
  94. File outDir = new File(baseDir, "out");
  95. outDir.mkdirs();
  96. //Setup output file
  97. File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
  98. File iffile = new File(outDir, "team.if.xml");
  99. File stampxsltfile = new File(baseDir, "xml/xslt/ifstamp.xsl");
  100. File pdffile = new File(outDir, "ResultIFStamped.pdf");
  101. System.out.println("Intermediate file : " + iffile.getCanonicalPath());
  102. System.out.println("Stamp XSLT: " + stampxsltfile.getCanonicalPath());
  103. System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
  104. System.out.println();
  105. ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
  106. //Create intermediate file
  107. ExampleConcat concatapp = new ExampleConcat();
  108. concatapp.convertToIntermediate(
  109. team1.getSourceForProjectTeam(),
  110. new StreamSource(xsltfile), iffile);
  111. //Stamp document and produce a PDF from the intermediate format
  112. ExampleStamp app = new ExampleStamp();
  113. app.stampToPDF(iffile, stampxsltfile, pdffile);
  114. System.out.println("Success!");
  115. } catch (Exception e) {
  116. e.printStackTrace(System.err);
  117. System.exit(-1);
  118. }
  119. }
  120. }