|Summary:||Unable to add image to a word document header using XWPF|
|Component:||XWPF||Assignee:||POI Developers List <dev>|
Description nishant 2015-08-12 12:17:53 UTC
I have a requirement to add image to header of a word document file (.docx) I tried using the available API methods to do so (passed inputstream of image and XWPFDocument for both but none of the two approaches worked): Approach1: ------------ XWPFHeaderFooterPolicy headerFooterPolicy = document .getHeaderFooterPolicy(); XWPFHeader defaultHeader = headerFooterPolicy.getDefaultHeader(); defaultHeader.getParagraphs().get(0).getRuns().get(0).addPicture(is, Document.PICTURE_TYPE_JPEG, "test.jpg", 100, 100); Approach 2: ----------- CTP ctp = CTP.Factory.newInstance(); CTR ctr = ctp.addNewR(); CTRPr rpr = ctr.addNewRPr(); XWPFParagraph imagePara = new XWPFParagraph(ctp, document); imagePara.createRun().addPicture(is, Document.PICTURE_TYPE_JPEG, "test.jpg", 50, 50); XWPFParagraph imaheparagraphs = new XWPFParagraph; imaheparagraphs=imagePara; headerFooterPolicy.createHeader( STHdrFtr.DEFAULT,imaheparagraphs); I am able to add an image to body but not able to find a way to do the same to the header even though POI has methods that suggest that it should. Thanks.
Comment 1 Nick Burch 2015-08-12 12:29:22 UTC
Can you define "didn't work"? Logged a problem? Got an exception? Added a different picture? Added it, but upsidedown / only greyscale / some other image problem? Made your pet cat sad? Ran without error, but POI couldn't then read it back? POI could read it, but couldn't see the image? POI was fine with it, but Word gave an error? And if so what? POI was happy, but Word crashed? Other?
Comment 2 nishant 2015-08-12 12:34:11 UTC
There was no exception thrown in either of the cases. The modified/saved word document simply didn't have the image in the header.
Comment 3 Nick Burch 2015-08-12 12:38:04 UTC
If you save and re-load the file with POI, can POI see the image in the header?
Comment 4 nishant 2015-08-13 08:26:38 UTC
Tried reading back the header after saving but dont get any image through .getPicturesTable()
Comment 5 Nick Burch 2015-08-13 20:25:56 UTC
Looks like there's a bug, and the picture wasn't ending up on the right part when added from a run. I've hopefully fixed this in r1695772. The new test TestXWPFPictureData.testCreateHeaderPicture() verifies that a picture can be added to a header, seen on the header, and still be found there after a save and re-load. Any chance you could do a build from svn / get a new enough nightly build / wait for 3.13 beta 2, then confirm if that has solved it completely?
Comment 6 Javen O'Neal 2015-10-07 10:16:07 UTC
Comment 7 nishant 2015-11-17 14:45:09 UTC
Have tried testing with 3.13 but it still doesn't work. Below is the code I am using FileInputStream stream=new FileInputStream(new File("D:/PNL.docx")); XWPFDocument document=new XWPFDocument(stream); XWPFHeaderFooterPolicy policy = document.getHeaderFooterPolicy(); XWPFHeader defaultHeader = policy.getDefaultHeader(); defaultHeader.addPictureData(new FileInputStream(new File("D:/logo/xyz.jpg")), Document.PICTURE_TYPE_JPEG); but it doesnt update the image in the document file. The document remains as such with no modification. This is returning size of 1: List<XWPFPictureData> pictures = defaultHeader.getAllPictures(); System.out.println(pictures.size()); Please help validate the code.
Comment 8 nishant 2016-06-10 09:58:13 UTC
Any updates on the above issue please? Please let me know if there is an alternate to updating/inserting image in header of a word document.
Comment 9 Nick Burch 2016-06-10 10:04:07 UTC
If you can write a junit unit test, which shows the issue, that would be a big help for us in understanding what's wrong + trying to fix it (Last year I added TestXWPFPictureData.testCreateHeaderPicture() which covers at least some of the use case, so if there's more still wrong then we need another unit test to show the problem!)
Comment 10 Bilgehan 2016-11-11 12:17:47 UTC
Had the same problem, it was fixed in 3.13 but with 3.15 appear again.
Comment 11 Mark Murphy 2016-11-11 20:50:52 UTC
(In reply to Bilgehan from comment #10) > Had the same problem, it was fixed in 3.13 but with 3.15 appear again. Please attach a minimal document that shows the problem, and if you can a unit test. this will help us work through the issue.
Comment 12 Carlo Sciolla 2016-11-26 19:14:36 UTC
Bumped into the same issue here. Not sure how to create a unit test to show the issue, as I'm not familiar with POI codebase. But I believe this guy did a fairly thorough analysis including code pointers that should give you enough details: http://stackoverflow.com/a/35103749/350923
Comment 13 Nicola Isotta 2016-12-09 12:00:17 UTC
Hopefuly I'm adding the necessary infos. Here's the class I used to generate the docx: http://pastebin.com/UT4RS8ah Here's a link to the generated .docx: https://drive.google.com/file/d/0B2TPcgQDtWuiM0VJZEs5ZVFFdlE/view?usp=sharing I don't know how to write a Unit Test for this problem but I hope the files I provided together with the infos from the stackoverflow thread can help track down the problem.
Comment 14 Mark Murphy 2016-12-09 13:04:14 UTC
I believe the issue is in XWPFRun.addPicture(...) This block of code: CTBlipFillProperties blipFill = pic.addNewBlipFill(); CTBlip blip = blipFill.addNewBlip(); blip.setEmbed(parent.getDocument().getRelationId(picData)); blipFill.addNewStretch().addNewFillRect(); retrieves the main document rather than the document part containing the image, and attempts to retrieve the relation id from that. This works when the image is in the main document, but not when the image is in another part such as a header or footer. In fact, the missing element in the header and footer is the relation ID. I believe I can write some unit tests to prove this out.