|
| 1 | +package com.baeldung.docx; |
| 2 | + |
| 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 | + |
| 10 | +import org.docx4j.dml.wordprocessingDrawing.Inline; |
| 11 | +import org.docx4j.jaxb.Context; |
| 12 | +import org.docx4j.model.table.TblFactory; |
| 13 | +import org.docx4j.openpackaging.exceptions.Docx4JException; |
| 14 | +import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
| 15 | +import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; |
| 16 | +import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
| 17 | +import org.docx4j.wml.BooleanDefaultTrue; |
| 18 | +import org.docx4j.wml.Color; |
| 19 | +import org.docx4j.wml.Drawing; |
| 20 | +import org.docx4j.wml.ObjectFactory; |
| 21 | +import org.docx4j.wml.P; |
| 22 | +import org.docx4j.wml.R; |
| 23 | +import org.docx4j.wml.RPr; |
| 24 | +import org.docx4j.wml.Tbl; |
| 25 | +import org.docx4j.wml.Tc; |
| 26 | +import org.docx4j.wml.Text; |
| 27 | +import org.docx4j.wml.Tr; |
| 28 | + |
| 29 | +public class Docx4jExample { |
| 30 | + |
| 31 | + public void createDocumentPackage(String outputPath, String imagePath) throws Exception { |
| 32 | + WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage(); |
| 33 | + MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); |
| 34 | + mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!"); |
| 35 | + mainDocumentPart.addParagraphOfText("Welcome To Baeldung!"); |
| 36 | + ObjectFactory factory = Context.getWmlObjectFactory(); |
| 37 | + P p = factory.createP(); |
| 38 | + R r = factory.createR(); |
| 39 | + Text t = factory.createText(); |
| 40 | + t.setValue("Welcome To Baeldung"); |
| 41 | + r.getContent().add(t); |
| 42 | + p.getContent().add(r); |
| 43 | + RPr rpr = factory.createRPr(); |
| 44 | + BooleanDefaultTrue b = new BooleanDefaultTrue(); |
| 45 | + rpr.setB(b); |
| 46 | + rpr.setI(b); |
| 47 | + rpr.setCaps(b); |
| 48 | + Color red = factory.createColor(); |
| 49 | + red.setVal("green"); |
| 50 | + rpr.setColor(red); |
| 51 | + r.setRPr(rpr); |
| 52 | + mainDocumentPart.getContent().add(p); |
| 53 | + |
| 54 | + File image = new File(imagePath); |
| 55 | + 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); |
| 58 | + P Imageparagraph = addImageToParagraph(inline); |
| 59 | + mainDocumentPart.getContent().add(Imageparagraph); |
| 60 | + |
| 61 | + int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions() |
| 62 | + .getWritableWidthTwips(); |
| 63 | + int columnNumber = 3; |
| 64 | + Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber); |
| 65 | + List<Object> rows = tbl.getContent(); |
| 66 | + for (Object row : rows) { |
| 67 | + Tr tr = (Tr) row; |
| 68 | + List<Object> cells = tr.getContent(); |
| 69 | + for (Object cell : cells) { |
| 70 | + Tc td = (Tc) cell; |
| 71 | + td.getContent().add(p); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + mainDocumentPart.getContent().add(tbl); |
| 76 | + File exportFile = new File(outputPath); |
| 77 | + wordPackage.save(exportFile); |
| 78 | + } |
| 79 | + |
| 80 | + public boolean isTextExist(String testText) throws Docx4JException, JAXBException { |
| 81 | + File doc = new File("helloWorld.docx"); |
| 82 | + WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc); |
| 83 | + MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); |
| 84 | + String textNodesXPath = "//w:t"; |
| 85 | + List<Object> paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true); |
| 86 | + for (Object obj : paragraphs) { |
| 87 | + Text text = (Text) ((JAXBElement) obj).getValue(); |
| 88 | + String textValue = text.getValue(); |
| 89 | + if (textValue != null && textValue.contains(testText)) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private static P addImageToParagraph(Inline inline) { |
| 97 | + ObjectFactory factory = new ObjectFactory(); |
| 98 | + P p = factory.createP(); |
| 99 | + R r = factory.createR(); |
| 100 | + p.getContent().add(r); |
| 101 | + Drawing drawing = factory.createDrawing(); |
| 102 | + r.getContent().add(drawing); |
| 103 | + drawing.getAnchorOrInline().add(inline); |
| 104 | + return p; |
| 105 | + } |
| 106 | +} |
0 commit comments