We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to save a created PDF to new location?

I'm trying to save a Syncfusion.Pdf.PdfDocument ​to my computer. Everything in the " Open and save PDF file in C# "​ page (  https://help.syncfusion.com/file-formats/pdf/open-and-save-pdf-file-in-c-sharp-vb-net?cs-save-lang=1&cs-lang=csharp#opening-a-corrupted-pdf-document ), saves the file using Syncfusion.Pdf.PdfLoadedDocument​, but i have programmatically created my PDFDocument using FixedDocument/XPS. I have no loaded pdf document. I have never used "MemoryStream".



private void SaveDocument()
        {
            Syncfusion.XPS.XPSToPdfConverter converter = new Syncfusion.XPS.XPSToPdfConverter();


            string xpsPath = "c:\\Stuff\\MyTest";


            XpsDocument xps = new XpsDocument(xpsPath, FileAccess.Write, CompressionOption.Maximum);
            System.Windows.Xps.XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xps);
            writer.Write(fixedDoc);
            xps.Close();


            FileStream fStream = File.OpenRead(xpsPath);
            Syncfusion.Pdf.PdfDocument pdf = converter.Convert(fStream);
            fStream.Close();


            //Save the PDF Document to disk.
            pdf.Save(I CANNOT ENTER A PATH HERE!!!);
            pdf.Close();
        }

How do i change this code so that when i run this function a PDF file will be created on the given location that i saved too? Noob friendly reply please, assume i know nothing, thanks.

1 Reply

IJ Irfana Jaffer Sadhik Syncfusion Team February 1, 2023 11:49 AM UTC

Please refer to the code snippet below:

 //Create converter class

 XPSToPdfConverter converter = new XPSToPdfConverter();


string xpsPath = "../../../Data/source.xps";


            // An xps document is one or more FixedDocuments containing FixedPages

             FixedDocument fDoc = new FixedDocument();


            XpsDocument xps = new XpsDocument(xpsPath, FileAccess.Write, CompressionOption.Maximum);

            System.Windows.Xps.XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xps);


            writer.Write("source.xps");

            xps.Close();



            FileStream fStream = File.OpenRead(xpsPath);

            Syncfusion.Pdf.PdfDocument pdf = converter.Convert(fStream);

            fStream.Close();



            using (var doc = converter.Convert(fStream))

            {


                MemoryStream ms = new MemoryStream();

                doc.Save(ms); // write directly to the file


                File.WriteAllBytes("output.pdf", ms.ToArray());


                //Open the Pdf document

                System.Diagnostics.Process.Start("output.pdf");


                doc.Close(true);

            }



Try this in your end and let us know the result


Loader.
Up arrow icon