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:
- -webkit- (Chrome, Safari, newer versions of Opera, almost all iOS browsers (including Firefox for iOS); basically, any WebKit based browser)
- -moz- (Mozilla-based browsers such as Firefox)
- -o- (Old, pre-WebKit, versions of Opera)
- -ms- (Internet Explorer and Microsoft Edge)
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:
Name | Description |
---|---|
currentPageNumber | This function returns the number of the current rendering page. |
totalPagesNumber | This 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:
- Initialize an instance of the Configuration class:
- Use the getService() method to fetch the User Agent service implementation from the configuration.
- Use the setUserStyleSheetU() method to define CSS rules for page margins, content placement, and styling for page counters and titles.
- Here is used the -aspose-content CSS property to define the content displayed in the bottom-right area. It includes the currentPageNumber() and the totalPagesNumber() functions, which are provided by the Aspose.HTML library to dynamically generate the current page number and a total number of pages.
- Also, the -aspose-content CSS extension defines the content displayed in the top-center area. It sets the content to the string “Hello World Document Title!!!”.
- Initializes an HTML document using the HTMLDocument class.
- Create an XpsDevice instance and specify the output file path where the rendered document will be saved.
- Use the renderTo() method to convert the HTML to XPS.
1// @java java/com/aspose/html/examples/Examples_Java_AdvancedUsage_CSSExtensions_AddTitleAndPageNumber.java
2// @java java/com/aspose/html/documentation/examples/Advanced_CSSExtension.java
3// Initialize configuration object and set up the page-margins for the document
4Configuration configuration = new Configuration();
5
6// Get the User Agent service
7IUserAgentService userAgent = configuration.getService(IUserAgentService.class);
8
9// Set the style of custom margins and create marks on it
10userAgent.setUserStyleSheet(
11 "@page {\n" +
12 " /* Page margins should be not empty in order to write content inside the margin-boxes */\n" +
13 " margin-top: 1cm;\n" +
14 " margin-left: 2cm;\n" +
15 " margin-right: 2cm;\n" +
16 " margin-bottom: 2cm;\n" +
17 " /* Page counter located at the bottom of the page */\n" +
18 " @bottom-right {\n" +
19 " -aspose-content: \"Page \" currentPageNumber() \" of \" totalPagesNumber();\n" +
20 " color: green;\n" +
21 " }\n" +
22 " /* Page title located at the top-center box */\n" +
23 " @top-center {\n" +
24 " -aspose-content: \"Hello World Document Title!!!\";\n" +
25 " vertical-align: bottom;\n" +
26 " color: blue;\n" +
27 " }\n" +
28 "}"
29);
30
31// Initialize an HTML document
32HTMLDocument document = new HTMLDocument("<div>Hello World!!!</div>", ".", configuration);
33
34// Initialize an output device
35XpsDevice device = new XpsDevice("output/output.xps");
36
37// Send the document to the output device
38document.renderTo(device);
You can download the complete examples and data files from GitHub.