Search code examples
c#wpfpdfsharp

Prompt user for saving PDF reports generated using pdfsharp


I'm currently working in a project in wpf with c#. I have a problem with report generation using pdfsharp. Report is successfully generated but i need to prompt the user to manually select the path where they need to store(save) the report generated. So i just used SaveFileDialog but failed. Please help me with this...!!!


Solution

  • You can use the below code for Save.

        PdfDocument document = new PdfDocument();
            document.Info.Title = "Created with PDFsharp";
            // Create an empty page
            PdfPage page = document.AddPage();
            // Get an XGraphics object for drawing
            XGraphics gfx = XGraphics.FromPdfPage(page);
            // Create a font
            XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
            // Draw the text
            gfx.DrawString("Hello, World!", font, XBrushes.Black,
            new XRect(0, 0, page.Width, page.Height),
            XStringFormats.Center);
            string filename = string.Empty;
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".pdf"; // Default file extension
            dlg.Filter = "PDF documents (.pdf)|*.pdf"; // Filter files by extension 
    
            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();
    
            // Process save file dialog box results 
            if (result == true)
            {
                // Save document 
                filename = dlg.FileName;
            }
            document.Save(filename);
    

    SaveFileDialog Class