Search code examples
c#pdfxps

XPS to PDF conversion


I am working on a code which will help conversion of XPS to PDF. Does the NuGet library have a free library which can help me achieve the above or if I can write my own C# code for the conversion. Not sure where to start with.

I have been searching for free libraries, came across iTextSharp not sure if it supports XPS to PDF conversion because I don't see a word about XPS in the NuGet gallery description https://www.nuget.org/packages/iTextSharp/5.5.13.1


Solution

  • PDFsharp seems to be what you want. See WPF to XPS to PDF.

    Also see https://nathanpjones.com/2013/03/output-to-pdf-in-wpf-for-free/:

    using System.IO;
    using System.IO.Packaging;
    using System.Windows.Xps.Packaging;
    using System.Windows.Xps;
    MemoryStream lMemoryStream = new MemoryStream();
    Package package = Package.Open(lMemoryStream, FileMode.Create);
    XpsDocument doc = new XpsDocument(package);
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
    writer.Write(dp);
    doc.Close();
    package.Close();
    var pdfXpsDoc = PdfSharp.Xps.XpsModel.XpsDocument.Open(lMemoryStream);
    

    Then

    PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, d.FileName, 0);
    

    Or for an XPS file,

    PdfSharp.Xps.XpsConverter.Convert(sourceXpsFile, destPdfFile, 0);