poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1695772 - in /poi/trunk/src/ooxml: java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java java/org/apache/poi/xwpf/usermodel/XWPFRun.java testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java
Date Thu, 13 Aug 2015 20:23:59 GMT
Author: nick
Date: Thu Aug 13 20:23:58 2015
New Revision: 1695772

URL: http://svn.apache.org/r1695772
Log:
#58237 When adding a picture to a XWPF header or footer, attach it to the right part

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
    poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java?rev=1695772&r1=1695771&r2=1695772&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java Thu Aug
13 20:23:58 2015
@@ -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);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=1695772&r1=1695771&r2=1695772&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Thu Aug 13 20:23:58
2015
@@ -915,12 +915,22 @@ public class XWPFRun implements ISDTCont
      * @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 {

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java?rev=1695772&r1=1695771&r2=1695772&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java Thu
Aug 13 20:23:58 2015
@@ -30,7 +30,6 @@ import org.apache.poi.openxml4j.opc.Pack
 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
         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
         
         // 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);



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message