下面显示了用于将 XML 文档转换为 PDF 文档的 Java 应用程序。
FOP.java
package fop.pdf;
import org.apache.fop.apps.Driver;
import java.io.*;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.InputHandler;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.apps.FOPException;
import org.xml.sax.InputSource;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
public class FOP
{
public FOP()
{
}
public void xmlToFO(File xmlFile, File xsltFile, File foFile){
try {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));
Source src = new StreamSource(xmlFile);
Result result = new StreamResult(foFile);
transformer.transform(src, result);
} catch (Exception e){
catch(TransformerException e){}
}
public void foToPDF(File foFile, File pdfFile){
try {
Driver driver=new Driver();
Logger logger=new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
org.apache.fop.messaging.MessageHandler.setScreenLogger(logger);
driver.setRenderer(Driver.RENDER_PDF);
InputStream input=new FileInputStream(foFile);
driver.setInputSource(new InputSource(input));
OutputStream output=new FileOutputStream(pdfFile);
driver.setOutputStream(output);
driver.run();
output.flush();
output.close();
}catch(IOException e){}
catch(org.apache.fop.apps.FOPException e){}
}
public static void main(String[] args) {
File xmlFile=new File("C:/FOP/Catalog.xml");
File xsltFile=new File("C:/FOP/Catalog.xslt");
File pdfFile=new File("C:/FOP/Catalog.pdf");
File foFile=new File("C:/FOP/Catalog.fo");
FOP fop=new FOP();
fop.xmlToFO(xmlFile, xsltFile,foFile);
fop.foToPDF(foFile, pdfFile);
}
}Catalog.xml <?xml version="1.0" encoding="UTF-8" ?> <!--A Oracle Magazine Catalog--> <catalog title="Oracle Magazine" publisher="Oracle Publishing"> <magazine date="January-February 2005"> <article> <title>Refactoring for PL/SQL Developers</title> <author> Steven Feuerstein </author> </article> <article> <title>XQuery:A New Way to Search</title> <author> Caroline Kvitka </author> </article> </magazine> <magazine date="November-December 2004"> <article> <title>Database Resource Manager</title> <author>Kimberly Floss </author> </article> <article> <title>Editors' Choice Awards</title> <author> David Kelly </author> </article> </magazine> </catalog> 将该示例 XSLT 文档 Catalog.xslt(如下所示)复制到 C:/FOP 目录。 <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/> <!-- ========================= --> <!-- root element:catalog --> <!-- ========================= --> <xsl:template match="/catalog"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="simpleA4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="16pt" font-weight="bold" space-after="5mm"> Catalog:<xsl:value-of select="@title"/> </fo:block> <fo:block font-size="16pt" font-weight="bold" space-after="5mm"> Publisher:<xsl:value-of select="@publisher"/> </fo:block> <fo:block font-size="10pt"> <fo:table table-layout="fixed"> <fo:table-column column-width="4cm"/> <fo:table-column column-width="4cm"/> <fo:table-column column-width="5cm"/> <fo:table-header> <fo:table-row font-weight="bold"><fo:table-cell> <fo:block> <xsl:text>Date</xsl:text> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:text>Title</xsl:text> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:text>Author</xsl:text> </fo:block> </fo:table-cell> </fo:table-row> </fo:table-header> <fo:table-body> <xsl:apply-templates select="magazine"/> </fo:table-body> </fo:table> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="magazine"> <fo:table-row> <fo:table-cell> <fo:block> <xsl:value-of select="@date"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="article[1]/title"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="article[1]/author"/> </fo:block> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block> <xsl:value-of select="@date"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="article[2]/title"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block> <xsl:value-of select="article[2]/author"/> </fo:block> </fo:table-cell> </fo:table-row> </xsl:template> </xsl:stylesheet> <?xml version="1.0" encoding="UTF-8" ?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master margin-right="2cm" margin-left="2cm" margin-bottom="2cm" margin-top="2cm" page-width="21cm" page-height="29.7cm" master-name="simpleA4"> <fo:region-body/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="simpleA4"> <fo:flow flow-name="xsl-region-body"> <fo:block space-after="5mm" font-weight="bold" font-size="16pt">Catalog:Oracle Magazine</fo:block> <fo:block space-after="5mm" font-weight="bold" font-size="16pt">Publisher:Oracle Publishing</fo:block> <fo:block font-size="10pt"> <fo:table table-layout="fixed"> <fo:table-column column-width="4cm"/> <fo:table-column column-width="4cm"/> <fo:table-column column-width="5cm"/> <fo:table-header> <fo:table-row font-weight="bold"> <fo:table-cell> <fo:block>Date</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Title</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Author</fo:block> </fo:table-cell> </fo:table-row> </fo:table-header> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block>January-February 2005</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Refactoring for PL/SQL Developers</fo:block> </fo:table-cell> <fo:table-cell> <fo:block> Steven Feuerstein </fo:block> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block>January-February 2005</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>XQuery:A New Way to Search</fo:block> </fo:table-cell> <fo:table-cell> <fo:block> Caroline Kvitka </fo:block> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block>November-December 2004</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Database Resource Manager</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Kimberly Floss </fo:block> </fo:table-cell> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block>November-December 2004</fo:block> </fo:table-cell> <fo:table-cell> <fo:block>Editors' Choice Awards</fo:block> </fo:table-cell> <fo:table-cell> <fo:block> David Kelly </fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </fo:block> </fo:flow> </fo:page-sequence> </fo:root>这个FCKEditor怎么加附件啊