[BAEL-16003] Moved missing docx test. Removed remaining docx folders. modified libraries & libraries-data README.md files.Added libraries-data-2 to project pom.
This commit is contained in:
		
							parent
							
								
									98f59f58a2
								
							
						
					
					
						commit
						b72e8b3f1d
					
				
							
								
								
									
										
											BIN
										
									
								
								libraries-data-2/src/main/resources/image.jpg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								libraries-data-2/src/main/resources/image.jpg
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| After Width: | Height: | Size: 69 KiB | 
| @ -1,19 +1,19 @@ | |||||||
| package com.baeldung.docx; | package com.baeldung.docx; | ||||||
| 
 | 
 | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| 
 | 
 | ||||||
| import static org.junit.Assert.assertTrue; | import static org.junit.Assert.assertTrue; | ||||||
| 
 | 
 | ||||||
| public class Docx4jReadAndWriteIntegrationTest { | public class Docx4jReadAndWriteIntegrationTest { | ||||||
| 
 | 
 | ||||||
|     private static final String imagePath = "src/main/resources/image.jpg"; |     private static final String imagePath = "src/main/resources/image.jpg"; | ||||||
|     private static final String outputPath = "helloWorld.docx"; |     private static final String outputPath = "helloWorld.docx"; | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenWordPackage_whenTextExist_thenReturnTrue() throws Exception { |     public void givenWordPackage_whenTextExist_thenReturnTrue() throws Exception { | ||||||
|         Docx4jExample docx4j = new Docx4jExample(); |         Docx4jExample docx4j = new Docx4jExample(); | ||||||
|         docx4j.createDocumentPackage(outputPath, imagePath); |         docx4j.createDocumentPackage(outputPath, imagePath); | ||||||
|         assertTrue(docx4j.isTextExist("Hello World!")); |         assertTrue(docx4j.isTextExist("Hello World!")); | ||||||
|         assertTrue(!docx4j.isTextExist("InexistantText")); |         assertTrue(!docx4j.isTextExist("InexistantText")); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -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<Object> rows = tbl.getContent(); |  | ||||||
|         for (Object row : rows) { |  | ||||||
|             Tr tr = (Tr) row; |  | ||||||
|             List<Object> 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<Object> 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; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user