diff --git a/libraries-data-2/src/main/resources/image.jpg b/libraries-data-2/src/main/resources/image.jpg new file mode 100644 index 0000000000..e2554a0d9c Binary files /dev/null and b/libraries-data-2/src/main/resources/image.jpg differ diff --git a/libraries/src/test/java/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java b/libraries-data-2/src/test/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java similarity index 97% rename from libraries/src/test/java/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java rename to libraries-data-2/src/test/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java index b6e157beea..9a2691d3f0 100644 --- a/libraries/src/test/java/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java +++ b/libraries-data-2/src/test/com/baeldung/docx/Docx4jReadAndWriteIntegrationTest.java @@ -1,19 +1,19 @@ -package com.baeldung.docx; - -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -public class Docx4jReadAndWriteIntegrationTest { - - private static final String imagePath = "src/main/resources/image.jpg"; - private static final String outputPath = "helloWorld.docx"; - - @Test - public void givenWordPackage_whenTextExist_thenReturnTrue() throws Exception { - Docx4jExample docx4j = new Docx4jExample(); - docx4j.createDocumentPackage(outputPath, imagePath); - assertTrue(docx4j.isTextExist("Hello World!")); - assertTrue(!docx4j.isTextExist("InexistantText")); - } -} +package com.baeldung.docx; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class Docx4jReadAndWriteIntegrationTest { + + private static final String imagePath = "src/main/resources/image.jpg"; + private static final String outputPath = "helloWorld.docx"; + + @Test + public void givenWordPackage_whenTextExist_thenReturnTrue() throws Exception { + Docx4jExample docx4j = new Docx4jExample(); + docx4j.createDocumentPackage(outputPath, imagePath); + assertTrue(docx4j.isTextExist("Hello World!")); + assertTrue(!docx4j.isTextExist("InexistantText")); + } +} diff --git a/libraries-data/README.md b/libraries-data/README.md index 077961f887..1ad7e94a1f 100644 --- a/libraries-data/README.md +++ b/libraries-data/README.md @@ -11,7 +11,6 @@ - [Apache Ignite with Spring Data](http://www.baeldung.com/apache-ignite-spring-data) - [Guide to JMapper](https://www.baeldung.com/jmapper) - [A Guide to Apache Crunch](https://www.baeldung.com/apache-crunch) -- [Building a Data Pipeline with Flink and Kafka](https://www.baeldung.com/kafka-flink-data-pipeline) - [Intro to Apache Storm](https://www.baeldung.com/apache-storm) - [Guide to Ebean ORM](https://www.baeldung.com/ebean-orm) - [Introduction to Kafka Connectors](https://www.baeldung.com/kafka-connectors-guide) diff --git a/libraries/README.md b/libraries/README.md index 649e83177b..375863a5ab 100644 --- a/libraries/README.md +++ b/libraries/README.md @@ -15,7 +15,6 @@ - [Serenity BDD with Spring and JBehave](http://www.baeldung.com/serenity-spring-jbehave) - [Locality-Sensitive Hashing in Java Using Java-LSH](http://www.baeldung.com/locality-sensitive-hashing) - [Introduction to Awaitlity](http://www.baeldung.com/awaitlity-testing) -- [Guide to the HyperLogLog Algorithm](http://www.baeldung.com/java-hyperloglog) - [Introduction to Neuroph](http://www.baeldung.com/neuroph) - [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss) - [Introduction to PCollections](http://www.baeldung.com/java-pcollections) @@ -23,8 +22,6 @@ - [Introduction to Eclipse Collections](http://www.baeldung.com/eclipse-collections) - [DistinctBy in the Java Stream API](http://www.baeldung.com/java-streams-distinct-by) - [Introduction to NoException](http://www.baeldung.com/no-exception) -- [Introduction to Conflict-Free Replicated Data Types](http://www.baeldung.com/java-conflict-free-replicated-data-types) -- [Introduction to javax.measure](http://www.baeldung.com/javax-measure) - [Spring Yarg Integration](http://www.baeldung.com/spring-yarg) - [Delete a Directory Recursively in Java](http://www.baeldung.com/java-delete-directory) - [Guide to JDeferred](http://www.baeldung.com/jdeferred) @@ -33,15 +30,10 @@ - [Introduction to Retrofit](http://www.baeldung.com/retrofit) - [Using Pairs in Java](http://www.baeldung.com/java-pairs) - [Introduction to Caffeine](http://www.baeldung.com/java-caching-caffeine) -- [Introduction To Docx4J](http://www.baeldung.com/docx4j) - [Introduction to StreamEx](http://www.baeldung.com/streamex) - [Introduction to BouncyCastle with Java](http://www.baeldung.com/java-bouncy-castle) -- [Interact with Google Sheets from Java](http://www.baeldung.com/google-sheets-java-client) - [A Docker Guide for Java](http://www.baeldung.com/docker-java-api) -- [Introduction To OpenCSV](http://www.baeldung.com/opencsv) - [Introduction to Akka Actors in Java](http://www.baeldung.com/akka-actors-java) -- [Introduction to Smooks](http://www.baeldung.com/smooks) -- [A Guide to Infinispan in Java](http://www.baeldung.com/infinispan) - [A Guide to Unirest](http://www.baeldung.com/unirest) - [Introduction to Akka Actors in Java](http://www.baeldung.com/akka-actors-java) - [A Guide to Byte Buddy](http://www.baeldung.com/byte-buddy) diff --git a/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java b/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java deleted file mode 100644 index 97fbf4adc7..0000000000 --- a/libraries/src/main/java/com/baeldung/docx/Docx4jExample.java +++ /dev/null @@ -1,105 +0,0 @@ -package com.baeldung.docx; - -import org.docx4j.dml.wordprocessingDrawing.Inline; -import org.docx4j.jaxb.Context; -import org.docx4j.model.table.TblFactory; -import org.docx4j.openpackaging.exceptions.Docx4JException; -import org.docx4j.openpackaging.packages.WordprocessingMLPackage; -import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage; -import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; -import org.docx4j.wml.BooleanDefaultTrue; -import org.docx4j.wml.Color; -import org.docx4j.wml.Drawing; -import org.docx4j.wml.ObjectFactory; -import org.docx4j.wml.P; -import org.docx4j.wml.R; -import org.docx4j.wml.RPr; -import org.docx4j.wml.Tbl; -import org.docx4j.wml.Tc; -import org.docx4j.wml.Text; -import org.docx4j.wml.Tr; - -import javax.xml.bind.JAXBElement; -import javax.xml.bind.JAXBException; -import java.io.File; -import java.nio.file.Files; -import java.util.List; - -class Docx4jExample { - - void createDocumentPackage(String outputPath, String imagePath) throws Exception { - WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage(); - MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); - mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!"); - mainDocumentPart.addParagraphOfText("Welcome To Baeldung!"); - - ObjectFactory factory = Context.getWmlObjectFactory(); - P p = factory.createP(); - R r = factory.createR(); - Text t = factory.createText(); - t.setValue("Welcome To Baeldung"); - r.getContent().add(t); - p.getContent().add(r); - RPr rpr = factory.createRPr(); - BooleanDefaultTrue b = new BooleanDefaultTrue(); - rpr.setB(b); - rpr.setI(b); - rpr.setCaps(b); - Color red = factory.createColor(); - red.setVal("green"); - rpr.setColor(red); - r.setRPr(rpr); - mainDocumentPart.getContent().add(p); - - File image = new File(imagePath); - byte[] fileContent = Files.readAllBytes(image.toPath()); - BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent); - Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false); - P Imageparagraph = addImageToParagraph(inline); - mainDocumentPart.getContent().add(Imageparagraph); - - int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips(); - int columnNumber = 3; - Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber); - List rows = tbl.getContent(); - for (Object row : rows) { - Tr tr = (Tr) row; - List cells = tr.getContent(); - for (Object cell : cells) { - Tc td = (Tc) cell; - td.getContent().add(p); - } - } - - mainDocumentPart.getContent().add(tbl); - File exportFile = new File(outputPath); - wordPackage.save(exportFile); - } - - boolean isTextExist(String testText) throws Docx4JException, JAXBException { - File doc = new File("helloWorld.docx"); - WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc); - MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); - String textNodesXPath = "//w:t"; - List paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true); - for (Object obj : paragraphs) { - Text text = (Text) ((JAXBElement) obj).getValue(); - String textValue = text.getValue(); - if (textValue != null && textValue.contains(testText)) { - return true; - } - } - return false; - } - - private static P addImageToParagraph(Inline inline) { - ObjectFactory factory = new ObjectFactory(); - P p = factory.createP(); - R r = factory.createR(); - p.getContent().add(r); - Drawing drawing = factory.createDrawing(); - r.getContent().add(drawing); - drawing.getAnchorOrInline().add(inline); - return p; - } -} diff --git a/pom.xml b/pom.xml index d095e8a7bc..eff74cfe70 100644 --- a/pom.xml +++ b/pom.xml @@ -503,6 +503,7 @@ libraries libraries-2 libraries-data + libraries-data-2 libraries-apache-commons libraries-primitive libraries-security @@ -1196,6 +1197,7 @@ libraries libraries-data + libraries-data-2 libraries-apache-commons libraries-security libraries-server