浏览代码

Added an example for the intermediate format the demonstrates stamping a document using XSLT.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@369908 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-0_92-beta
Jeremias Maerki 18 年前
父节点
当前提交
9904a3e4e8

+ 137
- 0
examples/embedding/java/embedding/intermediate/ExampleStamp.java 查看文件

@@ -0,0 +1,137 @@
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* $Id: ExampleDOM2PDF.java 332791 2005-11-12 15:58:07Z jeremias $ */
package embedding.intermediate;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.area.AreaTreeModel;
import org.apache.fop.area.AreaTreeParser;
import org.apache.fop.area.RenderPagesModel;
import org.apache.fop.fonts.FontInfo;
import org.xml.sax.SAXException;

import embedding.ExampleObj2XML;
import embedding.model.ProjectTeam;

/**
* Example for the intermediate format that demonstrates the stamping of a document with some
* kind of watermark. The resulting document is then rendered to a PDF file.
*/
public class ExampleStamp {

/**
* Stamps an intermediate file and renders it to a PDF file.
* @param atfile the intermediate file (area tree XML)
* @param stampSheet the stylesheet that does the stamping
* @param pdffile the target PDF file
* @throws IOException In case of an I/O problem
* @throws TransformerException In case of a XSL transformation problem
* @throws SAXException In case of an XML-related problem
*/
public void stampToPDF(File atfile, File stampSheet, File pdffile)
throws IOException, TransformerException, SAXException {
// Setup output
OutputStream out = new java.io.FileOutputStream(pdffile);
out = new java.io.BufferedOutputStream(out);
try {
//Setup fonts and user agent
FontInfo fontInfo = new FontInfo();
FOUserAgent userAgent = new FOUserAgent();

//Construct the AreaTreeModel that will received the individual pages
AreaTreeModel treeModel = new RenderPagesModel(userAgent,
MimeConstants.MIME_PDF, fontInfo, out);
//Iterate over all intermediate files
AreaTreeParser parser = new AreaTreeParser();
Source src = new StreamSource(atfile);
Source xslt = new StreamSource(stampSheet);
//Setup Transformer for XSLT processing
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslt);
//Send XSLT result to AreaTreeParser
SAXResult res = new SAXResult(parser.getContentHandler(treeModel, userAgent));
//Start XSLT transformation and area tree parsing
transformer.transform(src, res);
//Signal the end of the processing. The renderer can finalize the target document.
treeModel.endDocument();
} finally {
out.close();
}
}
/**
* Main method.
* @param args command-line arguments
*/
public static void main(String[] args) {
try {
System.out.println("FOP ExampleConcat\n");
//Setup directories
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();
//Setup output file
File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
File atfile = new File(outDir, "team.at.xml");
File stampxsltfile = new File(baseDir, "xml/xslt/atstamp.xsl");
File pdffile = new File(outDir, "ResultStamped.pdf");
System.out.println("Intermediate file : " + atfile.getCanonicalPath());
System.out.println("Stamp XSLT: " + stampxsltfile.getCanonicalPath());
System.out.println("PDF Output File: " + pdffile.getCanonicalPath());
System.out.println();

ProjectTeam team1 = ExampleObj2XML.createSampleProjectTeam();
//Create intermediate file
ExampleConcat concatapp = new ExampleConcat();
concatapp.convertToIntermediate(
team1.getSourceForProjectTeam(),
new StreamSource(xsltfile), atfile);
//Stamp document and produce a PDF from the intermediate format
ExampleStamp app = new ExampleStamp();
app.stampToPDF(atfile, stampxsltfile, pdffile);
System.out.println("Success!");
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}

}

+ 57
- 0
examples/embedding/xml/xslt/atstamp.xsl 查看文件

@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2006 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- $Id$ -->
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
<!-- ========================= -->
<!-- stamping... -->
<!-- ========================= -->
<xsl:template match="flow">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<!-- Stamp a big "SPECIMEN" text over the whole page using an area tree fragment inserted at the right place... -->
<block ipd="595275" bpd="841889" is-viewport-area="true" left-position="0" top-position="0" ctm="[1.0 0.0 0.0 1.0 0.0 0.0]" positioning="fixed">
<block ipd="595275" bpd="841889" is-reference-area="true">
<block ipd="595275" bpd="841889">
<lineArea ipd="595275" bpd="841889">
<viewport ipd="595275" bpd="841889" offset="0" pos="0 0 595275 841889">
<foreignObject ipd="0" bpd="0" ns="http://www.w3.org/2000/svg">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<g transform="rotate(-50 50 50)">
<text x="50" y="60" style="font-size:20;fill:#dfdfdf;stroke:none;font-family:sans-serif" text-anchor="middle">SPECIMEN</text>
</g>
</svg>
</foreignObject>
</viewport>
</lineArea>
</block>
</block>
</block>
<xsl:apply-templates select="child::*"/>
</xsl:copy>
</xsl:template>
<!-- ========================= -->
<!-- identity transformation -->
<!-- ========================= -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

+ 1
- 1
examples/embedding/xml/xslt/projectteam2fo.xsl 查看文件

@@ -35,7 +35,7 @@
<fo:block font-size="12pt" space-after="5mm">Version <xsl:value-of select="$versionParam"/>
</fo:block>
<fo:block font-size="10pt">
<fo:table table-layout="fixed">
<fo:table table-layout="fixed" width="100%" border-collapse="separate">
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="5cm"/>

正在加载...
取消
保存