11package com .baeldung .docx ;
22
3- import java .io .File ;
4- import java .nio .file .Files ;
5- import java .util .List ;
6-
7- import javax .xml .bind .JAXBElement ;
8- import javax .xml .bind .JAXBException ;
9-
103import org .docx4j .dml .wordprocessingDrawing .Inline ;
114import org .docx4j .jaxb .Context ;
125import org .docx4j .model .table .TblFactory ;
2619import org .docx4j .wml .Text ;
2720import org .docx4j .wml .Tr ;
2821
29- public class Docx4jExample {
22+ import javax .xml .bind .JAXBElement ;
23+ import javax .xml .bind .JAXBException ;
24+ import java .io .File ;
25+ import java .nio .file .Files ;
26+ import java .util .List ;
27+
28+ class Docx4jExample {
3029
31- public void createDocumentPackage (String outputPath , String imagePath ) throws Exception {
30+ void createDocumentPackage (String outputPath , String imagePath ) throws Exception {
3231 WordprocessingMLPackage wordPackage = WordprocessingMLPackage .createPackage ();
3332 MainDocumentPart mainDocumentPart = wordPackage .getMainDocumentPart ();
3433 mainDocumentPart .addStyledParagraphOfText ("Title" , "Hello World!" );
3534 mainDocumentPart .addParagraphOfText ("Welcome To Baeldung!" );
35+
3636 ObjectFactory factory = Context .getWmlObjectFactory ();
3737 P p = factory .createP ();
3838 R r = factory .createR ();
@@ -53,13 +53,16 @@ public void createDocumentPackage(String outputPath, String imagePath) throws Ex
5353
5454 File image = new File (imagePath );
5555 byte [] fileContent = Files .readAllBytes (image .toPath ());
56- BinaryPartAbstractImage imagePart = BinaryPartAbstractImage .createImagePart (wordPackage , fileContent );
57- Inline inline = imagePart .createImageInline ("Baeldung Image" , "Alt Text" , 1 , 2 , false );
56+ BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
57+ .createImagePart (wordPackage , fileContent );
58+ Inline inline = imagePart .createImageInline (
59+ "Baeldung Image" , "Alt Text" , 1 , 2 , false );
5860 P Imageparagraph = addImageToParagraph (inline );
5961 mainDocumentPart .getContent ().add (Imageparagraph );
6062
61- int writableWidthTwips = wordPackage .getDocumentModel ().getSections ().get (0 ).getPageDimensions ()
62- .getWritableWidthTwips ();
63+ int writableWidthTwips = wordPackage .getDocumentModel ()
64+ .getSections ().get (0 ).getPageDimensions ()
65+ .getWritableWidthTwips ();
6366 int columnNumber = 3 ;
6467 Tbl tbl = TblFactory .createTable (3 , 3 , writableWidthTwips / columnNumber );
6568 List <Object > rows = tbl .getContent ();
@@ -77,7 +80,7 @@ public void createDocumentPackage(String outputPath, String imagePath) throws Ex
7780 wordPackage .save (exportFile );
7881 }
7982
80- public boolean isTextExist (String testText ) throws Docx4JException , JAXBException {
83+ boolean isTextExist (String testText ) throws Docx4JException , JAXBException {
8184 File doc = new File ("helloWorld.docx" );
8285 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage .load (doc );
8386 MainDocumentPart mainDocumentPart = wordMLPackage .getMainDocumentPart ();
0 commit comments