@@ -101,26 +101,24 @@ pageSetup.setOrientation(Orientation.LANDSCAPE);
101
101
// Add headers and footers
102
102
pageSetup. setHeaderDistance(20 );
103
103
pageSetup. setFooterDistance(10 );
104
- pageSetup. setHeaderFooter(HeaderFooterType . HEADER_PRIMARY , new Paragraph (doc, " Header Text" ));
105
- pageSetup. setHeaderFooter(HeaderFooterType . FOOTER_PRIMARY , new Paragraph (doc, " Footer Text" ));
106
104
```
107
105
108
106
### Headers and Footers
109
107
110
108
Headers and footers provide consistent information across document pages. You can add different content to primary, first-page, and even odd/even headers and footers.
111
109
112
110
``` java
113
- // Adding content to primary header
114
- HeaderFooter primaryHeader = pageSetup . getHeaderFooter( HeaderFooterType . HEADER_PRIMARY );
115
- Paragraph headerPara = new Paragraph (doc, " This is the header text. " );
116
- primaryHeader . appendChild(headerPara );
117
-
118
- // Adding content to primary footer
119
- HeaderFooter primaryFooter = pageSetup . getHeaderFooter( HeaderFooterType . FOOTER_PRIMARY );
120
- Paragraph footerPara = new Paragraph (doc, " Page number : " );
121
- FieldPage fieldPage = new FieldPage ( );
122
- footerPara . appendChild(fieldPage);
123
- primaryFooter . appendChild(footerPara );
111
+ Document doc = new Document ();
112
+ DocumentBuilder builder = new DocumentBuilder (doc );
113
+
114
+ builder . moveToHeaderFooter( HeaderFooterType . HEADER_PRIMARY );
115
+ builder . write( " Header Text " );
116
+ builder . moveToHeaderFooter( HeaderFooterType . FOOTER_PRIMARY );
117
+
118
+ builder . write( " Page Number : " );
119
+ builder . insertField( FieldType . FIELD_PAGE , true );
120
+
121
+ doc . save( " HeaderFooterDocument.docx " );
124
122
```
125
123
126
124
## Rendering Documents
@@ -133,13 +131,13 @@ To render a document, you need to use the Document class's save method and speci
133
131
134
132
``` java
135
133
// Render to PDF
136
- doc. save(" output.pdf" , SaveFormat . PDF );
134
+ doc. save(" output.pdf" );
137
135
138
136
// Render to XPS
139
- doc. save(" output.xps" , SaveFormat . XPS );
137
+ doc. save(" output.xps" );
140
138
141
139
// Render to images
142
- ImageSaveOptions saveOptions = new ImageSaveOptions (SaveFormat . PNG );
140
+ ImageSaveOptions saveOptions = new ImageSaveOptions ();
143
141
saveOptions. setResolution(300 );
144
142
doc. save(" output.png" , saveOptions);
145
143
```
@@ -161,7 +159,7 @@ When rendering documents to image formats, you can control the image quality to
161
159
162
160
``` java
163
161
// Set image options
164
- ImageSaveOptions imageOptions = new ImageSaveOptions (SaveFormat . PNG );
162
+ ImageSaveOptions imageOptions = new ImageSaveOptions ();
165
163
imageOptions. setResolution(300 );
166
164
imageOptions. setPrettyFormat(true );
167
165
doc. save(" output.png" , imageOptions);
@@ -179,7 +177,7 @@ You can render specific pages of a document, allowing you to display specific se
179
177
// Render specific page range
180
178
int startPage = 3 ;
181
179
int endPage = 5 ;
182
- ImageSaveOptions saveOptions = new ImageSaveOptions (SaveFormat . PNG );
180
+ ImageSaveOptions saveOptions = new ImageSaveOptions ();
183
181
saveOptions. setPageSet(new PageSet (startPage, endPage));
184
182
doc. save(" output.png" , saveOptions);
185
183
```
@@ -191,7 +189,7 @@ If you want to render only specific parts of a document, such as paragraphs or s
191
189
``` java
192
190
// Render specific paragraphs
193
191
int [] paragraphIndices = {0 , 2 , 4 };
194
- ImageSaveOptions saveOptions = new ImageSaveOptions (SaveFormat . PNG );
192
+ ImageSaveOptions saveOptions = new ImageSaveOptions ();
195
193
saveOptions. setPageSet(new PageSet (paragraphIndices));
196
194
doc. save(" output.png" , saveOptions);
197
195
```
@@ -203,7 +201,7 @@ For more granular control, you can render individual document elements like tabl
203
201
``` java
204
202
// Render specific table
205
203
int tableIndex = 1 ;
206
- ImageSaveOptions saveOptions = new ImageSaveOptions (SaveFormat . PNG );
204
+ ImageSaveOptions saveOptions = new ImageSaveOptions ();
207
205
saveOptions. setPageSet(new PageSet (tableIndex));
208
206
doc. save(" output.png" , saveOptions);
209
207
```
0 commit comments