Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert images into document with shapes #458

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions poi-ooxml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ dependencies {
testImplementation project(path:':poi', configuration:'tests')
testImplementation project(path:':poi-ooxml-lite-agent', configuration: 'archives')
testRuntimeOnly "org.apiguardian:apiguardian-api:${apiGuardianVersion}"
testRuntimeOnly("net.sf.saxon:Saxon-HE:${saxonVersion}") {
exclude group: 'xml-apis', module: 'xml-apis'
}
testImplementation 'org.xmlunit:xmlunit-core:2.9.1'
testImplementation 'org.reflections:reflections:0.10.2'
testImplementation 'org.openjdk.jmh:jmh-core:1.36'
Expand All @@ -134,6 +137,7 @@ dependencies {

javadocs project(':poi')
javadocs project(':poi-scratchpad')

}

final String MODULE_NAME = 'org.apache.poi.ooxml'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.xwpf.usermodel;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -25,13 +26,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.collections4.ListUtils;
import org.apache.poi.POIDataSamples;
import org.apache.poi.common.usermodel.PictureType;
import org.apache.poi.ooxml.POIXMLDocumentPart;
Expand All @@ -42,11 +46,18 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;

Expand All @@ -55,7 +66,7 @@ public final class TestXWPFDocument {
@Test
void testContainsMainContentType() throws Exception {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
OPCPackage pack = doc.getPackage()) {
OPCPackage pack = doc.getPackage()) {
String ct = XWPFRelation.DOCUMENT.getContentType();
boolean found = pack.getParts().stream().anyMatch(p -> ct.equals(p.getContentType()));
assertTrue(found);
Expand Down Expand Up @@ -480,20 +491,117 @@ void testInsertNewParagraphWithSdt() throws IOException {
}
}

@ParameterizedTest(name = "insert {0} pictures into document with {1} shapes")
@MethodSource("documentsAndNumberOfPictures")
void testInsertImagesIntoDocumentWithExistingShapes(int numberOfPictures, int numberOfShapes) throws Exception {
String pathToDocument = String.format("/bugfixing/shape in footer/document with shapes %d.docx", numberOfShapes);

InputStream docxContents = getClass().getResourceAsStream(pathToDocument);
XWPFDocument document = new XWPFDocument(docxContents);
workaround(document);

IntStream.rangeClosed(1, numberOfPictures).forEach(
i -> addImage(document.createParagraph(), "/bugfixing/" + i + ".png"));

/*
document.write(Files.newOutputStream(new File(
System.getProperty("user.home"),
String.format("/Desktop/out/%d_%d-out.docx", numberOfPictures, numberOfShapes)).toPath()));
*/

Set<String> docPrIds = new HashSet<>();
XWPFParagraph p = document.getParagraphs().get(0);
XmlCursor paragraphCursor = p.getCTP().newCursor();
paragraphCursor.selectPath("//*:docPr");
while (paragraphCursor.hasNextSelection()) {
paragraphCursor.toNextSelection();
paragraphCursor.toFirstAttribute();
boolean newItem = docPrIds.add(paragraphCursor.getTextValue());
assertTrue(newItem);
}
}

private XWPFDocument workaround(XWPFDocument document) {
try {
for (XWPFParagraph paragraph : bodyAndFooterParagraphs(document)) {
for (XWPFRun run : paragraph.getRuns()) {
XmlCursor cursor = run.getCTR().newCursor();
cursor.selectPath(
"declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' " +
"declare namespace mc='http://schemas.openxmlformats.org/markup-compatibility/2006' " +
"declare namespace wp='http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' " +
".//wp:docPr");

while (cursor.hasNextSelection()) {
cursor.toNextSelection();
XmlObject obj = cursor.getObject();

CTNonVisualDrawingProps docPr = CTNonVisualDrawingProps.Factory.parse(obj.xmlText());

document.getDrawingIdManager().reserve(docPr.getId());
}
}
}

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
document.write(outStream);
byte[] byteArray = outStream.toByteArray();
return new XWPFDocument(new ByteArrayInputStream(byteArray));

} catch (XmlException | IOException e) {
throw new RuntimeException(e);
}
}

private List<XWPFParagraph> bodyAndFooterParagraphs(XWPFDocument document) {

List<XWPFParagraph> footerParagraphs = document.getFooterList().stream()
.flatMap(f -> f.getParagraphs().stream())
.collect(Collectors.toList());

return ListUtils.union(document.getParagraphs(), footerParagraphs);
}

private void addImage(XWPFParagraph paragraph, String path) {
XWPFRun run = paragraph.getRuns().stream().findFirst().orElse(paragraph.createRun());

try {
run.addPicture(
getClass().getResourceAsStream(path),
Document.PICTURE_TYPE_PNG,
"some image.png",
10 * Units.EMU_PER_PIXEL,
10 * Units.EMU_PER_PIXEL
);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


public static List<Arguments> documentsAndNumberOfPictures() {
return IntStream.rangeClosed(0, 10)
.boxed()
.flatMap(files ->
IntStream.rangeClosed(0, 10)
.mapToObj(shapes -> arguments(files, shapes)))
.collect(Collectors.toList());
}

@Test
@Disabled("XWPF should be able to write to a new Stream when opened Read-Only")
void testWriteFromReadOnlyOPC() throws Exception {
try (OPCPackage opc = OPCPackage.open(
POIDataSamples.getDocumentInstance().getFile("SampleDoc.docx"),
PackageAccess.READ
);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ext = new XWPFWordExtractor(doc)
);
XWPFDocument doc = new XWPFDocument(opc);
XWPFWordExtractor ext = new XWPFWordExtractor(doc)
) {
final String origText = ext.getText();

try (XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);
XWPFWordExtractor ext2 = new XWPFWordExtractor(doc2)) {
XWPFWordExtractor ext2 = new XWPFWordExtractor(doc2)) {
assertEquals(origText, ext2.getText());
}
}
Expand Down
Binary file added poi-ooxml/src/test/resources/.DS_Store
Binary file not shown.
Binary file added poi-ooxml/src/test/resources/bugfixing/0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added poi-ooxml/src/test/resources/bugfixing/9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.