C# 使用 XSL-FO 在 .NET 中生成 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1099485/
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
Generating PDF in .NET using XSL-FO
提问by Eldila
I need to generate a pdf in .NET using XSL-FO. There are no shortage of libraries to do this. What library would you suggest that I use and why?
我需要使用 XSL-FO 在 .NET 中生成 pdf。不乏图书馆可以做到这一点。你建议我使用什么库,为什么?
回答by Eldila
Well i used iText(open source) a couple of times about 2 years ago, but i would not recommend this. The main reason is the lack of documentation and i really dislike the API it just feels outdated setting global variables before calling functions.
好吧,我大约 2 年前使用过 iText(开源)几次,但我不推荐这样做。主要原因是缺乏文档,我真的不喜欢 API,它只是感觉在调用函数之前设置全局变量已经过时了。
What is the reason you need to use XSL-FO? I also used ABC-pdf(commercial) which allows the user to turn HTML pages ( including stylesheets ) to pdf documents, or you can use a simple and documented API.
您需要使用 XSL-FO 的原因是什么?我还使用了 ABC-pdf(commercial),它允许用户将 HTML 页面(包括样式表)转换为 pdf 文档,或者您可以使用简单的文档化 API。
回答by Magnus Johansson
I have in the past used the Ibex PDF generator. I was thrown in in a project that already had a license for that, so I had really no other choice. At first I thought it was cumboersome, but eventually I got used to how it works.
我过去曾使用过Ibex PDF 生成器。我被投入到一个已经获得许可证的项目中,所以我真的别无选择。起初我认为它很麻烦,但最终我习惯了它的工作原理。
I would recommend that you also use some good XML/XSL editor for testing XSL/XPath. XML Copy Editoris a good free open source one.
我建议您还使用一些好的 XML/XSL 编辑器来测试 XSL/XPath。 XML Copy Editor是一款不错的免费开源软件。
回答by Ben Griswold
I researched options a couple of years back. Here's the shortlist: iTextSharp, SharpPDF, Report.NET, NFop and Siberix Report Writer. Ultimately I went with Siberix but I now think iTextSharp would have been the better choice. If it is helpful, you can find out a bit more here.
几年前我研究了选项。这是候选名单:iTextSharp、SharpPDF、Report.NET、NFop 和 Siberix Report Writer。最终我选择了 Siberix,但我现在认为 iTextSharp 会是更好的选择。如果有帮助,您可以在此处了解更多信息。
回答by X181
回答by Jay Stevens
This is an old question, I know, but none of the answers to date actually addressed the OP QUESTION. She asked for options for using XSL-FO, not "how to generate PDF using code?".
我知道这是一个老问题,但迄今为止没有一个答案真正解决了 OP 问题。她询问使用 XSL-FO 的选项,而不是“如何使用代码生成 PDF?”。
The primary non-commercial answer on the actual question is:
对实际问题的主要非商业性回答是:
Apache.org Project's FOP
Apache.org 项目的 FOP
Formatting Objects Processor. It takes XSL-FO compliant xsl code and xml and generates PDF files according to the XSL-FO spec. There are several other commercial options:
格式化对象处理器。它采用 XSL-FO 兼容的 xsl 代码和 xml,并根据 XSL-FO 规范生成 PDF 文件。还有其他几种商业选择:
- Ibex PDF Creator
- AntennaHouse
- RenderX
- Ibex PDF 生成器
- 天线房
- 渲染X
If you're looking for an open-source option that WORKS, FOP is it. The FOP is a Java library natively, but it has been ported to .NET via J# (That has it's own issues there I know) in the nFOP project.
如果您正在寻找一个有效的开源选项,FOP 就是它。FOP 本身是一个 Java 库,但它已通过nFOP 项目中的 J#(我知道那里有它自己的问题)移植到 .NET 。
回答by hal
If you can use something different, you can try to use PdfSharp + MigraDoc.DocumentObjectModel libraries for .NET (see http://www.pdfsharp.net/). Both have MIT license and free to use in commercial projects.
如果您可以使用不同的东西,您可以尝试为 .NET 使用 PdfSharp + MigraDoc.DocumentObjectModel 库(请参阅http://www.pdfsharp.net/)。两者都有 MIT 许可证,可免费用于商业项目。
You can install this libraries from one nuget package (https://www.nuget.org/packages/PDFsharp-MigraDoc/).
您可以从一个 nuget 包 ( https://www.nuget.org/packages/PDFsharp-MigraDoc/)安装此库。
Then, you can define your PDF DOM with MigraDoc.DocumentObjectModel.Document
class and print it with PdfDocumentRenderer
class. Here is sample code:
然后,您可以使用MigraDoc.DocumentObjectModel.Document
class定义您的 PDF DOM 并使用class 打印它PdfDocumentRenderer
。这是示例代码:
using System;
using System.IO;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
namespace MigraDocTest
{
class Program
{
static void Main(string[] args)
{
var document = new Document();
// initial page setup and declare the styles
document.DefaultPageSetup.Orientation = Orientation.Landscape;
var mainStyle = document.Styles[StyleNames.Normal];
mainStyle.Font.Name = "Arial";
const string TitleStyleName = "Title";
var titleStyle = document.Styles.AddStyle(TitleStyleName, StyleNames.Normal);
titleStyle.ParagraphFormat.Font.Size = 12;
titleStyle.ParagraphFormat.SpaceAfter = "0";
titleStyle.Font.Bold = true;
// main page section setup
var section = document.AddSection();
section.PageSetup.PageFormat = PageFormat.A4;
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.LeftMargin = "15mm";
section.PageSetup.RightMargin = "15mm";
section.PageSetup.TopMargin = "12mm";
section.PageSetup.BottomMargin = "20mm";
// add content to DOM here
section.AddParagraph("Hello, world!").Style = TitleStyleName;
// actually print document to PDF
var pdfRenderer = new PdfDocumentRenderer(unicode: true) { Document = document };
pdfRenderer.RenderDocument();
using (var stream = new MemoryStream())
{
pdfRenderer.Save(stream, closeStream: false);
File.WriteAllBytes(@"_output.pdf", stream.ToArray());
}
}
}
}
Note, that MigraDoc nuget supports .NET Framework 2+, .NETStandard is not supported. So, you need targeting to full .NET Framework to use this libraries.
请注意,MigraDoc nuget 支持 .NET Framework 2+,不支持 .NETStandard。因此,您需要面向完整的 .NET Framework 才能使用此库。