Skip to content

Commit 6276451

Browse files
Update _index.md
1 parent a22f79e commit 6276451

File tree

1 file changed

+61
-82
lines changed
  • content/english/java/document-styling/styling-paragraphs-text

1 file changed

+61
-82
lines changed

content/english/java/document-styling/styling-paragraphs-text/_index.md

+61-82
Original file line numberDiff line numberDiff line change
@@ -182,25 +182,11 @@ In this example, we set the alignment of the paragraph to
182182
Creating lists with bullets or numbering is a common document formatting task. Aspose.Words for Java makes it straightforward. Here's how to create a bulleted list:
183183

184184
```java
185-
// Create a new document
186-
Document doc = new Document();
187-
188-
// Create a list
189-
List list = new List(doc);
190-
191-
// Add list items with bullets
192-
list.getListFormat().setListType(ListTemplateType.BULLET_DEFAULT);
193-
list.getListFormat().setListLevelNumber(0);
194-
195-
list.appendChild(new ListItem(doc, "Item 1"));
196-
list.appendChild(new ListItem(doc, "Item 2"));
197-
list.appendChild(new ListItem(doc, "Item 3"));
198-
199-
// Add the list to the document
200-
doc.getFirstSection().getBody().appendChild(list);
201-
202-
// Save the document
203-
doc.save("BulletedListDocument.docx");
185+
List list = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
186+
builder.getListFormat().setList(list);
187+
builder.writeln("Item 1");
188+
builder.writeln("Item 2");
189+
builder.writeln("Item 3");
204190
```
205191

206192
In this code, we create a bulleted list with three items.
@@ -210,24 +196,21 @@ In this code, we create a bulleted list with three items.
210196
Hyperlinks are essential for adding interactivity to your documents. Aspose.Words for Java allows you to insert hyperlinks easily. Here's an example:
211197

212198
```java
213-
// Create a new document
214199
Document doc = new Document();
200+
DocumentBuilder builder = new DocumentBuilder(doc);
215201

216-
// Create a paragraph
217-
Paragraph para = new Paragraph(doc);
218-
219-
// Create a hyperlink
220-
Hyperlink link = new Hyperlink(doc);
221-
link.setAddress("https://www.example.com");
222-
link.appendChild(new Run(doc, "Visit Example.com"));
223-
224-
para.appendChild(link);
202+
builder.write("For more information, please visit the ");
225203

226-
// Add the paragraph to the document
227-
doc.getFirstSection().getBody().appendChild(para);
204+
// Insert a hyperlink and emphasize it with custom formatting.
205+
// The hyperlink will be a clickable piece of text which will take us to the location specified in the URL.
206+
builder.getFont().setColor(Color.BLUE);
207+
builder.getFont().setUnderline(Underline.SINGLE);
208+
builder.insertHyperlink("Google website", "https://www.google.com", false);
209+
builder.getFont().clearFormatting();
210+
builder.writeln(".");
228211

229-
// Save the document
230-
doc.save("HyperlinkDocument.docx");
212+
// Ctrl + left clicking the link in the text in Microsoft Word will take us to the URL via a new web browser window.
213+
doc.save("InsertHyperlink.docx");
231214
```
232215

233216
This code inserts a hyperlink to "https://www.example.com" with the text "Visit Example.com."
@@ -237,23 +220,7 @@ This code inserts a hyperlink to "https://www.example.com" with the text "Visit
237220
Documents often require visual elements like images and shapes. Aspose.Words for Java enables you to insert images and shapes seamlessly. Here's how to add an image:
238221

239222
```java
240-
// Create a new document
241-
Document doc = new Document();
242-
243-
// Create a paragraph
244-
Paragraph para = new Paragraph(doc);
245-
246-
// Load an image from a file
247-
Shape image = new Shape(doc, ShapeType.IMAGE);
248-
image.getImageData().setImage("path/to/your/image.png");
249-
250-
para.appendChild(image);
251-
252-
// Add the paragraph to the document
253-
doc.getFirstSection().getBody().appendChild(para);
254-
255-
// Save the document
256-
doc.save("ImageDocument.docx");
223+
builder.insertImage("path/to/your/image.png");
257224
```
258225

259226
In this code, we load an image from a file and insert it into the document.
@@ -287,27 +254,20 @@ In this example, we set equal margins of 1 inch on all sides of the page.
287254
Headers and footers are essential for adding consistent information to each page of your document. Here's how to work with headers and footers:
288255

289256
```java
290-
// Create a new document
291257
Document doc = new Document();
258+
DocumentBuilder builder = new DocumentBuilder(doc);
292259

293-
// Access the header and footer of the first section
294-
HeaderFooter header = doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
295-
HeaderFooter footer = doc.getFirstSection().getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);
296-
297-
// Add content to the header
298-
Run headerRun = new Run(doc, "Header Text");
299-
header.appendChild(headerRun);
260+
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
261+
builder.write("Header Text");
262+
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
300263

301-
// Add content to the footer
302-
Run footerRun = new Run(doc, "Page Number: ");
303-
footer.appendChild(footerRun);
304-
Field pageField = new Field(doc, FieldType.FIELD_PAGE);
305-
footer.appendChild(pageField);
264+
builder.write("Page Number: ");
265+
builder.insertField(FieldType.FIELD_PAGE, true);
306266

307-
// Add content to the document body
267+
// Add content to the document body.
308268
// ...
309269

310-
// Save the document
270+
// Save the document.
311271
doc.save("HeaderFooterDocument.docx");
312272
```
313273

@@ -318,26 +278,45 @@ In this code, we add content to both the header and footer of the document.
318278
Tables are a powerful way to organize and present data in your documents. Aspose.Words for Java provides extensive support for working with tables. Here's an example of creating a table:
319279

320280
```java
321-
// Create a new document
322281
Document doc = new Document();
282+
DocumentBuilder builder = new DocumentBuilder(doc);
323283

324-
// Create a table with 3 rows and 3 columns
325-
Table table = new Table(doc);
326-
table.ensureMinimum();
327-
table.getRows().add(new Row(doc));
328-
table.getRows().add(new Row(doc));
329-
table.getRows().add(new Row(doc));
284+
builder.startTable();
330285

331-
// Add content to the table cells
332-
table.getFirstRow().getCells().get(0).appendChild(new Paragraph(doc, "Row 1, Cell 1"));
333-
table.getFirstRow().getCells().get(1).appendChild(new Paragraph(doc, "Row 1, Cell 2"));
334-
table.getFirstRow().getCells().get(2).appendChild(new Paragraph(doc, "Row 1, Cell 3"));
286+
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
335287

336-
// Add the table to the document
337-
doc.getFirstSection().getBody().appendChild(table);
288+
builder.insertCell();
289+
builder.write("Row 1, Col 1");
338290

339-
// Save the document
340-
doc.save("TableDocument.docx");
291+
builder.insertCell();
292+
builder.write("Row 1, Col 2");
293+
builder.endRow();
294+
295+
// Changing the formatting will apply it to the current cell,
296+
// and any new cells that we create with the builder afterward.
297+
// This will not affect the cells that we have added previously.
298+
builder.getCellFormat().getShading().clearFormatting();
299+
300+
builder.insertCell();
301+
builder.write("Row 2, Col 1");
302+
303+
builder.insertCell();
304+
builder.write("Row 2, Col 2");
305+
306+
builder.endRow();
307+
308+
// Increase row height to fit the vertical text.
309+
builder.insertCell();
310+
builder.getRowFormat().setHeight(150.0);
311+
builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
312+
builder.write("Row 3, Col 1");
313+
314+
builder.insertCell();
315+
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
316+
builder.write("Row 3, Col 2");
317+
318+
builder.endRow();
319+
builder.endTable();
341320
```
342321

343322
In this code, we create a simple table with three rows and three columns.
@@ -354,7 +333,7 @@ Document doc = new Document();
354333
// ...
355334

356335
// Save the document as a PDF
357-
doc.save("Document.pdf", SaveFormat.PDF);
336+
doc.save("Document.pdf");
358337
```
359338

360339
This code snippet saves the document as a PDF file.
@@ -393,7 +372,7 @@ Yes, you can easily convert a document to PDF using Aspose.Words for Java. Here'
393372

394373
```java
395374
Document doc = new Document("input.docx");
396-
doc.save("output.pdf", SaveFormat.PDF);
375+
doc.save("output.pdf");
397376
```
398377

399378
### How do I format text as

0 commit comments

Comments
 (0)