Add and manipulate print tickets

Create, edit, link and get print tickets of XPS files via C#

 

All the Page Description Language formats support printing. Some of them, like PDF, support high-quality printing with a variety of color spaces and resolution-independent environments. As XPS was intended to be printed on ordinary office printers it supports fewer color spaces and just 2 types of fonts. Aspose.Page API solution among the different other features allows working with print tickets. Here you will find information that explains how to create, edit, get and link them.

To manipulate print tickets of XPS files, we need:

  • Aspose.Page for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for the C# platform.

  • Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to create a custom print ticket C# .NET.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. Add a custom job print ticket using the JobPrintTicket Constructor.
  4. Add a custom page parameter initializer and a custom page resolution option to the ticket.
  5. Save the changed XPS document using the XPsDocument.Save() Method.

C# Code to make print tickets in an XPS file

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsMetadata;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    using System;
    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Create a new XPS document
    XpsDocument xDocs = new XpsDocument();

    // Add a custom job print ticket
    xDocs.JobPrintTicket = new JobPrintTicket(
        new PageDevModeSnaphot("SABlAGwAbABvACEAAAA="),             // Custom page parameter initializer
        new DocumentCollate(Collate.CollateOption.Collated),
        new JobCopiesAllDocuments(1),
        new PageICMRenderingIntent(PageICMRenderingIntent.PageICMRenderingIntentOption.Photographs),
        new PageColorManagement(PageColorManagement.PageColorManagementOption.None),
        new JobNUpAllDocumentsContiguously(
            new NUp.PresentationDirection(NUp.PresentationDirection.PresentationDirectionOption.RightBottom),
            new Borders(Borders.BordersOption.On) /* Custom nested feature */).AddPagesPerSheetOption(1),
        new PageMediaSize(PageMediaSize.PageMediaSizeOption.NorthAmericaLetter),
        new JobInputBin(InputBin.InputBinOption.AutoSelect),
        new JobDuplexAllDocumentsContiguously(Duplex.DuplexOption.OneSided),
        new PageOrientation(PageOrientation.PageOrientationOption.Portrait),
        new PageResolution(
            new PageResolution.PageResolutionOption("ns0000:ESDL300x300")             // Custom page resolution option
                .SetResolutionX(300).SetResolutionY(300)),
        new PageMediaType(PageMediaType.PageMediaTypeOption.Plain),
        new PageOutputColor(PageOutputColor.PageOutputColorOption.Color.Clone().SetDeviceBitsPerPixel(0).SetDriverBitsPerPixel(24)));


    // Save the document with the custom job print ticket.
    xDocs.Save(dir + "output1.xps");

Steps to edit XPS print ticket via C# .NET.

  1. Set the path to the documents directory.
  2. Open XPS Document with print tickets using the XpsDocument Class.
  3. To remove the not needed parameters from the ticket use the Remove() Method.
  4. Save the document with changed job print ticket by means of the XPsDocument.Save() Method.

C# Code to edit print tickets in an XPS file

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Open the XPS Document with print tickets
    XpsDocument xDocs = new XpsDocument(dir + "input3.xps");

    JobPrintTicket pt = xDocs.JobPrintTicket;

    // Remove some parameters from the job print ticket
    pt.Remove(
        "ns0000:PageDevmodeSnapshot",
        "ns0000:JobInterleaving",
        "ns0000:JobImageType");

    // Add some parameters to the job print ticket
    pt.Add(
        new JobCopiesAllDocuments(2),
        new PageMediaSize(PageMediaSize.PageMediaSizeOption.ISOA4));

    // Save the document with the changed job print ticket.
    xDocs.Save(dir + "output3.xps");

Steps to get print ticket via C# .NET.

  1. Set the path to the documents directory.
  2. Open XPS Document with print tickets using the XpsDocument Class.
  3. Create the job print ticket with the JobPrintTicket Constructor.
  4. Create the document print ticket using the GetDocumentPrintTicket() Method.
  5. Get the page print ticket using the GetPagePrintTicket() Method.
  6. Save the document with changed job print ticket by means of the XPsDocument.Save() Method.

C# Code to get print tickets in an XPS file

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Open the XPS Document without print tickets
    XpsDocument xDocs = new XpsDocument(dir + "input1.xps");

    // Get the job print ticket
    JobPrintTicket jobPrintTicket = xDocs.JobPrintTicket; // must be null for this document

    // Get the document print ticket
    DocumentPrintTicket docPrintTicket = xDocs.GetDocumentPrintTicket(1); // must be null for this document

    // Get the page print ticket
    PagePrintTicket pagePrintTicket = xDocs.GetPagePrintTicket(1, 1); // must be null for this document


    // Save the document. Default print tickets are automatically added to document while saving.
    xDocs.Save(dir + "output1.xps");

    // Open the saved earlier XPS Document with print tickets
    XpsDocument xDocs2 = new XpsDocument(dir + "output1.xps");

    // Print tickets must not be null

    Console.WriteLine(xDocs2.JobPrintTicket);

    Console.WriteLine(xDocs2.GetDocumentPrintTicket(1));

    Console.WriteLine(xDocs2.GetPagePrintTicket(1, 1));

Steps to link print tickets of XPS file via C# .NET.

  1. Set the path to the documents directory.
  2. Create a new XPS file and open XPS Document with print tickets using XpsDocument Class.
  3. Open XPS Document with print tickets using XpsDocument Class
  4. Link the job print ticket with the JobPrintTicket Constructor.
  5. Link the document print ticket using the GetDocumentPrintTicket() and SetDocumentPrintTicket() Methods
  6. Link the page print ticket using the GetPagePrintTicket() and SetPagePrintTicket() Methods.
  7. Save the document with changed job print ticket by means of XPsDocument.Save() Method.

C# Code to link print tickets in an XPS file

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithPrintTickets();

    // Create a new XPS document
    XpsDocument xDocs1 = new XpsDocument();

    // Open the XPS Document with print tickets
    XpsDocument xDocs2 = new XpsDocument(dir + "input2.xps");

    // Link the job print ticket
    xDocs1.JobPrintTicket = xDocs2.JobPrintTicket;

    // Link the document print ticket
    xDocs1.SetDocumentPrintTicket(1, xDocs2.GetDocumentPrintTicket(2));

    // Link the page print ticket
    xDocs1.SetPagePrintTicket(1, 1, xDocs2.GetPagePrintTicket(3, 2));


    // Save the document with linked print tickets.
    xDocs1.Save(dir + "output1.xps");



FAQ

1. How can I create a print ticket for an XPS file?

To make a print ticket (or printing information) to the document before sending it to the printer, use the JobPrintTicket Class.

2. What operations with print tickets are available within Aspose.Page API Solution?

With this .NET Solution you can create, edit, get, and link printing information.

3. How can I edit the printing information of the XPS file?

Set the path and open an XPS document with print tickets. Use Methods of the PrintTicket Class.

XPS What is XPS File Format

XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.