在 C# 中从 XML XSLT 创建 PDF 的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17589204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to create PDF from XML XSLT in C#
提问by Bravo11
I have a requirement to crate a PDF of XML Records. I think there is no way to directly create pdf from xml but using XSLT or XSL FO i believe it can be done. I have been reading lots of articles searching for the good way to do that using C#.
我需要创建一个 PDF 的 XML 记录。我认为没有办法从 xml 直接创建 pdf,但使用 XSLT 或 XSL FO 我相信可以做到。我一直在阅读很多文章,寻找使用 C# 做到这一点的好方法。
--> What's the best approach of during this? any example would really be great.
--> 在此期间最好的方法是什么?任何例子都会很棒。
My Scenario:
我的场景:
I have XML that looks like:
我有如下所示的 XML:
<Products>
<Brand name="Test">
<Quantity value="2/>
<Price value=""/>
</Brand>
<Brand name="Test2">
<Quantity value="3/>
<Price value=""/>
</Brand>
<Brand name="Test3">
<Quantity value="4/>
<Price value=""/>
</Brand>
</Products>
How can i create a pdf that will have a table showing all this information?
如何创建一个包含所有这些信息的表格的 pdf?
I know there are lots of similar questions like this but most of them are outdated. Any help really appreciated.
我知道有很多类似的问题,但大多数都过时了。任何帮助真的很感激。
采纳答案by Enrico Campidoglio
In the past I've used a commercial library called Ibex PDF Creatorto generate PDF documents from XML data using the XSL-FO standard that has worked really well.
过去,我使用了一个名为Ibex PDF Creator的商业库,使用非常有效的 XSL-FO 标准从 XML 数据生成 PDF 文档。
Here's an example of how I would use it:
这是我将如何使用它的示例:
XML data:
XML 数据:
<DocumentRoot>
<!-- Some content -->
</DocumentRoot>
XSL-FO layout:
XSL-FO 布局:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/DocumentRoot">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ibex="http://www.xmlpdf.com/2003/ibex/Format">
<ibex:properties
title="Some document"
subject=""
author=""
keywords=""
creator="" />
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm">
<fo:region-body margin-bottom="1cm" margin-top="3cm"/>
<fo:region-before extent="20mm"/>
<fo:region-after extent="8mm"/>
<fo:region-start extent="1mm"/>
<fo:region-end extent="1mm"/>
</fo:simple-page-master>
</fo:layout-master-set>
</<fo:root>
</xsl:template>
</xsl:stylesheet>
Generating the PDF document in .NET:
在 .NET 中生成 PDF 文档:
var data = new MemoryStream(dataBytes);
var layout = new MemoryStream(layoutBytes);
var pdf = new MemoryStream();
// Using the Ibex PDF Creator .NET API
var doc = new FODocument();
doc.generate(data, layout, pdf);
I hope this helps.
我希望这有帮助。
回答by Hudson Lane
You can create pdf file from xml by using Aspose.PDF for .NET API. I could find the code sample for transforming xml to pdf in C#/.NETon Aspose documentation page for pdf.
您可以使用 Aspose.PDF for .NET API 从 xml 创建 pdf 文件。我可以在pdf 的Aspose 文档页面上找到在 C#/.NET中将xml 转换为 pdf的代码示例。
回答by gman
I used the Apache Fop.bat in a method like this. (using System.Diagnostics)
我在这样的方法中使用了 Apache Fop.bat。(使用 System.Diagnostics)
private void XML_TO_PDF_VIA_FOP(String xmlName, String xsltName, String pdfName)
{
String batchFile = "XSLT\FOPv1\fop.bat";
String xmlFile = xmlName;
String xsltFile = "XSLT\" + xsltName;
String pdfFile = pdfName;
Process.Start(batchFile, " -xml " + xmlFile + " -xsl " + xsltFile + " -pdf " + pdfFile);
}

