Generating PDF in .NET using XSL-FO

匆匆过客 提交于 2020-01-10 01:23:07

问题


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?


回答1:


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?".

The primary non-commercial answer on the actual question is:

Apache.org Project's 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:

  • Ibex PDF Creator
  • AntennaHouse
  • RenderX

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.




回答2:


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.

I would recommend that you also use some good XML/XSL editor for testing XSL/XPath. XML Copy Editor is a good free open source one.




回答3:


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.




回答4:


http://fonet.codeplex.com/

FO.NET is your library




回答5:


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.

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.




回答6:


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.

You can install this libraries from one nuget package (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:

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.



来源:https://stackoverflow.com/questions/1099485/generating-pdf-in-net-using-xsl-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!