#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
This commit is contained in:
Nick Burch 2015-08-13 20:23:58 +00:00
parent cfbc261e65
commit 1a84b476e8
3 changed files with 26 additions and 16 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -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);