Xps to Pdf conversion failing

Hello,

I am working on project to convert XPS document,

earlier I was using below code for conversion,

final com.aspose.xps.rendering.PdfSaveOptions pdfSaveOptions = new com.aspose.xps.rendering.PdfSaveOptions();
			document = new com.aspose.xps.XpsDocument(sourceFilePath);
			final FileOutputStream pdfStream = new FileOutputStream(outputFilePath);

			if (targetType == TargetDocFormats.PDF) {
				if (-1 != pdfStandardLvl) {
					((IPdfOptions) pdfSaveOptions).setCompliance(pdfStandardLvl);
				}

				// Create rendering device for PDF format
				final com.aspose.xps.rendering.PdfDevice device = new com.aspose.xps.rendering.PdfDevice(pdfStream);

				// Export XPS to PDF
				document.save(device, pdfSaveOpt

which uses PdfDevice for conversion to pdf.
Below code for xps to png conversion,

final com.aspose.xps.rendering.PngSaveOptions pngSaveOptions = new com.aspose.xps.rendering.PngSaveOptions();
				pngSaveOptions.setSmoothingMode(SmoothingMode.HighQuality);
				pngSaveOptions.setResolution(300);
				pngSaveOptions.setPageNumbers(new int[] { 1 });

				// Create rendering device for XPS format
				final ImageDevice device = new ImageDevice();

				// Save XPS as PNG
				document.save(device, pngSaveOptions);

				// Iterate through document partitions (fixed documents, in XPS terms)
				for (int i = 0; i < 1; i++) {
					// Iterate through partition pages
					for (int j = 0; j < device.getResult()[i].length; j++) {
						final FileOutputStream imageStream = new FileOutputStream(outputFilePath);
						imageStream.write(device.getResult()[i][j], 0, device.getResult()[i][j].length);
						imageStream.close();
					}
				}

which uses ImageDevice for conversion to png.

But after upgrading to version 24.10 I got error for PdfDevice and ImageDevice as unrecognized smbols.
After that I tried below ways,

				// Convert XPS to PDF
	            FileOutputStream pdfStream = new FileOutputStream(outputFilePath);
	            PdfSaveOptions pdfOptions = new PdfSaveOptions();
	            document.save(pdfStream);
	            pdfStream.close();

and

// Iterate through pages and save as PNG
	            for (int i = 1; i <= document.getPageCount(); i++) {
	                FileOutputStream pngStream = new FileOutputStream(outputFilePath);
	                document.save(pngStream);
	                pngStream.close();
	            }

but these are also not working!
getting error like, com.aspose.imaging.coreexceptions.ImageLoadException: Cannot open an image. The image file format may be not supported at the moment.
at com.aspose.imaging.Image.a(Unknown Source)
at com.aspose.imaging.Image.load(Unknown Source)

What is the correct way to convert XPS to Pdf and Png by not using PdfDevice and ImageDevice?

@aakanksha76

To convert XPS documents to PDF and PNG formats without using PdfDevice and ImageDevice, you can follow the updated approach using the XpsDocument class along with the appropriate save options. Here’s how you can do it:

Converting XPS to PDF

  1. Load the XPS Document: Use the XpsDocument class to load your XPS file.
  2. Set Up PDF Save Options: Create an instance of PdfSaveOptions to configure the conversion settings.
  3. Save the Document: Use the SaveAsPdf method to save the document to a PDF file.

Here’s a code example for converting XPS to PDF:

import com.aspose.page.xps.XpsDocument;
import com.aspose.page.xps.presentation.pdf.PdfSaveOptions;

public class XpsToPdf {
    public static void main(String[] args) throws Exception {
        // Load the XPS document
        XpsDocument document = new XpsDocument("input.xps", new XpsLoadOptions());

        // Initialize PDF save options
        PdfSaveOptions options = new PdfSaveOptions();
        options.setJpegQualityLevel(100);
        options.setImageCompression(PdfImageCompression.Jpeg);
        options.setTextCompression(PdfTextCompression.Flate);
        options.setPageNumbers(new int[] { 1, 2, 6 }); // Specify pages to convert

        // Save the document as PDF
        document.saveAsPdf("output.pdf", options);
    }
}

Converting XPS to PNG

For converting XPS to PNG, you can follow a similar approach:

  1. Load the XPS Document: Again, use the XpsDocument class.
  2. Set Up PNG Save Options: Create an instance of PngSaveOptions to configure the conversion settings.
  3. Save the Document: Use the Save method to save the document as PNG.

Here’s a code example for converting XPS to PNG:

import com.aspose.page.xps.XpsDocument;
import com.aspose.page.xps.rendering.PngSaveOptions;

public class XpsToPng {
    public static void main(String[] args) throws Exception {
        // Load the XPS document
        XpsDocument document = new XpsDocument("input.xps");

        // Initialize PNG save options
        PngSaveOptions pngOptions = new PngSaveOptions();
        pngOptions.setSmoothingMode(SmoothingMode.HighQuality);
        pngOptions.setResolution(300);
        pngOptions.setPageNumbers(new int[] { 1 }); // Specify pages to convert

        // Save the document as PNG
        document.save("output.png", pngOptions);
    }
}

Important Notes

  • Ensure that you have the correct version of the Aspose library that supports these classes and methods.
  • Handle exceptions appropriately to catch any issues during the conversion process.

By following these steps, you should be able to convert XPS files to both PDF and PNG formats without encountering the issues you faced with PdfDevice and ImageDevice.

Sources:
[1]: XPS To PDF | Aspose.PDF for .NET API Reference
[2]: Convert XPS files | .NET API Solution - Aspose Documentation

Thanks for the support,
issue has been solved.