From: Maxim Valyanskiy Date: Mon, 21 Mar 2011 12:43:58 +0000 (+0000) Subject: XWPF: support for pictures in headers X-Git-Tag: REL_3_8_BETA2~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=71b9455d886a52c7f0e1816a0a2e940ebf0a4f6a;p=poi.git XWPF: support for pictures in headers git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1083764 13f79535-47bb-0310-9956-ffa450edef68 --- 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 3a5b77effd..65020e6935 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java +++ b/src/ooxml/java/org/apache/poi/xwpf/model/XWPFHeaderFooterPolicy.java @@ -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(); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java index 07f59afffe..6ccbf46241 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java @@ -133,7 +133,6 @@ public class XWPFHeader extends XWPFHeaderFooter { } } cursor.dispose(); - getAllPictures(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java index eaeb780720..9f0a49f24b 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPicture.java @@ -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; } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java index b46f8d32ac..738ca7af13 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFHeader.java @@ -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"); 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 b17af3644c..c05e6201e1 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFPictureData.java @@ -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 pictures = header.getAllPictures(); + assertEquals(1, pictures.size()); + } + public void testNew() throws Exception { XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); byte[] jpegData = "test jpeg data".getBytes(); diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java index 695f936a58..dd0cad864a 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java @@ -17,10 +17,12 @@ 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 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 index 0000000000..01072e547e Binary files /dev/null and b/test-data/document/headerPic.docx differ