Add and manipulate print tickets

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

 

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 Java API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for the Java platform.
  • Add Aspose.Page for Java to your Maven project or download the latest package from the Aspose releases page.

Steps to create a custom print ticket Java.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. Create a custom job print ticket with the JobPrintTicket Constructor and assign it with setJobPrintTicket().
  4. Configure its input and output bins, page orientation, duplex mode, and output color settings.
  5. Save the changed XPS document using the XpsDocument.save() Method.

// Create custom print ticket in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/print-tickets/

String outputFileName = "CreateCustomPrintTicket.xps";

try (XpsDocument document = new XpsDocument()) {
    // Set a custom job-level print ticket
    document.setJobPrintTicket(new JobPrintTicket(
        // Specify input bin.
        new JobInputBin(InputBin.InputBinOption.Manual.clone().add(
            InputBin.FeedFace.FaceDown, InputBin.FeedDirection.LongEdgeFirst, new InputBin.MediaSheetCapacity(100))),
        // Specify output bin.
        new JobOutputBin(new OutputBin.OutputBinOption(OutputBin.BinType.Sorter),
            new OutputBin.OutputBinOption(OutputBin.BinType.Stacker, new OutputBin.MediaSheetCapacity(100))),
        // Specify page orientation.
        new PageOrientation(PageOrientation.PageOrientationOption.Landscape),
        // Specify duplex mode fof the output.
        new JobDuplexAllDocumentsContiguously(Duplex.DuplexOption.twoSidedLongEdge(Duplex.DuplexMode.Automatic)),
        // Specify the color settings for the output.
        new PageOutputColor(PageOutputColor.PageOutputColorOption.Grayscale(0, 8))));

    // Save the document with the custom job-level print ticket.
    document.save(getOutputDir() + outputFileName);
} catch (IOException ex) {
}

Steps to edit XPS print ticket via Java.

  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.

// Edit print ticket in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/print-tickets/

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

String outputFileName = "EditPrintTicket.xps";

JobPrintTicket pt = xDocs.getJobPrintTicket();

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

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

// Save the document with changed job print ticket.
xDocs.save(getOutputDir() + outputFileName);

Steps to get print ticket via Java.

  1. Set the path to the documents directory.
  2. Open XPS Document with print tickets using the XpsDocument Class.
  3. Get the job print ticket using the getJobPrintTicket() Method.
  4. Create the document print ticket using the getDocumentPrintTicket() Method.
  5. Get the page print ticket using the getPagePrintTicket() Method.
  6. Save the document so default print tickets are added, reopen it, and verify the job, document, and page print tickets.

// Edit print ticket in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/print-tickets/

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

String outputFileName = "GetPrintTicket.xps";

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

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

// Get 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(getOutputDir() + outputFileName);

// Open saved earlier XPS Document with print tickets
XpsDocument xDocs2 = new XpsDocument(getOutputDir() + outputFileName);

// Print tickets must not be null

System.out.println(xDocs2.getJobPrintTicket());

System.out.println(xDocs2.getDocumentPrintTicket(1));

System.out.println(xDocs2.getPagePrintTicket(1, 1));

Steps to link print tickets of XPS file via Java.

  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 using getJobPrintTicket() and setJobPrintTicket().
  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.

Package Manager Console Command

// Edit print ticket in XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/print-tickets/

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

String outputFileName = "LinkPrintTicket.xps";

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

// Link job print ticket
xDocs1.setJobPrintTicket(xDocs2.getJobPrintTicket());

// Link document print ticket
xDocs1.setDocumentPrintTicket(1, xDocs2.getDocumentPrintTicket(2));

// Link page print ticket
xDocs1.setPagePrintTicket(1, 1, xDocs2.getPagePrintTicket(3, 2));


// Save the document with linked print tickets.
xDocs1.save(getOutputDir() + outputFileName);

{{i18n.feature5.title}}

  • {{i18n.feature5.item1}}
  • {{i18n.feature5.item2}}
  • {{i18n.feature5.item3}}

FAQ

1. What is an XPS Print Ticket in Aspose.Page?

An XPS Print Ticket is an XML structure defined by the Print Schema specification. It embeds hardware print instructions (like orientation, duplex, copies, paper dimensions, and output trays) directly inside the .xps ZIP package file.

2. Can I modify individual XML parameters inside a Print Ticket dynamically?

Yes. You can retrieve the raw XML content via XpsPrintTicket.getXml(), manipulate the DOM nodes or attributes using standard Java XML parsers, and reassign the modified ticket back to the document or page.

3. What happens if both a Job-level and Page-level Print Ticket are present?

Page-level Print Tickets take priority over Job-level Print Tickets for that specific page, allowing developers to create mixed-media print jobs (e.g., cardstock cover pages mixed with standard plain text pages).