CSS Extensions – CSS Vendor Prefixes – Aspose.HTML for Java

CSS Vendor Prefixes

CSS vendor prefixes, sometimes called CSS browser prefixes, are used in CSS property names to implement experimental or pre-release CSS features that are not yet standardized or may have limited support in certain browsers. Vendor prefixes are used to identify specific browsers or browser versions that support these features.

Historically, browser vendors use the prefixes for nonstandard CSS features. Following is the list of the major browsers prefixes:

According to the CSS specification, CSS vendor-specific extensions must start with a dash or underscore and have the following format:

['-' or '_'] + [vendor identifier] + ['-'] + [name]

For example, the -webkit-border-radius is a vendor-prefixed property that allows you to round the corners of elements to create a visually appealing and modern design in WebKit-based browsers, such as Chrome.

Aspose CSS Extension – Java Example

The prefix that is used by Aspose.HTML library looks like -aspose- and gives you some experimental features. Following is a list of CSS functions that can be enabled by using -aspose- prefix:

NameDescription
currentPageNumberThis function returns the number of the current rendering page.
totalPagesNumberThis function returns the number of the total pages in the document.

The next code snippet demonstrates how to use CSS extensions to create custom marks on document margins:

  1. Initialize an instance of the Configuration class:
  2. Use the getService() method to fetch the User Agent service implementation from the configuration.
  3. Use the setUserStyleSheetU() method to define CSS rules for page margins, content placement, and styling for page counters and titles.
  1. Initializes an HTML document using the HTMLDocument class.
  2. Create an XpsDevice instance and specify the output file path where the rendered document will be saved.
  3. Use the renderTo() method to convert the HTML to XPS.
 1//  Initialize configuration object and set up the page-margins for the document
 2com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
 3
 4// Get the User Agent service
 5com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
 6
 7// Set the style of custom margins and create marks on it
 8userAgent.setUserStyleSheet(
 9        "@page {\n" +
10        "  /* Page margins should be not empty in order to write content inside the margin-boxes */\n" +
11        "  margin-top: 1cm;\n" +
12        "  margin-left: 2cm;\n" +
13        "  margin-right: 2cm;\n" +
14        "  margin-bottom: 2cm;\n" +
15        "  /* Page counter located at the bottom of the page */\n" +
16        "  @bottom-right {\n" +
17        "    -aspose-content: \"Page \" currentPageNumber() \" of \" totalPagesNumber();\n" +
18        "    color: green;\n" +
19        "  }\n" +
20        "  /* Page title located at the top-center box */\n" +
21        "  @top-center {\n" +
22        "    -aspose-content: \"Hello World Document Title!!!\";\n" +
23        "    vertical-align: bottom;\n" +
24        "    color: blue;\n" +
25        "  }\n" +
26        "}"
27);
28
29//  Initialize an HTML document
30com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("<div>Hello World!!!</div>", ".", configuration);
31
32//  Initialize an output device
33com.aspose.html.rendering.xps.XpsDevice device = new com.aspose.html.rendering.xps.XpsDevice("output/output.xps");
34
35// Send the document to the output device
36document.renderTo(device);

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.