Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/b8975d164ab4acf6374350075a6ae599 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/b8975d164ab4acf6374350075a6ae599 to your computer and use it in GitHub Desktop.
Add, change and remove PrintTickets in XPS document with Java

Working with Print Tickets in XPS Examples

These code snippets show how to manage print tickets in XPS documents using Aspose.Page for Java. Print tickets allow you to specify printing preferences and settings for XPS files.

Key Use Cases

  • How to get existing Print Tickets in XPS document
  • How to change existing Print Ticket in XPS document
  • How to add custom Print Ticket in XPS document
  • How to add link Print Ticket in one XPS document to the Print Ticket in another one

How to Run Examples

  1. Reference Aspose.Page for Java in your project.
  2. Copy the required code snippet into your project.
  3. Download a temporary license and set it up as described here or use a paid license.
  4. Run the example.

Restrictions

Evaluation mode limits the number of manipulated XPS pages to 4. Set up a valid temporary or paid license to unlock full functionality.

Related Documentation

Detailed information about working with Print Tickets in XPS documents can be found in Working with print tickets | .NET

Related Resources

Requirements

  • Java 8 or higher
  • Aspose.Page for Java library
Aspose.Page for Java – Add Print Tickets in XPS Examples
// 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) {
}
// 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);
// 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));
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment