locked
WPF: How to export WPF control/page to PDF file? RRS feed

  • Question

  • Our WPF application needs to export WPF UserControl into a PDF format file.

    What we learn is export WPF control into XPS file format first and then using

    var dialog = new SaveFileDialog();
    
                    dialog.AddExtension = true;
                    dialog.DefaultExt = "pdf";
                    dialog.Filter = "PDF Document (*.pdf)|*.pdf";
                    dialog.FileName = "xxxReport_" + Utility.FormatDate(DateTime.Now).Replace("/", "_");
    
                    if (dialog.ShowDialog() == false)
                        return;
    
                    FixedDocument fixedDoc = new FixedDocument();
                    PageContent pageContent = new PageContent();
                    FixedPage fixedPage = new FixedPage();
    
                    PrintDialog printDlg = new PrintDialog();
                    Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight-100);
                    _reportBorder.Measure(pageSize);
                    _reportBorder.Arrange(new Rect(10, 50, pageSize.Width, pageSize.Height));
    
                    //Create first page of document
                    fixedPage.Children.Add(_reportBorder);
                    ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
                    fixedDoc.Pages.Add(pageContent);
    
    // write to PDF file
    string tempFilename = "temp.xps";
                File.Delete(tempFilename);
                XpsDocument xpsDoc = new XpsDocument(tempFilename, FileAccess.Write);
                XpsDocumentWriter xWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                xWriter.Write(fixedDoc.DocumentPaginator);
                xpsDoc.Close();
    
                PdfSharp.Xps.XpsConverter.Convert(tempFilename, path, 0);
                    

    we use the library PdfSharp from the link: https://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/

    However, we notice that the PdfSharp.Xps.XpsConverter.Convert() from Xps to PDF, the images inside wpf control lost.

    Does anyone use PdfSharp library without losing image controls?

    Does anyone has idea how to export WPF control which includes images into PDF format? Thx!


    JaneC


    • Edited by JJChen Monday, March 21, 2016 5:12 PM
    Saturday, March 19, 2016 12:58 AM

Answers

All replies

  • Make sure that the source of the Image element is set to a valid Uri. The following code prints the image inside the Border to the XPS document as expected:

    private void Save()
            {
                Border _reportBorder = new Border() { BorderBrush = Brushes.Red, BorderThickness = new Thickness(2) };
                _reportBorder.Width = 400;
                _reportBorder.Height = 400;
                Image img = new Image() { Stretch = Stretch.None };
                BitmapImage source = new BitmapImage();
                source.BeginInit();
                source.UriSource = new Uri("pack://application:,,,/WpfApplication5;component/av1.jpg");
                source.EndInit();
                img.Source = source;
                _reportBorder.Child = img;
    
                FixedDocument fixedDoc = new FixedDocument();
                PageContent pageContent = new PageContent();
                FixedPage fixedPage = new FixedPage();
    
                //CalibrationReportTemplate cr = new CalibrationReportTemplate();
                //cr.DataContext = this;
                PrintDialog printDlg = new PrintDialog();
                Size pageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight - 100);
                _reportBorder.Measure(pageSize);
                _reportBorder.Arrange(new Rect(10, 50, pageSize.Width, pageSize.Height));
    
    
                //Create first page of document
                fixedPage.Children.Add(_reportBorder);
                ((System.Windows.Markup.IAddChild)pageContent).AddChild(fixedPage);
                fixedDoc.Pages.Add(pageContent);
    
                // write to PDF file
                string tempFilename = "temp.xps";
                File.Delete(tempFilename);
                XpsDocument xpsDoc = new XpsDocument(tempFilename, FileAccess.Write);
                XpsDocumentWriter xWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                xWriter.Write(fixedDoc.DocumentPaginator);
                xpsDoc.Close();
            }
    

    If you have any questions or issues regarding PdfSharp, i.e. the conversion of the XPS document to PDF, you should ask in the following forum: http://forum.pdfsharp.net/

    Please don't ask questions about third-party software in these forums as they are intended for Microsoft's own products and technologies only.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Saturday, March 19, 2016 11:03 AM
  • Hi Magnus,

    Thanks for replying our question!

    Yes, after exporting WPF control into XPS file, the image is still existed in the XPS file.

    However, after converting from XPF format into PDF format, the image is lost.

    We have post the question on the link: https://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/

    However, we have not get any response yet.

    That is the reason we ask question in this forum, how developers normally handle to export WPF control into PDF when WPF includes images? 


    JaneC


    • Edited by JJChen Monday, March 21, 2016 4:56 PM
    Monday, March 21, 2016 4:53 PM
  • >>However, we have not get any response yet.

    Well, it is definetely definitely an issue with the PdfSharp component then, and as mentioned any such issues should be asked to the manufacturer of this component. Start a thread here: http://forum.pdfsharp.net/.

    >>how developers normally handle to export WPF control into PDF when WPF includes images?

    There is no built-in method to convert to PDF so the most common approach is to use some third-party library. You should however not ask questions about these here since the MSDN forums are intended for Microsoft's products and technologies only.

    Please remember to close this threads by marking helpful posts as answer and then start a new thread in an appropriate forum if you have a new question. Please don't ask several questions in the same thread.

    Monday, March 21, 2016 9:05 PM
  • Hi JJChen,

    If the remain problem is to convert XPS file to PDF format without image missing, this sample may

    helps: How to convert PDF to XPS and how to convert XPS to PDF in C#

    PdfDocument doc = new PdfDocument();
    doc.LoadFromFile(xpsFile,FileFormat.XPS);
    doc.SaveToFile(pdfFile, FileFormat.PDF);


    • Edited by Shawn Sh Wednesday, March 23, 2016 2:49 AM
    • Proposed as answer by Andy ONeill Tuesday, March 29, 2016 8:17 AM
    • Unproposed as answer by Andy ONeill Thursday, March 31, 2016 9:38 AM
    Wednesday, March 23, 2016 2:42 AM
  • Hi Shawn,

    Thanks for providing the solution for us!

    We check the Free Spire.PDF for .NET home site as following link: http://www.e-iceblue.com/Introduce/free-pdf-component.html#.VvnY26b2YSk

    The free version of Spire.PDF has limitation to 10 pages of PDF.

    Free version is limited to 10 pages of PDF. This limitation is enforced during loading and creating files.  When converting PDF to Image, the first 3 pages of PDF files will be converted to Image format successfully.


    JaneC

    Tuesday, March 29, 2016 1:29 AM
  • We found the solution PdfSharp site in following link: http://forum.pdfsharp.net/viewtopic.php?f=2&t=2144&p=6195&hilit=convert+image+from+xps+to+pdf#p6195

    After changing following two lines code, convert from XPS to PDF will keep the image.

    XpsParser.ImageBrush.cs (ln 22, added): brush.Opacity = 1;

    PdfContentWriter.cs (ln 526, changed): if (opacity <= 1)

    thanks for Magnus for pointing to correct website (http://forum.pdfsharp.net/) and we register as a user and search their website and found out the answer.

    Cheers!


    JaneC

    • Marked as answer by Andy ONeill Thursday, March 31, 2016 9:38 AM
    Wednesday, March 30, 2016 7:39 PM