Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

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

Work with XPS Document Examples

These snippets show how to create and manipulate XPS (XML Paper Specification) documents using Aspose.Page for .NET. Create, change, insert, and remove XPS pages, exchange graphics elements and properties between XPS packages (files), and modify XPS pages in response to some event.

Key Use Cases

  • How to create XPS documents from scratch
  • How to add pages to an XPS document
  • How to change XPS page
  • How to manipulate XPS pages
  • Modify the XPS page upon saving the document
  • Cross-package operations

How to Run Examples

  1. Reference Aspose.Page for .NET: Aspose.Page on Windows; Aspose.Page.Drawing on non‑Windows.
  2. Copy a snippet into your project or run the related test.
  3. Apply a temporary license as described in the licensing guide.
  4. Build and run.

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 | .NET and Working with Pages in XPS file | .NET pages of public documentation.

Related Resources

Requirements

  • .NET 6.0+, .NET Core, or .NET Framework
  • Aspose.Page for .NET library
Aspose.Page for .NET – 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/net/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, FontStyle.Bold, 50, 250, "Test");
// Fill glyphs in the first document with one color
glyphs.Fill = 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.Clone());
// Fill glyphs in the second document with another color
((XpsSolidColorBrush)glyphs.Fill).Color = doc2.CreateColor(Color.Red);
// Save the first XPS document
doc1.Save(OutputDir + outputFileName1);
// Save the second XPS document
doc2.Save(OutputDir + outputFileName2);
// Shows possibility of using raster fill used in one document for painting glyphs in another one.
// Learn more: https://docs.aspose.com/page/net/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, FontStyle.Bold, 50, 250, "Test");
// Fill the glyphs with an image brush
glyphs1.Fill = doc1.CreateImageBrush(DataDir + "R08SY_NN.tif", new RectangleF(0f, 0f, 128f, 192f),
new RectangleF(0f, 0f, 64f, 96f));
((XpsImageBrush)glyphs1.Fill).TileMode = 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.Font, 200, 50, 250, "Test");
// Create an image brush from the fill of the the first document and fill glyphs in the second document
glyphs2.Fill = doc2.CreateImageBrush(((XpsImageBrush)glyphs1.Fill).Image, new RectangleF(0f, 0f, 128f, 192f),
new RectangleF(0f, 0f, 128f, 192f));
((XpsImageBrush)glyphs2.Fill).TileMode = XpsTileMode.Tile;
// Save the first XPS document
doc1.Save(OutputDir + outputFileName1);
// Save the second XPS document
doc2.Save(OutputDir + outputFileName2);
// Add page to XPS document.
// Learn more: https://docs.aspose.com/page/net/xps/working-with-pages/
// Create new XPS Document
XpsDocument doc = new XpsDocument(DataDir + "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(OutputDir + outputFileName);
// Change exisiting XPS document.
// Learn more: https://docs.aspose.com/page/net/xps/working-with-pages/
// Create XPS document from XPS file
XpsDocument document = new XpsDocument(DataDir + "input.xps", new XpsLoadOptions());
string outputFileName = "ChangeDocument_out.xps";
// Create fill of the signature text
XpsSolidColorBrush textFill = document.CreateSolidColorBrush(Color.BlueViolet);
// 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, FontStyle.Bold, 650, 900, "Confirmed");
// define fill for glyphs
glyphs.Fill = textFill;
}
// save changed XPS document
document.Save(OutputDir + outputFileName);
// Create new XPS document from scratch.
// Learn more: https://docs.aspose.com/page/net/xps/working-with-pages/
// Create new XPS Document
XpsDocument xDocs = new XpsDocument();
string outputFileName = "CreateDocument_out.xps";
// add glyph to the document
var glyphs = xDocs.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");
glyphs.Fill = xDocs.CreateSolidColorBrush(Color.Black);
// save result
xDocs.Save(OutputDir + outputFileName);
// Manipulate pages between XPS documents.
// Learn more: https://docs.aspose.com/page/net/xps/working-with-pages/
// Create the first XPS Document
XpsDocument doc1 = new XpsDocument(DataDir + "input1.xps");
// Create the second XPS Document
XpsDocument doc2 = new XpsDocument(DataDir + "input2.xps");
// Create the third XPS Document
XpsDocument doc3 = new XpsDocument(DataDir + "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.Page, false);
// Insert active page (1 in this case) from the third document to the end of the fourth document
doc4.AddPage(doc3.Page, 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(OutputDir + outputFileName);
// Modify page on conversion event.
// Learn more: https://docs.aspose.com/page/net/xps/event-based-modifications/
// Open an XPS document
using (XpsDocument doc = new XpsDocument(DataDir + "Sample3.xps"))
// Create a font
using (Stream fontStream = File.OpenRead(DataDir + "arialbd.ttf"))
{
// Create options for conversion to PDF
PdfSaveOptions options = new PdfSaveOptions();
// Set the filter for the pages that need conversion
options.PageNumbers = new int[] { 2, 6, 7, 13 };
// Add the event handler that will execute right before the conversion of each page
options.BeforePageSavingEventHandlers.Add(new NavigationInjector(doc.CreateFont(fontStream), options.PageNumbers));
// Save resultant XPS document
doc.SaveAsPdf(OutputDir + "ModifyPageOnConversion_out.pdf", options);
}
// The class to handle the before-page event while converting an XPS document.
// Learn more: https://docs.aspose.com/page/net/xps/event-based-modifications/
class NavigationInjector : BeforePageSavingEventHandler
{
// The font in which navigation hyperlinks and page numbers will be displayed.
private readonly XpsFont _font;
// The page numbers to convert.
private readonly SortedList<int, int> _pageNumbers;
public NavigationInjector(XpsFont font, int[] pageNumbers)
{
_font = font;
if (pageNumbers == null)
return;
// Turn the page number array into a sorted collection of unique values.
_pageNumbers = new SortedList<int, int>();
foreach (int pn in pageNumbers)
_pageNumbers[pn] = 0;
}
/// <summary>
/// The action itself to be triggered on a before-page event.
/// </summary>
/// <param name="args">The event arguments.</param>
public override void Handle(BeforeSavingEventArgs<PageAPI> args)
{
PageAPI api = args.ElementAPI;
XpsGlyphs glyphs;
// For all pages in the output PDF except the first one...
if (args.OutputPageNumber > 1)
{
// ...insert a hyperlink to the first page...
glyphs = api.CreateGlyphs(_font, 15f, 5f, api.Height - 10f, "[First]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(_pageNumbers == null ? 1 : _pageNumbers.Keys[0]);
api.Add(glyphs);
// ...and to the previous page.
glyphs = api.CreateGlyphs(_font, 15f, 60f, api.Height - 10f, "[Prev]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? args.AbsolutePageNumber - 1 : _pageNumbers.Keys[args.OutputPageNumber - 2]);
api.Add(glyphs);
}
// For all pages in the output PDF except the last one...
if ((_pageNumbers != null && args.OutputPageNumber < _pageNumbers.Count) ||
(_pageNumbers == null && args.OutputPageNumber < api.TotalPageCount))
{
// ...insert a hyperlink to the next page...
glyphs = api.CreateGlyphs(_font, 15f, 110f, api.Height - 10f, "[Next]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? args.AbsolutePageNumber + 1 : _pageNumbers.Keys[args.OutputPageNumber]);
api.Add(glyphs);
// ...and to the last page.
glyphs = api.CreateGlyphs(_font, 15f, 160f, api.Height - 10f, "[Last]");
glyphs.Fill = api.CreateSolidColorBrush(Color.Blue);
glyphs.HyperlinkTarget = new XpsPageLinkTarget(
_pageNumbers == null ? api.TotalPageCount : _pageNumbers.Keys[_pageNumbers.Keys.Count - 1]);
api.Add(glyphs);
}
// Insert a page number in the bottom-right corner.
glyphs = api.CreateGlyphs(_font, 15f, api.Width - 20f, api.Height - 10f, args.OutputPageNumber.ToString());
glyphs.Fill = 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 {0}", args.OutputPageNumber), 1, args.AbsolutePageNumber);
}
}
// Remove page from XPS document.
// Learn more: https://docs.aspose.com/page/net/xps/working-with-pages/
// Create new XPS Document from XPS file containig 3 pages
XpsDocument doc = new XpsDocument(DataDir + "Sample2.xps");
string outputFileName = "RemovePages_out.xps";
// Remove the first page (at index 1).
doc.RemovePageAt(1);
// Save resultant XPS document
doc.Save(OutputDir + outputFileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment