Customize HTML Page Margins with Aspose.HTML

Aspose.HTML for Java is a powerful library for processing HTML documents in Java applications. In this tutorial, we will explore how to create custom page margins and add page numbers and titles to your HTML documents using Aspose.HTML for Java. This step-by-step guide will break down the process into manageable steps to help you easily integrate these features into your HTML documents.

Prerequisites

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

  1. Java Development Environment: Ensure you have a Java development environment set up on your computer.

  2. Aspose.HTML for Java: Download and install the Aspose.HTML for Java library from here.

Import Packages

To get started, you need to import the necessary packages from Aspose.HTML for Java. Add the following import statements to your Java code:

// Import Aspose.HTML packages
import com.aspose.html.Configuration;
import com.aspose.html.services.IUserAgentService;
import com.aspose.html.HTMLDocument;
import com.aspose.html.rendering.xps.XpsDevice;

Now, let’s break down the process of adding custom page margins, page numbers, and titles into manageable steps:

Step 1: Initialize Configuration and Page Margins

// Initialize configuration object and set up the page-margins for the document
Configuration configuration = new Configuration();
try {
    // Get the User Agent service
    IUserAgentService userAgent = configuration.getService(IUserAgentService.class);
    // Set the style of custom margins and create marks on it
    userAgent.setUserStyleSheet("@page\n" +
            "{\n" +
            "    /* Page margins should be not empty in order to write content inside the margin-boxes */\n" +
            "    margin-top: 1cm;\n" +
            "    margin-left: 2cm;\n" +
            "    margin-right: 2cm;\n" +
            "    margin-bottom: 2cm;\n" +
            "    /* Page counter located at the bottom of the page */\n" +
            "    @bottom-right\n" +
            "    {\n" +
            "        -aspose-content: \"Page \" currentPageNumber() \" of \" totalPagesNumber();\n" +
            "        color: green;\n" +
            "    }\n" +
            "\n" +
            "    /* Page title located at the top-center box */\n" +
            "    @top-center\n" +
            "    {\n" +
            "        -aspose-content: \"Hello World Document Title!!!\";\n" +
            "        vertical-align: bottom;\n" +
            "        color: blue;\n" +
            "    }\n" +
            "}\n");

In this step, we initialize the configuration object and set up custom page margins, including the position of the page counter and page title.

Step 2: Initialize an HTML Document

// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<div>Hello World!!!</div>", ".", configuration);

Here, we create an HTML document with a sample content (in this case, a “Hello World” message) and apply the configuration from Step 1.

Step 3: Initialize an Output Device and Render the Document

// Initialize an output device
XpsDevice device = new XpsDevice(Resources.output("output.xps"));
try {
    // Send the document to the output device
    document.renderTo(device);
} finally {
    if (device != null) {
        device.dispose();
    }
}

In this step, we set up an output device and render the HTML document. The document will be processed and saved as an XPS file with the specified page margins, page numbers, and title.

Conclusion

Congratulations! You’ve successfully learned how to create custom page margins and add page numbers and titles to your HTML documents using Aspose.HTML for Java. This customization allows you to create more professional and visually appealing documents.

If you have any questions or face any issues, feel free to visit the Aspose.HTML for Java documentation or seek assistance on the Aspose support forum.

FAQ’s

Q1: What is Aspose.HTML for Java?

A1: Aspose.HTML for Java is a Java library that provides powerful tools for working with HTML documents in Java applications.

Q2: Can I customize the page margins further?

A2: Yes, you can modify the CSS styles in Step 1 to customize page margins as per your requirements.

Q3: How can I add more content to the HTML document?

A3: You can modify the HTML content in Step 2 by replacing the sample content with your own.

Q4: Is Aspose.HTML for Java compatible with other document formats?

A4: Yes, Aspose.HTML for Java can be used to convert HTML documents to various formats, including PDF, XPS, and images.

Q5: Do I need a license for using Aspose.HTML for Java?

A5: Yes, you can obtain a license or a free trial from here or here.