提示用户保存使用 pdfsharp 生成的 PDF 报告

Prompt user for saving PDF reports generated using pdfsharp

我目前正在使用 c# 在 wpf 中的一个项目中工作。我在使用 pdfsharp 生成报告时遇到问题。报告已成功生成,但我需要提示用户手动 select 他们需要存储(保存)生成的报告的路径。所以我只是使用 SaveFileDialog 但失败了。请帮我解决这个问题...!!!

您可以使用以下代码进行保存。

    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