XPS Conversion

XPS Conversion

XpsDocument is the entry point for loading XML Paper Specification (.xps) files. It provides the same to_pdf() and to_image() interface as PsDocument.


Loading an XPS File

Use XpsDocument.from_file() to load from a file path, or from_bytes() to load from raw bytes.

from aspose.page.xps.document import XpsDocument

xps = XpsDocument.from_file("document.xps")

Exporting to PDF

Call to_pdf() to obtain the document as a PDF byte array.

from pathlib import Path
from aspose.page.xps.document import XpsDocument

xps = XpsDocument.from_file("input.xps")
pdf_bytes = xps.to_pdf()
Path("output.pdf").write_bytes(pdf_bytes)

Exporting to PNG or JPEG

Pass an ImageSaveOptions instance to to_image():

from pathlib import Path
from aspose.page.xps.document import XpsDocument
from aspose.page.ps.output import ImageSaveOptions

xps = XpsDocument.from_file("input.xps")
opts = ImageSaveOptions(format="png", dpi=96)
Path("output.png").write_bytes(xps.to_image(opts))

Page Management

XpsDocument provides methods to add, insert, remove, and retrieve individual pages.

from aspose.page.xps.document import XpsDocument

xps = XpsDocument.from_file("input.xps")
page = xps.add_page(595, 842)       # add an A4 page
xps.remove_page(0)                   # remove the first page

Tips and Best Practices

  • Use from_bytes() for in-memory XPS data from uploads or streams.
  • The add_page(), remove_page(), and get_page() methods enable page-level manipulation before export.
  • to_pdf() and to_image() return bytes directly — no temporary files required.

Common Issues

IssueCauseFix
FileNotFoundErrorPath does not existVerify path before from_file()
Empty outputXPS file has no fixed pagesInspect the XPS source

FAQ

Can I convert XPS to PNG?

Yes. Pass ImageSaveOptions(format="png", dpi=150) to to_image().

Is there a page limit?

No explicit page limit is imposed. Memory usage scales with page count and DPI.


API Reference Summary

Class / MethodDescription
XpsDocument.from_file(path)Load XPS from a file path
XpsDocument.from_bytes(data)Load XPS from raw bytes
XpsDocument.to_pdf(options)Export to PDF bytes
XpsDocument.to_image(options)Export to raster image bytes
XpsDocument.add_page(width, height)Add a new page
XpsDocument.remove_page(index)Remove page at index
XpsDocument.get_page(index)Get page at index

See Also