]> source.dussan.org Git - poi.git/commitdiff
XWPF: support for pictures in headers
authorMaxim Valyanskiy <maxcom@apache.org>
Mon, 21 Mar 2011 12:43:58 +0000 (12:43 +0000)
committerMaxim Valyanskiy <maxcom@apache.org>
Mon, 21 Mar 2011 12:43:58 +0000 (12:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1083764 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java
test-data/document/headerPic.docx [new file with mode: 0644]

index 3a5b77effd94336474883fd9e559168ecded9a45..65020e69351c2d1e2d723f48bf2f245a2ce53ecd 100644 (file)
@@ -107,9 +107,14 @@ public class XWPFHeaderFooterPolicy {
                        // Get the header
                        CTHdrFtrRef ref = sectPr.getHeaderReferenceArray(i);
                        PackagePart hdrPart = doc.getPartById(ref.getId());
-                       HdrDocument hdrDoc = HdrDocument.Factory.parse(hdrPart.getInputStream());
-                       CTHdrFtr hdrFtr = hdrDoc.getHdr();
-                       XWPFHeader hdr = new XWPFHeader(doc, hdrFtr);
+
+            XWPFHeader hdr = null;
+
+            for (POIXMLDocumentPart part : doc.getRelations()) {
+                if (part.getPackagePart().getPartName().equals(hdrPart.getPartName())) {
+                    hdr = (XWPFHeader) part;
+                }
+            }
 
                        // Assign it
                        Enum type = ref.getType();
index 07f59afffea3eafe24eee28d3095b821b021bf16..6ccbf462417ed7880700673e7b4d2a81ca75fd15 100644 (file)
@@ -133,7 +133,6 @@ public class XWPFHeader extends XWPFHeaderFooter {
                            }
                        }
                 cursor.dispose();
-                       getAllPictures();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
index eaeb78072009342989368addc16f7526138d0ee8..9f0a49f24bd8d3ddb938fa6ddeffc9e5c234a09f 100644 (file)
@@ -64,7 +64,7 @@ public class XWPFPicture {
      */
     public XWPFPictureData getPictureData(){
        String blipId = ctPic.getBlipFill().getBlip().getEmbed();
-       for(POIXMLDocumentPart part: paragraph.getDocument().getRelations()){
+        for(POIXMLDocumentPart part: ((POIXMLDocumentPart) paragraph.getPart()).getRelations()){
                  if(part.getPackageRelationship().getId().equals(blipId)){
                          return (XWPFPictureData)part;
                  }
index b46f8d32acca0a68704b3082cda492bb155e4a3d..738ca7af139b0b027d814115d941f57cdf3c91c4 100644 (file)
@@ -34,16 +34,22 @@ public final class TestXWPFHeader extends TestCase {
 
                XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
 
-
                XWPFHeader header = policy.getDefaultHeader();
                XWPFFooter footer = policy.getDefaultFooter();
                assertNotNull(header);
                assertNotNull(footer);
+       }
 
-               // TODO verify if the following is correct
-               assertNull(header.toString());
+    public void testImageInHeader() {
+        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
 
-       }
+        XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
+
+        XWPFHeader header = policy.getDefaultHeader();
+
+        assertNotNull(header.getRelations());
+        assertEquals(1, header.getRelations().size());
+    }
 
        public void testSetHeader() throws IOException {
                XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("SampleDoc.docx");
index b17af3644c592df91207709658cad121bcf8a2a5..c05e6201e1cf1d787f074f4ef53284c7666de523 100644 (file)
@@ -26,6 +26,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 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;
 
 public class TestXWPFPictureData extends TestCase {
           public void testRead(){
@@ -58,6 +59,16 @@ public class TestXWPFPictureData extends TestCase {
                        }
            }
 
+        public void testPictureInHeader() {
+            XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
+            XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
+
+            XWPFHeader header = policy.getDefaultHeader();
+
+            List<XWPFPictureData> pictures = header.getAllPictures();
+            assertEquals(1, pictures.size());
+        }
+
            public void testNew() throws Exception {
                XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
                byte[] jpegData = "test jpeg data".getBytes();
index 695f936a582150564be8785d09c95e0d14440d4b..dd0cad864aee0ec318dfbc82369abde62ca16e91 100644 (file)
 package org.apache.poi.xwpf.usermodel;
 
 import java.math.BigInteger;
+import java.util.List;
 
 import junit.framework.TestCase;
 
 import org.apache.poi.xwpf.XWPFTestDataSamples;
+import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
@@ -326,4 +328,27 @@ public class TestXWPFRun extends TestCase {
        assertEquals(false, run.isStrike());
        assertEquals(null, run.getCTR().getRPr());
     }
+
+    public void testPictureInHeader() {
+        XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("headerPic.docx");
+        XWPFHeaderFooterPolicy policy = sampleDoc.getHeaderFooterPolicy();
+
+        XWPFHeader header = policy.getDefaultHeader();
+
+        int count = 0;
+
+        for (XWPFParagraph p : header.getParagraphs()) {
+            for (XWPFRun r : p.getRuns()) {
+                List<XWPFPicture> pictures = r.getEmbeddedPictures();
+
+                for (XWPFPicture pic : pictures) {
+                    assertNotNull(pic.getPictureData());
+                }
+
+                count+= pictures.size();
+            }
+        }
+
+        assertEquals(1, count);
+    }
 }
diff --git a/test-data/document/headerPic.docx b/test-data/document/headerPic.docx
new file mode 100644 (file)
index 0000000..01072e5
Binary files /dev/null and b/test-data/document/headerPic.docx differ