Printing in C# WPF

Matthew Armshaw 41 Reputation points
2020-07-28T18:08:28.26+00:00

I am writing a program in C# WPF so I am hoping this is the correct forum. I am having difficulty with xps creation. Please refer to the following link:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.xps.packaging.xpsdocument.addfixeddocumentsequence?view=netcore-3.1#System_Windows_Xps_Packaging_XpsDocument_AddFixedDocumentSequence

The code following the comment "add content to the 1st document" calls the function AddDocumentContent, which does not exist as part of System and the article does not provide the code for this function. What I need to do is add either a URI or a FixedDocument refernce, but I do not know how. Can someone clarify this for me?

Thanks,

MattSA

Could not make link work, but https address is valid.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,783 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bram Pesik 1 Reputation point
    2020-07-28T18:32:50.763+00:00

    @Matthew Armshaw Did you try adding the using System.Windows.Xps.Packaging at the top of your class?

    0 comments No comments

  2. DaisyTian-1203 11,626 Reputation points
    2020-07-29T06:56:45.433+00:00

    Here is the details for AddDocumentContent which use the AddFixedPage method to obtain a fixed-page writer for adding pages to an XpsDocument.[Driver from IXpsFixedPageWriter Interface. By the way, I will also give you the example of GetPrintTicketFromPrinter which may give you some help.

    private void AddDocumentContent(IXpsFixedDocumentWriter fixedDocumentWriter)  
            {  
                // Collection of image and font resources used on the current page.  
                //   Key: "XpsImage", "XpsFont"  
                //   Value: List of XpsImage or XpsFont resources  
                Dictionary<string, List<XpsResource>> resources;  
              
                try  
                {  
                    // Add Page 1 to current document.  
                    IXpsFixedPageWriter fixedPageWriter =  
                        fixedDocumentWriter.AddFixedPage();  
              
                    // Add the resources for Page 1 and get the resource collection.  
                    resources = AddPageResources(fixedPageWriter);  
              
                    // Write page content for Page 1.  
                    WritePageContent(fixedPageWriter.XmlWriter,  
                        "Page 1 of " + fixedDocumentWriter.Uri.ToString(), resources);  
              
                    // Commit Page 1.  
                    fixedPageWriter.Commit();  
              
                    // Add Page 2 to current document.  
                    fixedPageWriter = fixedDocumentWriter.AddFixedPage();  
              
                    // Add the resources for Page 2 and get the resource collection.  
                    resources = AddPageResources(fixedPageWriter);  
              
                    // Write page content to Page 2.  
                    WritePageContent(fixedPageWriter.XmlWriter,  
                        "Page 2 of " + fixedDocumentWriter.Uri.ToString(), resources);  
              
                    // Commit Page 2.  
                    fixedPageWriter.Commit();  
                }  
                catch (XpsPackagingException xpsException)  
                {  
                    throw xpsException;  
                }  
            }// end:AddDocumentContent()  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.