#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:
parent
cfbc261e65
commit
1a84b476e8
@ -166,7 +166,9 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
String pStyle = "Header";
|
String pStyle = "Header";
|
||||||
int i = getRelationIndex(relation);
|
int i = getRelationIndex(relation);
|
||||||
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
|
HdrDocument hdrDoc = HdrDocument.Factory.newInstance();
|
||||||
|
|
||||||
XWPFHeader wrapper = (XWPFHeader) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
XWPFHeader wrapper = (XWPFHeader) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
||||||
|
wrapper.setXWPFDocument(doc);
|
||||||
|
|
||||||
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
|
CTHdrFtr hdr = buildHdr(type, pStyle, wrapper, pars);
|
||||||
wrapper.setHeaderFooter(hdr);
|
wrapper.setHeaderFooter(hdr);
|
||||||
@ -201,7 +203,9 @@ public class XWPFHeaderFooterPolicy {
|
|||||||
String pStyle = "Footer";
|
String pStyle = "Footer";
|
||||||
int i = getRelationIndex(relation);
|
int i = getRelationIndex(relation);
|
||||||
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
|
FtrDocument ftrDoc = FtrDocument.Factory.newInstance();
|
||||||
|
|
||||||
XWPFFooter wrapper = (XWPFFooter) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
XWPFFooter wrapper = (XWPFFooter) doc.createRelationship(relation, XWPFFactory.getInstance(), i);
|
||||||
|
wrapper.setXWPFDocument(doc);
|
||||||
|
|
||||||
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
|
CTHdrFtr ftr = buildFtr(type, pStyle, wrapper, pars);
|
||||||
wrapper.setHeaderFooter(ftr);
|
wrapper.setHeaderFooter(ftr);
|
||||||
|
@ -915,12 +915,22 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
|
|||||||
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
|
* @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_DIB
|
||||||
*/
|
*/
|
||||||
public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
|
public XWPFPicture addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
|
||||||
throws InvalidFormatException, IOException {
|
throws InvalidFormatException, IOException {
|
||||||
XWPFDocument doc = parent.getDocument();
|
String relationId;
|
||||||
|
XWPFPictureData picData;
|
||||||
// Add the picture + relationship
|
|
||||||
String relationId = doc.addPictureData(pictureData, pictureType);
|
// Work out what to add the picture to, then add both the
|
||||||
XWPFPictureData picData = (XWPFPictureData) doc.getRelationById(relationId);
|
// 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
|
// Create the drawing entry for it
|
||||||
try {
|
try {
|
||||||
|
@ -30,7 +30,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationship;
|
|||||||
import org.apache.poi.xssf.usermodel.XSSFRelation;
|
import org.apache.poi.xssf.usermodel.XSSFRelation;
|
||||||
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
import org.apache.poi.xwpf.XWPFTestDataSamples;
|
||||||
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
|
||||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
|
|
||||||
|
|
||||||
public class TestXWPFPictureData extends TestCase {
|
public class TestXWPFPictureData extends TestCase {
|
||||||
|
|
||||||
@ -64,7 +63,7 @@ public class TestXWPFPictureData extends TestCase {
|
|||||||
verifyOneHeaderPicture(readBack);
|
verifyOneHeaderPicture(readBack);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FIXMEtestCreateHeaderPicture() throws Exception { // TODO Fix
|
public void testCreateHeaderPicture() throws Exception {
|
||||||
XWPFDocument doc = new XWPFDocument();
|
XWPFDocument doc = new XWPFDocument();
|
||||||
|
|
||||||
// Starts with no header
|
// Starts with no header
|
||||||
@ -73,16 +72,13 @@ public class TestXWPFPictureData extends TestCase {
|
|||||||
|
|
||||||
// Add a default header
|
// Add a default header
|
||||||
policy = doc.createHeaderFooterPolicy();
|
policy = doc.createHeaderFooterPolicy();
|
||||||
|
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
|
||||||
XWPFParagraph[] hparas = new XWPFParagraph[] {
|
header.getParagraphs().get(0).createRun().setText("Hello, Header World!");
|
||||||
new XWPFParagraph(CTP.Factory.newInstance(), doc)
|
header.createParagraph().createRun().setText("Paragraph 2");
|
||||||
};
|
|
||||||
hparas[0].createRun().setText("Header Hello World!");
|
|
||||||
XWPFHeader header = policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, hparas);
|
|
||||||
assertEquals(0, header.getAllPictures().size());
|
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(
|
header.getParagraphs().get(0).getRuns().get(0).addPicture(
|
||||||
new ByteArrayInputStream(new byte[] {1,2,3,4}),
|
new ByteArrayInputStream(new byte[] {1,2,3,4}),
|
||||||
Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
|
Document.PICTURE_TYPE_JPEG, "test.jpg", 2, 2);
|
||||||
|
Loading…
Reference in New Issue
Block a user