Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save aspose-com-gists/45d371019485aee5021806b705269c75 to your computer and use it in GitHub Desktop.
Create XPS document from scratch and manipulate pages with Java

Working with XPS Document Examples

These code snippets show how to create, modify, and manage XPS documents using Aspose.Page for Java. The library enables programmatic generation, page manipulation, and content editing for XPS files.

Key Use Cases

  • How to create XPS documents from scratch
  • How to add pages to XPS document
  • How to change XPS page
  • How to manipulate XPS pages
  • Modify XPS page on event of saving document
  • Cross-package operations

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 elements to 4. Apply a valid license to unlock full functionality.

Related Documentation

Additional information about creating XPS documents and manipulating XPS pages can be found on Working with Pages in XPS file | Java and Working with Pages in XPS file | Java pages of public documentation.

Related Resources

Requirements

  • Java 8 or higher
  • Aspose.Page for Java library
Aspose.Page for Java – Create XPS document Examples
// Shows possibility of clonning graphics elements in one document and adding them to another one.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
String outputFileName1 = "AddGlyphCloneAndChangeColor1_out.xps";
String outputFileName2 = "AddGlyphCloneAndChangeColor2_out.xps";
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument();
// Add glyphs to the first document
XpsGlyphs glyphs = doc1.addGlyphs("Times New Roman", 200, XpsFontStyle.Bold, 50, 250, "Test");
// Fill glyphs in the first document with one color
glyphs.setFill(doc1.createSolidColorBrush(Color.GREEN));
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument();
// Add glyphs cloned from the one's from the first document
glyphs = doc2.add(glyphs.deepClone());
// Fill glyphs in the second document with another color
((XpsSolidColorBrush)glyphs.getFill()).setColor(doc2.createColor(Color.RED));
// Save the first XPS document
doc1.save(getOutputDir() + outputFileName1);
// Save the second XPS document
doc2.save(getOutputDir() + outputFileName2);
// Shows possibility of using raster fill used in one document for painting glyphs in another one.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
String outputFileName1 = "AddImageFilledGlyphAndForeignImage1_out.xps";
String outputFileName2 = "AddImageFilledGlyphAndForeignImage2_out.xps";
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument();
// Add glyphs to the first document
XpsGlyphs glyphs1 = doc1.addGlyphs("Times New Roman", 200, XpsFontStyle.Bold, 50, 250, "Test");
// Fill the glyphs with an image brush
glyphs1.setFill(doc1.createImageBrush(getDataDir() + "R08SY_NN.tif", new Rectangle(0, 0, 128, 192),
new Rectangle(0, 0, 64, 96)));
((XpsImageBrush)glyphs1.getFill()).setTileMode(XpsTileMode.Tile);
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument();
// Add glyphs with the font from the first document to the second document
XpsGlyphs glyphs2 = doc2.addGlyphs(glyphs1.getFont(), 200, 50, 250, "Test");
// Create an image brush from the fill of the the first document and fill glyphs in the second document
glyphs2.setFill(doc2.createImageBrush(((XpsImageBrush)glyphs1.getFill()).getImage(), new Rectangle(0, 0, 128, 192),
new Rectangle(0, 0, 128, 192)));
((XpsImageBrush)glyphs2.getFill()).setTileMode(XpsTileMode.Tile);
// Save the first XPS document
doc1.save(getOutputDir() + outputFileName1);
// Save the second XPS document
doc2.save(getOutputDir() + outputFileName2);
// Add page to XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
// Create new XPS Document
XpsDocument doc = new XpsDocument(getDataDir() + "Sample1.xps");
String outputFileName = "AddPages_out.xps";
// Insert an empty page at beginning of pages list
doc.insertPage(1, true);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
// Change exisiting XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
// Create XPS document from XPS file
XpsDocument document = new XpsDocument(getDataDir() + "input.xps");
String outputFileName = "ChangeDocument_out.xps";
// Create fill of the signature text
XpsSolidColorBrush textFill = document.createSolidColorBrush(Color.PINK);
// Define pages where signature will be set
int[] pageNumbers = new int[] { 1, 2, 3 };
// For every defined page set signature "Confirmed" at coordinates x=650 and y=950
for (int i = 0; i < pageNumbers.length; i++) {
// Define active page
document.selectActivePage(pageNumbers[i]);
// Create glyphs object
XpsGlyphs glyphs = document.addGlyphs("Arial", 24, XpsFontStyle.Bold, 650, 900, "Confirmed");
// define fill for glyphs
glyphs.setFill(textFill);
}
// save changed XPS document
document.save(getOutputDir() + outputFileName);
// Create new XPS document from scratch.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
// Create new XPS Document
XpsDocument xDocs = new XpsDocument();
String outputFileName = "CreateDocument_out.xps";
// add glyph to the document
XpsGlyphs glyphs = xDocs.addGlyphs("Arial", 12, XpsFontStyle.Regular, 300f, 450f, "Hello World!");
glyphs.setFill(xDocs.createSolidColorBrush(Color.BLACK));
// save result
xDocs.save(getOutputDir() + outputFileName);
// Manipulate pages between XPS documents.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument(getDataDir() + "input1.xps");
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument(getDataDir() + "input2.xps");
// Create the third XPS Document
XpsDocument doc3 = new XpsDocument(getDataDir() + "input3.xps");
// Create the fourth XPS Document
XpsDocument doc4 = new XpsDocument();
String outputFileName = "ManipulatePages_out.xps";
// Insert active page (1 in this case) from the second document to the beginning of the fourth document
doc4.insertPage(1, doc2.getPage(), false);
// Insert active page (1 in this case) from the third document to the end of the fourth document
doc4.addPage(doc3.getPage(), false);
// Remove page 2 from the fourth document. This is an empty page that was created when document has been created.
doc4.removePageAt(2);
// Insert page 3 from the first document to the second postion of the fourth document
doc4.insertPage(2, doc1.selectActivePage(3), false);
// Save the fourth XPS document
doc4.save(getOutputDir() + outputFileName);
// Modify page on conversion event.
// Learn more: https://docs.aspose.com/page/java/xps/modifying-xps-pages-on-events/
// Open an XPS document
try (XpsDocument doc = new XpsDocument(getDataDir() + "Sample3.xps");
// Create a font
FileInputStream fontStream = new FileInputStream(getDataDir() + "arialbd.ttf")) {
// Create options for conversion to PDF
PdfSaveOptions options = new PdfSaveOptions();
// Set the filter for the pages that need conversion
options.setPageNumbers(new int[] { 2, 6, 7, 13 });
// Add the event handler that will execute right before the conversion of each page
options.getBeforePageSavingEventHandlers().add(new NavigationInjector(doc.createFont(fontStream), options.getPageNumbers()));
// Save resultant XPS document
doc.saveAsPdf(getOutputDir() + "ModifyPageOnConversion_out.pdf", options);
}
// The class to handle the before-page event while converting an XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/modifying-xps-pages-on-events/
private static class NavigationInjector extends BeforePageSavingEventHandler {
// The font in which navigation hyperlinks and page numbers will be displayed.
private final XpsFont font;
// The page numbers to convert.
private final TreeMap<Integer, Integer> pageNumbers;
public NavigationInjector(XpsFont font, int[] pageNumbers) {
this.font = font;
if (pageNumbers == null) {
this.pageNumbers = null;
return;
}
// Turn the page number array into a sorted collection of unique values.
this.pageNumbers = new TreeMap<>();
for (int pn : pageNumbers) {
this.pageNumbers.put(pn, 0);
}
}
/**
* The action itself to be triggered on a before-page event.
*
* @param args The event arguments.
*/
@Override
public void handle(BeforeSavingEventArgs<PageAPI> args) {
PageAPI api = args.getElementAPI();
XpsGlyphs glyphs;
// For all pages in the output PDF except the first one...
if (args.getOutputPageNumber() > 1) {
// ...insert a hyperlink to the first page...
glyphs = api.createGlyphs(font, 15f, 5f, api.getHeight() - 10f, "[First]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(pageNumbers == null ? 1 : pageNumbers.firstKey()));
api.add(glyphs);
// ...and to the previous page.
glyphs = api.createGlyphs(font, 15f, 60f, api.getHeight() - 10f, "[Prev]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
pageNumbers == null ? args.getAbsolutePageNumber() - 1 :
pageNumbers.keySet().toArray(new Integer[0])[args.getOutputPageNumber() - 2]));
api.add(glyphs);
}
// For all pages in the output PDF except the last one...
if ((pageNumbers != null && args.getOutputPageNumber() < pageNumbers.size()) ||
(pageNumbers == null && args.getOutputPageNumber() < api.getTotalPageCount())) {
// ...insert a hyperlink to the next page...
glyphs = api.createGlyphs(font, 15f, 110f, api.getHeight() - 10f, "[Next]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
pageNumbers == null ? args.getAbsolutePageNumber() + 1 :
pageNumbers.keySet().toArray(new Integer[0])[args.getOutputPageNumber()]));
api.add(glyphs);
// ...and to the last page.
glyphs = api.createGlyphs(font, 15f, 160f, api.getHeight() - 10f, "[Last]");
glyphs.setFill(api.createSolidColorBrush(Color.BLUE));
glyphs.setHyperlinkTarget(new XpsPageLinkTarget(
pageNumbers == null ? api.getTotalPageCount() :
pageNumbers.lastKey()));
api.add(glyphs);
}
// Insert a page number in the bottom-right corner.
glyphs = api.createGlyphs(font, 15f, api.getWidth() - 20f, api.getHeight() - 10f,
String.valueOf(args.getOutputPageNumber()));
glyphs.setFill(api.createSolidColorBrush(Color.BLACK));
api.add(glyphs);
// Add an outline entry to display the links to the converted pages in the navigation pane of a PDF viewer.
api.addOutlineEntry(String.format("Page %d", args.getOutputPageNumber()), 1, args.getAbsolutePageNumber());
}
}
// Remove page from XPS document.
// Learn more: https://docs.aspose.com/page/java/xps/working-with-pages/
// Create new XPS Document from XPS file containig 3 pages
XpsDocument doc = new XpsDocument(getDataDir() + "Sample2.xps");
String outputFileName = "RemovePages_out.xps";
// Remove the first page (at index 1).
doc.removePageAt(1);
// Save resultant XPS document
doc.save(getOutputDir() + outputFileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment