From 1a84b476e81f167922db249c44768f7fb98f1dee Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 13 Aug 2015 20:23:58 +0000 Subject: [PATCH] #58237 When adding a picture to a XWPF header or footer, attach it to the right part git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1695772 13f79535-47bb-0310-9956-ffa450edef68 --- .../xwpf/model/XWPFHeaderFooterPolicy.java | 4 ++++ .../apache/poi/xwpf/usermodel/XWPFRun.java | 22 ++++++++++++++----- .../xwpf/usermodel/TestXWPFPictureData.java | 16 +++++--------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java index 4a381de9eb..8c0d2e63f4 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java +++ b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java @@ -166,7 +166,9 @@ public class XWPFHeaderFooterPolicy { String pStyle = "Header"; int i = getRelationIndex(relation); HdrDocument hdrDoc = HdrDocument.Factory.newInstance(); + XWPFHeader wrapper = (XWPFHeader) doc.createRelationship(relation, XWPFFactory.getInstance(), i); + wrapper.setXWPFDocument(doc); CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars); wrapper.setHeaderFooter(hdr); @@ -201,7 +203,9 @@ public class XWPFHeaderFooterPolicy { String pStyle = "Footer"; int i = getRelationIndex(relation); FtrDocument ftrDoc = FtrDocument.Factory.newInstance(); + XWPFFooter wrapper = (XWPFFooter) doc.createRelationship(relation, XWPFFactory.getInstance(), i); + wrapper.setXWPFDocument(doc); CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars); wrapper.setHeaderFooter(ftr); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index 1c14bb274a..b5e465dcb7 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -915,12 +915,22 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB */ public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height) - throws InvalidFormatException, IOException { - XWPFDocument doc = parent.getDocument(); - - // Add the picture + relationship - String relationId = doc.addPictureData(pictureData, pictureType); - XWPFPictureData picData = (XWPFPictureData) doc.getRelationById(relationId); + throws InvalidFormatException, IOException { + String relationId; + XWPFPictureData picData; + + // Work out what to add the picture to, then add both the + // picture and the relationship for it + // TODO Should we have an interface for this sort of thing? + if (parent.getPart() instanceof XWPFHeaderFooter) { + XWPFHeaderFooter headerFooter = (XWPFHeaderFooter)parent.getPart(); + relationId = headerFooter.addPictureData(pictureData, pictureType); + picData = (XWPFPictureData) headerFooter.getRelationById(relationId); + } else { + XWPFDocument doc = parent.getDocument(); + relationId = doc.addPictureData(pictureData, pictureType); + picData = (XWPFPictureData) doc.getRelationById(relationId); + } // Create the drawing entry for it try { diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java index 0b29956c67..02ab71adcf 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -30,7 +30,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; public class TestXWPFPictureData extends TestCase { @@ -64,7 +63,7 @@ public class TestXWPFPictureData extends TestCase { verifyOneHeaderPicture(readBack); } - public void FIXMEtestCreateHeaderPicture() throws Exception { // TODO Fix + public void testCreateHeaderPicture() throws Exception { XWPFDocument doc = new XWPFDocument(); // Starts with no header @@ -73,16 +72,13 @@ public class TestXWPFPictureData extends TestCase { // Add a default header policy = doc.createHeaderFooterPolicy(); - - XWPFParagraph[] hparas = new XWPFParagraph[] { - new XWPFParagraph(CTP.Factory.newInstance(), doc) - }; - hparas[0].createRun().setText("Header Hello World!"); - XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, hparas); + XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT); + header.getParagraphs().get(0).createRun().setText("Hello, Header World!"); + header.createParagraph().createRun().setText("Paragraph 2"); assertEquals(0, header.getAllPictures().size()); - assertEquals(1, header.getParagraphs().size()); + assertEquals(2, header.getParagraphs().size()); - // Add a picture to it + // Add a picture to the first paragraph header.getParagraphs().get(0).getRuns().get(0).addPicture( new ByteArrayInputStream(new byte[] {1,2,3,4}), Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2); -- 2.39.5