Adjust XPS Page Size with Aspose.HTML for Java

In this tutorial, we will guide you through the process of adjusting the XPS page size using Aspose.HTML for Java. This powerful library allows you to manipulate HTML documents and render them into various formats, including XPS. Adjusting the page size is essential when you need to control the output dimensions of your XPS document.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. Java Development Environment: Ensure that you have Java Development Kit (JDK) installed on your system.

  2. Aspose.HTML for Java Library: You need to download and include the Aspose.HTML for Java library in your Java project. You can find the library here.

  3. Input HTML File: Prepare an HTML file that you want to render and adjust the XPS page size for. You can use your own HTML file for this tutorial.

Import Packages

First, you need to import the necessary packages to work with Aspose.HTML for Java. Include these packages at the beginning of your Java class:

import com.aspose.html.drawing.Page;
import com.aspose.html.rendering.HtmlRenderer;
import com.aspose.html.rendering.PageSetup;
import com.aspose.html.rendering.xps.XpsDevice;
import com.aspose.html.rendering.xps.XpsRenderingOptions;
import com.aspose.html.HTMLDocument;

Step 1: Set the Input File Name

try (java.io.FileInputStream fileInputStream = new java.io.FileInputStream("YourInputFile.html")) {
    // ...
}

In this step, we read your HTML input file using a FileInputStream.

Step 2: Create an HTML Document and Set Styles

com.aspose.html.HTMLDocument html_document = new com.aspose.html.HTMLDocument("YourOutputFile.html");

String style = "<style>\n" +
               ".st\n" +
               "{\n" +
               "color: green;\n" +
               "}\n" +
               "</style>\n" +
               "<div id=id1>Aspose.HTML rendering Text in Black Color</div>\n" +
               "<div id=id2 class=''st''>Aspose.HTML rendering Text in Green Color</div>\n" +
               "<div id=id3 class=''st'' style='color: blue;'>Aspose.HTML rendering Text in Blue Color</div>\n" +
               "<div id=id3 class=''st'' style='color: red;'>Aspose.HTML rendering Text in Red Color</div>\n";

// ...

This step involves creating an HTMLDocument and adding styles to it.

Step 3: Create XPS Rendering Options

com.aspose.html.rendering.xps.XpsRenderingOptions xps_options = new com.aspose.html.rendering.xps.XpsRenderingOptions();

Here, we create XPS rendering options to configure the rendering process.

Step 4: Adjust the Page Size

com.aspose.html.drawing.Page page = new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100));
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
pageSetup.setAnyPage(page);
pageSetup.setAdjustToWidestPage(false);
xps_options.setPageSetup(pageSetup);

This step involves setting the page size and specifying whether to adjust it to the widest page.

Step 5: Render the Output

com.aspose.html.rendering.xps.XpsDevice device = new com.aspose.html.rendering.xps.XpsDevice(xps_options, "YourOutputFile.xps");

renderer.render(device, html_document);

In the final step, we render the XPS output using the configured options.

Conclusion

In this tutorial, we’ve shown you how to adjust the XPS page size using Aspose.HTML for Java. You can control the output dimensions of your XPS documents, ensuring they meet your specific requirements. With the provided code and steps, you can easily implement this feature in your Java applications.

If you have any questions or need further assistance, feel free to visit the Aspose.HTML for Java documentation or ask for help on the Aspose Forum.

FAQ’s

Q1: What is Aspose.HTML for Java?

A1: Aspose.HTML for Java is a Java library that allows developers to manipulate and convert HTML documents into various formats, such as XPS, PDF, and images.

Q2: Where can I download Aspose.HTML for Java?

A2: You can download the Aspose.HTML for Java library from this link.

Q3: Is there a free trial available for Aspose.HTML for Java?

A3: Yes, you can get a free trial of Aspose.HTML for Java from here.

Q4: How can I obtain a temporary license for Aspose.HTML for Java?

A4: To get a temporary license for Aspose.HTML for Java, visit this page.

Q5: Can I get support for Aspose.HTML for Java?

A5: Yes, you can seek help and support from the Aspose community on the Aspose Forum.