Convert XPS to PDF using PDFsharp or iTextsharp

Learner picture Learner · Sep 22, 2015 · Viewed 7.4k times · Source

I am trying to convert xps to pdf using PDFsharp. I have gone through

this & this

But not able to find steps to convert XPS to PDF. Can anybody suggest me the informative link to convert xps to pdf?

I have downloaded the source of PDFSharp from here

But what should be next step? Please help me. Thanks.

P.S. I have visited all the links related to converting xps to pdf here but as I have low reputation I am not able to post more than 2 links.

UPDATE : Finally I figured it out.

I followed this.

  1. download the zips and add dll in your project.
  2. use following code

      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);
      PdfSharp.Xps.XpsConverter.Convert(pdfXpsDoc, FileName, 0);
    

    where dp should be your wpf control.

THEN

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

DONE :)

Answer

Orace picture Orace · Jun 12, 2020

Based on this question and Nathan Jones work there is now a NuGet package available to do the trick.