Skip to content

Commit 94436b1

Browse files
Updated advanced usage examples
1 parent 6bd4ba6 commit 94436b1

File tree

8 files changed

+896
-403
lines changed

8 files changed

+896
-403
lines changed

content/english/java/advanced-usage/_index.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ url: /java/advanced-usage/
99
---
1010

1111
## Advanced Usage of Aspose.HTML Java Tutorials
12-
### [CSS Extensions - Adding Title and Page Number](./css-extensions-adding-title-page-number/)
13-
### [DOM Mutation Observer - Observing Node Additions](./dom-mutation-observer-observing-node-additions/)
14-
### [HTML5 Canvas Manipulation Using Code](./html5-canvas-manipulation-using-code/)
15-
### [HTML5 Canvas Manipulation Using JavaScript](./html5-canvas-manipulation-using-javascript/)
16-
### [HTML Form Editor - Filling and Submitting Forms](./html-form-editor-filling-submitting-forms/)
17-
### [Adjusting PDF Page Size](./adjust-pdf-page-size/)
18-
### [Adjusting XPS Page Size](./adjust-xps-page-size/)
12+
### [Customize HTML Page Margins with Aspose.HTML](./css-extensions-adding-title-page-number/)
13+
Learn how to customize page margins, add page numbers, and titles to HTML documents using Aspose.HTML for Java.
14+
### [DOM Mutation Observer with Aspose.HTML for Java](./dom-mutation-observer-observing-node-additions/)
15+
Learn how to use Aspose.HTML for Java to implement a DOM Mutation Observer in this step-by-step guide. Monitor and react to DOM changes effectively.
16+
### [HTML5 Canvas Manipulation with Aspose.HTML for Java](./html5-canvas-manipulation-using-code/)
17+
Learn HTML5 Canvas manipulation using Aspose.HTML for Java. Create interactive graphics with step-by-step guidance.
18+
### [HTML5 Canvas Manipulation with Aspose.HTML for Java](./html5-canvas-manipulation-using-javascript/)
19+
Learn how to manipulate HTML5 Canvas with JavaScript using Aspose.HTML for Java. Create dynamic graphics and convert to PDF.
20+
### [Automate HTML Form Filling with Aspose.HTML for Java](./html-form-editor-filling-submitting-forms/)
21+
Learn how to automate HTML form filling and submission with Aspose.HTML for Java. Simplify web interaction with this tutorial.
22+
### [Adjust PDF Page Size with Aspose.HTML for Java](./adjust-pdf-page-size/)
23+
Learn how to adjust PDF page size with Aspose.HTML for Java. Create high-quality PDFs from HTML effortlessly. Control page dimensions effectively.
24+
### [Adjust XPS Page Size with Aspose.HTML for Java](./adjust-xps-page-size/)
25+
Learn how to adjust XPS page size with Aspose.HTML for Java. Control the output dimensions of your XPS documents easily.
Lines changed: 133 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,143 @@
11
---
2-
title: Adjusting PDF Page Size
2+
title: Adjust PDF Page Size with Aspose.HTML for Java
33
linktitle: Adjusting PDF Page Size
44
second_title: Java HTML Processing with Aspose.HTML
5-
description:
5+
description: Learn how to adjust PDF page size with Aspose.HTML for Java. Create high-quality PDFs from HTML effortlessly. Control page dimensions effectively.
66
type: docs
77
weight: 15
88
url: /java/advanced-usage/adjust-pdf-page-size/
99
---
1010

11-
## Complete Source Code
11+
In today's digital age, the need for generating high-quality PDFs from HTML content is on the rise. Aspose.HTML for Java is a powerful Java library that allows you to convert HTML documents to PDF format effortlessly. In this tutorial, we will focus on adjusting the page size when converting HTML to PDF using Aspose.HTML for Java.
12+
13+
## Prerequisites
14+
15+
Before you begin, make sure you have the following prerequisites in place:
16+
17+
- Java Development Environment: Ensure that you have Java Development Kit (JDK) installed on your system.
18+
- Aspose.HTML for Java: You need to download and install Aspose.HTML for Java from the website [here](https://releases.aspose.com/html/java/).
19+
- HTML Document: You should have an HTML document ready for conversion. If not, create one or use an existing HTML file.
20+
21+
## Import Packages
22+
23+
To start, you need to import the necessary packages to work with Aspose.HTML for Java. The following code snippet demonstrates how to do this:
24+
1225
```java
13-
//@START
14-
try (java.io.FileInputStream fileInputStream = new java.io.FileInputStream(Resources.input("FirstFile.html"))) {
15-
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(Resources.output("FirstFileOut.html"))) {
16-
byte[] bytes = new byte[fileInputStream.available()];
17-
fileInputStream.read(bytes);
18-
fileOutputStream.write(bytes);
19-
String style = "<style>\n" +
20-
".st\n" +
21-
"{\n" +
22-
"color:\n" +
23-
"green;\n" +
24-
"}\n" +
25-
"</style >\n" +
26-
"<div id = id1 > Aspose.Html rendering Text in Black Color</div >\n" +
27-
"<div id = id2 class='' st '' > Aspose.Html rendering Text in Green Color</div >\n" +
28-
"<div id = id3 class='' st '' style = 'color: blue;' > Aspose.Html rendering Text in Blue Color</div >\n" +
29-
"<div id = id3 class='' st '' style = 'color: red;' ><font face = 'Arial' > Aspose.Html rendering Text in Red\n" +
30-
"Color</font ></div >\n";
31-
fileOutputStream.write(style.getBytes(java.nio.charset.StandardCharsets.UTF_8));
32-
}
33-
String pdf_output;
34-
// Create HtmlRenderer object
35-
com.aspose.html.rendering.HtmlRenderer pdf_renderer = new com.aspose.html.rendering.HtmlRenderer();
36-
try {
37-
// Create HtmlDocument instnace while passing path of already created HTML file
38-
com.aspose.html.HTMLDocument html_document = new com.aspose.html.HTMLDocument(Resources.output("FirstFileOut.html"));
39-
try {
40-
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
41-
com.aspose.html.rendering.pdf.PdfRenderingOptions pdf_options =
42-
new com.aspose.html.rendering.pdf.PdfRenderingOptions();
43-
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
44-
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
45-
pageSetup.setAdjustToWidestPage(false);
46-
pdf_options.setPageSetup(pageSetup);
47-
pdf_output = Resources.output("not-adjusted-to-widest-page_out.pdf");
48-
com.aspose.html.rendering.pdf.PdfDevice device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);
49-
try {
50-
// Render the output
51-
pdf_renderer.render(device, html_document);
52-
} finally {
53-
if (device != null) {
54-
device.dispose();
55-
}
56-
}
57-
// Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
58-
pdf_options = new com.aspose.html.rendering.pdf.PdfRenderingOptions();
59-
pageSetup = new com.aspose.html.rendering.PageSetup();
60-
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
61-
pageSetup.setAdjustToWidestPage(true);
62-
pdf_options.setPageSetup(pageSetup);
63-
pdf_output = Resources.output("adjusted-to-widest-page_out.pdf");
64-
device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);
65-
try {
66-
// Render the output
67-
pdf_renderer.render(device, html_document);
68-
} finally {
69-
if (device != null) {
70-
device.dispose();
71-
}
72-
}
73-
} finally {
74-
if (html_document != null) {
75-
html_document.dispose();
76-
}
77-
}
78-
} finally {
79-
if (pdf_renderer != null) {
80-
pdf_renderer.dispose();
81-
}
82-
}
83-
}
84-
//@END
26+
import com.aspose.html.HTMLDocument;
27+
import com.aspose.html.rendering.HtmlRenderer;
28+
import com.aspose.html.rendering.pdf.PdfDevice;
29+
import com.aspose.html.rendering.pdf.PdfRenderingOptions;
30+
import com.aspose.html.drawing.Size;
31+
import com.aspose.html.rendering.PageSetup;
8532
```
33+
34+
Now that you have the prerequisites in place and have imported the necessary packages, let's break down the process of adjusting the PDF page size into multiple steps:
35+
36+
## Step 1: Reading HTML Content
37+
38+
First, you need to read the HTML content that you want to convert to PDF. In this example, we will read the HTML from a file named "FirstFile.html."
39+
40+
```java
41+
try (java.io.FileInputStream fileInputStream = new java.io.FileInputStream(Resources.input("FirstFile.html"))) {
42+
```
43+
44+
## Step 2: Writing HTML Content
45+
46+
Next, you will write the HTML content to a file named "FirstFileOut.html."
47+
48+
```java
49+
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream(Resources.output("FirstFileOut.html"))) {
50+
byte[] bytes = new byte[fileInputStream.available()];
51+
fileInputStream.read(bytes);
52+
fileOutputStream.write(bytes);
53+
// Add custom HTML styles or content here
54+
String style = "<style>\n" +
55+
".st\n" +
56+
"{\n" +
57+
"color:\n" +
58+
"green;\n" +
59+
"}\n" +
60+
"</style >\n" +
61+
"<div id = id1 > Aspose.Html rendering Text in Black Color</div >\n" +
62+
"<div id = id2 class='' st '' > Aspose.Html rendering Text in Green Color</div >\n" +
63+
"<div id = id3 class='' st '' style = 'color: blue;' > Aspose.Html rendering Text in Blue Color</div >\n" +
64+
"<div id = id3 class='' st '' style = 'color: red;' ><font face = 'Arial' > Aspose.Html rendering Text in Red\n" +
65+
"Color</font ></div >\n";
66+
fileOutputStream.write(style.getBytes(java.nio.charset.StandardCharsets.UTF_8));
67+
}
68+
```
69+
70+
## Step 3: Rendering HTML to PDF
71+
72+
Now, you will render the HTML content to a PDF file. We will cover two scenarios: one where the page size is not adjusted to the content width, and another where it is adjusted.
73+
74+
### Page Size Not Adjusted
75+
76+
In this scenario, the page size is set to a fixed width and height, which may crop the content if it exceeds these dimensions.
77+
78+
```java
79+
String pdf_output;
80+
com.aspose.html.rendering.HtmlRenderer pdf_renderer = new com.aspose.html.rendering.HtmlRenderer();
81+
82+
// Create an HTMLDocument instance from the HTML file
83+
com.aspose.html.HTMLDocument html_document = new com.aspose.html.HTMLDocument(Resources.output("FirstFileOut.html"));
84+
85+
// Set PDF rendering options
86+
com.aspose.html.rendering.pdf.PdfRenderingOptions pdf_options = new com.aspose.html.rendering.pdf.PdfRenderingOptions();
87+
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
88+
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
89+
pageSetup.setAdjustToWidestPage(false);
90+
pdf_options.setPageSetup(pageSetup);
91+
92+
pdf_output = Resources.output("not-adjusted-to-widest-page_out.pdf");
93+
com.aspose.html.rendering.pdf.PdfDevice device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);
94+
95+
// Render the output
96+
pdf_renderer.render(device, html_document);
97+
```
98+
99+
### Page Size Adjusted to Content Width
100+
101+
In this scenario, the page size is adjusted according to the content width.
102+
103+
```java
104+
com.aspose.html.rendering.pdf.PdfRenderingOptions pdf_options = new com.aspose.html.rendering.pdf.PdfRenderingOptions();
105+
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
106+
pageSetup.setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
107+
pageSetup.setAdjustToWidestPage(true);
108+
pdf_options.setPageSetup(pageSetup);
109+
110+
pdf_output = Resources.output("adjusted-to-widest-page_out.pdf");
111+
device = new com.aspose.html.rendering.pdf.PdfDevice(pdf_options, pdf_output);
112+
113+
// Render the output
114+
pdf_renderer.render(device, html_document);
115+
```
116+
117+
## Conclusion
118+
119+
In this tutorial, we have explored how to adjust the PDF page size when converting HTML to PDF using Aspose.HTML for Java. You have learned the prerequisites, imported the required packages, and followed a step-by-step guide to achieve this task. With Aspose.HTML for Java, you can easily control the page size of your generated PDFs, ensuring that your content is displayed as intended.
120+
121+
Feel free to experiment with different page sizes and settings to meet your specific requirements. If you encounter any issues or have further questions, don't hesitate to seek assistance from the [Aspose.HTML for Java documentation](https://reference.aspose.com/html/java/) or the [Aspose support forum](https://forum.aspose.com/).
122+
123+
## FAQ's
124+
125+
### Q1: What is Aspose.HTML for Java?
126+
127+
A1: Aspose.HTML for Java is a Java library that allows you to work with HTML documents, manipulate, convert, and render them to various formats, including PDF.
128+
129+
### Q2: How can I adjust the PDF page size when converting HTML to PDF with Aspose.HTML for Java?
130+
131+
A2: You can adjust the PDF page size by using the `PageSetup` class and setting the `AdjustToWidestPage` property to `true` or `false, depending on your requirements.
132+
133+
### Q3: Can I customize the styling of HTML content before converting it to PDF?
134+
135+
A3: Yes, you can customize the styling by adding CSS and HTML elements to your HTML content before converting it to PDF, as demonstrated in the tutorial.
136+
137+
### Q4: Where can I find the documentation for Aspose.HTML for Java?
138+
139+
A4: You can refer to the documentation [here](https://reference.aspose.com/html/java/) for comprehensive information and examples.
140+
141+
### Q5: Is there a free trial available for Aspose.HTML for Java?
142+
143+
A5: Yes, you can access a free trial of Aspose.HTML for Java from [this link](https://releases.aspose.com/).

0 commit comments

Comments
 (0)