Generating PDF in .NET using XSL-FO

前端 未结 6 1626
说谎
说谎 2020-12-16 17:41

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?

相关标签:
6条回答
  • 2020-12-16 17:55

    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.

    0 讨论(0)
  • 2020-12-16 18:01

    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.

    0 讨论(0)
  • 2020-12-16 18:02

    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.

    0 讨论(0)
  • 2020-12-16 18:09

    http://fonet.codeplex.com/

    FO.NET is your library

    0 讨论(0)
  • 2020-12-16 18:12

    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.

    0 讨论(0)
  • 2020-12-16 18:12

    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.

    0 讨论(0)
提交回复
热议问题