xml转换成PDF的一些思路及方法请教

dwt_131 2007-04-26 08:51:56
用C#语言开发,希望能提一些原理或思路,以及开发过程中要用到的一些资源,有源码能提供的最好.这关系到小弟我的毕业问题,我将大力散分啊.
...全文
1336 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
落曳声痕 2010-04-14
  • 打赏
  • 举报
回复
用iText,Java版与C#版的都有,C#版的事iTextSharp.dll,它比较好用,无需建立过度文件,直接转换,可以看看它的API文档,容易使用
andy6277 2008-09-25
  • 打赏
  • 举报
回复
..
dwt_131 2007-04-26
  • 打赏
  • 举报
回复
没有人响应了?
.....
xwzheng 2007-04-26
  • 打赏
  • 举报
回复
added
forgot 2007-04-26
  • 打赏
  • 举报
回复
http://blog.donews.com/limodou/archive/2004/04/01/9917.aspx
dwt_131 2007-04-26
  • 打赏
  • 举报
回复
谢谢这位大哥提供的源码,不过这个好像是用JAVA实现的,里面的思路我会好好揣摩的.不知道有没有相关的用C#实现的呢?

继续学习中....
forgot 2007-04-26
  • 打赏
  • 举报
回复
http://www-128.ibm.com/developerworks/tw/views/xml/tutorials.jsp?cv_doc_id=18840
CathySun118 2007-04-26
  • 打赏
  • 举报
回复
其具体的XSL-FO文件格式的语法可以参照一些其他资料.

建立好了此文件之后,我们就可以用FOP提供的一些接口方便的进行转换了.

FOP提供了XML->FO,XML->PDF,FO-PDF,OBJ->FO,OBJ->PDF的转换接口.

我们这里以XML->PDF的为例,其余的可以参照FOP包里相应的DEMO.



public class ExampleXML2PDF {

public void convertXML2PDF(File xml, File xslt, File pdf)
throws IOException, FOPException, TransformerException {
Driver driver = new Driver();
Logger logger = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
driver.setLogger(logger);
MessageHandler.setScreenLogger(logger);

//Setup Renderer (output format)
driver.setRenderer(Driver.RENDER_PDF);

//Setup output
OutputStream out = new java.io.FileOutputStream(pdf);
try {
driver.setOutputStream(out);

//Setup XSLT
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(xslt));

//Setup input for XSLT transformation
Source src = new StreamSource(xml);

//Resulting SAX events (the generated FO) must be piped through to FOP
Result res = new SAXResult(driver.getContentHandler());

//Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
}


public static void main(String[] args) {
try {
System.out.println("FOP ExampleXML2PDF\n");
System.out.println("Preparing...");

//Setup directories
File baseDir = new File(".");
File outDir = new File(baseDir, "out");
outDir.mkdirs();

//Setup input and output files
File xmlfile = new File(baseDir, "test.xml");
File xsltfile = new File(baseDir, "test.xsl");
File pdffile = new File(outDir, "test.pdf");

System.out.println("Input: XML (" + xmlfile + ")");
System.out.println("Stylesheet: " + xsltfile);
System.out.println("Output: PDF (" + pdffile + ")");
System.out.println();
System.out.println("Transforming...");

ExampleXML2PDF app = new ExampleXML2PDF();
app.convertXML2PDF(xmlfile, xsltfile, pdffile);

System.out.println("Success!");
} catch (Exception e) {
System.err.println(ExceptionUtil.printStackTrace(e));
System.exit(-1);
}
}
}




这样我们就很轻易地实现了XML文档到PDF文档的转换.

如果这些用在webservice的servlet中,想从xml文件直接生成pdf传输给浏览者而并不生成的pdf文件,我们可以如

下实现:

public class FOPServlet extends HttpServlet {
public static final String FO_REQUEST_PARAM = "fo";
public static final String XML_REQUEST_PARAM = "xml";
public static final String XSL_REQUEST_PARAM = "xsl";

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
try {
String xmlParam =getServletContext().getRealPath("WEB-INF/doc/xml/test.xml");
String xslParam =getServletContext().getRealPath("WEB-INF/doc/xsl/test.xsl");

if ((xmlParam != null) && (xslParam != null)) {
XSLTInputHandler input =
new XSLTInputHandler(new File(xmlParam),
new File(xslParam));
renderXML(input, response);
} else {
PrintWriter out = response.getWriter();
out.println("<html><head><title>Error</title></head>\n"+
"<body><h1>FopServlet Error</h1><h3>No 'fo' "+
"request param given.</body></html>");
}
} catch (ServletException ex) {
throw ex;
}
catch (Exception ex) {
throw new ServletException(ex);
}
}
public void renderXML(XSLTInputHandler input,
HttpServletResponse response) throws ServletException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();

response.setContentType("application/pdf");

Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());

byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception ex) {
throw new ServletException(ex);
}
}

}

CathySun118 2007-04-26
  • 打赏
  • 举报
回复
我们建立XSL-FO文件 test.xsl 如下:


<?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: projectteam -->
<!-- ========================= -->
<xsl:template match="FeatureSRS">
<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="20pt" font-weight="bold" space-after="5mm" text-align="center">Cardiac Feature SRS
</fo:block>
<fo:block font-size="10pt">
<xsl:apply-templates/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!-- ========================= -->
<!-- child element: member -->
<!-- ========================= -->
<xsl:template name="introduction" match="introduction">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">1. Intruction</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.1 Objective</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="objective"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.2 Scope</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="scope"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.3. Responsibilities</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="responsibilities"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.4. References</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="references"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.5. Definitions, Acronyms, and Abbreviations</fo:block>
<fo:block font-size="10pt" font-weight="bold" space-after="5mm" margin-left="5mm">
<fo:table table-layout="fixed" border="2cm" background-color="#fff2d9" >
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="6cm"/>
<fo:table-body>
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:text>Term</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>Definition</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="DAA">
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:value-of select="term"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="definition"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template name="generalDescription" match="generalDescription">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">2. General Description</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.1. Feature Name</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="7mm">2.1.1. Feature Summary</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="9mm">
<xsl:value-of select="featureName/summary"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="7mm">2.1.2. Feature Breakdown</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="9mm">
<xsl:value-of select="featureName/breakdown"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.2. Feature Requirements</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:for-each select="requirement">
<xsl:value-of select="content"/>
</xsl:for-each>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.3. Feature Interactions</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="featureInteractions"/>
</fo:block>
</xsl:template>
<xsl:template name="strResources" match="strResources">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">3. String Resources </fo:block>
<fo:block font-size="10pt" font-weight="bold" space-after="5mm" margin-left="5mm">
<fo:table table-layout="fixed" border="2cm" background-color="#fff2d9" >
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="10cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-body>
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:text>English String</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>Resource ID</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>Rqmt</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="strResource">
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:value-of select="estring"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="resourceid"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="rqmt"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
</xsl:stylesheet>

CathySun118 2007-04-26
  • 打赏
  • 举报
回复
二. 从XML到PDF
对于一个具有一定模板性质的XML文件,我们可以用FOP API来实现其到PDF的转换.

FOP需要fop.jar. 我们可以到http://xml.apache.org/fop/ 上获取和了解其用法.

以一个一般复杂的XML文件为例:

要转换XML文档 test.xml 如下:


<FeatureSRS title="SRS">
<introduction>
<objective>objective here</objective>
<scope>scope here</scope>
<responsibilities>responsibilities here</responsibilities>
<references>reference here</references>
<DAA>
<term>
term here
</term>
<definition>
definition here
</definition>
</DAA>
</introduction>
<generalDescription>
<featureName>
<summary>summary here</summary>
<breakdown>breakdown here</breakdown>
</featureName>
<requirement>
<content>
content here.
</content>
</requirement>
<requirement>
<content>
content2 here.
</content>
</requirement>
<featureInteractions>featureInteractions here</featureInteractions>
</generalDescription>
<strResources>
<strResource>
<estring>
estring here
</estring>
<resourceid>
resourceid here
</resourceid>
<rqmt>
rqmt here.
</rqmt>
</strResource>
</strResources>
</FeatureSRS>


对于这样一个XML文档,我们要将其转化成PDF格式必须建立一个XSL-FO文件,来定义对各element和value格

式的转换.
  • 打赏
  • 举报
回复
xml就是数据源了.弄个pdf报表.看你用什么报表了.其他的没用过,只熟悉activereport
dwt_131 2007-04-26
  • 打赏
  • 举报
回复
最终肯定是要打印出来的,不过现在首先第一步是要把原理以及大概的思路弄清楚.
  • 打赏
  • 举报
回复
不知道你到底要做什么?打印出来?
我就会activereport
dwt_131 2007-04-26
  • 打赏
  • 举报
回复
先谢谢回复,没有让我白等.

xml转换为WORD,然后再将WORD转换为PDF?
那第一步怎么转换呢?
请教了....

后面的那个网址我会去看的.

继续等待中......
mapserver 2007-04-26
  • 打赏
  • 举报
回复
看这个
http://sourceforge.net/projects/sharppdf/
hertcloud 2007-04-26
  • 打赏
  • 举报
回复
没具体做 不过 思路上
应该是 将xml 导入 为 word
然后 将word转换为 pdf


Word转换PDF格式
http://blog.csdn.net/lybid2002/articles/491441.aspx
olanty 2007-04-26
  • 打赏
  • 举报
回复
用Ghostscript好像可以,具体没做过,跟你一起问。

62,075

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧