XWPF: support for pictures in headers

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1083764 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Maxim Valyanskiy 2011-03-21 12:43:58 +00:00
parent 1ba16fb3b9
commit 8d72b9137a
7 changed files with 56 additions and 10 deletions

View 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();

View File

@ -133,7 +133,6 @@ public class XWPFHeader extends XWPFHeaderFooter {
}
}
cursor.dispose();
getAllPictures();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View 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;
}

View File

@ -34,15 +34,21 @@ 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 {

View 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();

View File

@ -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<XWPFPicture> pictures = r.getEmbeddedPictures();
for (XWPFPicture pic : pictures) {
assertNotNull(pic.getPictureData());
}
count+= pictures.size();
}
}
assertEquals(1, count);
}
}

Binary file not shown.