Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add image to a word document header or footer using XWPF #167

Closed
thomasan1 opened this issue Jan 7, 2019 · 8 comments
Closed

Comments

@thomasan1
Copy link

Hi,
I'm trying to add a picture into the footer of a word document, using the method AddPicture of Run object.
The word document is generated, but open the document the image is not available.

Using the same method with a Paragraph and a Run contained into the body of the document it work well.

How can I fix this problem?

Thanks

@tonyqus tonyqus self-assigned this Feb 8, 2019
@tonyqus tonyqus added the bug label Feb 8, 2019
@tonyqus
Copy link
Member

tonyqus commented Feb 8, 2019

As I remember, this issue has been fixed in 2.4.1. Can you check the latest release on nuget and come back to this issue again?

@Tschakah
Copy link

Hi,
thanks for the work you put in TQ, i'm having a similar problem and can maybe narrow this down a little bit.

Problem: Adding picture data to docx-structure i.e. word/media/xxx.jpg
NPOI Version: 2.4.1.

While trying to add an image to a new XWPF-Document: I can add the specific bytes to the XWPFDocument-Object as PictureData and/or PackagePictureData without a problem but the write method isn't creating a word/media content.

XWPFDocument outDoc = new XWPFDocument();
outDoc.CreateParagraph().CreateRun().AddPicture(new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite), (int)PictureType.JPEG, "Background.jpg", 200, 400);
FileStream outStream = new FileStream("Print.docx", FileMode.Create,FileAccess.ReadWrite);
outDoc.Write(outStream);
...

Without having a media-dir and image-file in the docx archive , there is a placeholder in the word doc instead of the actual picture (as to be expected).

I also tried to read a template docx in that contains the image and write it out into another file (respect. copy it) and in that case the picture is in the docx but the file is having 100s of pages instead of 1 making it useless.

That looks like iam just missing something while adding the picture as written above but maybe this is a bug. It would be really nice if you could check this out and come up with a fix or advise.

Thanks in advance.

@ssementsov
Copy link

ssementsov commented Sep 25, 2019

Hi,
I'm having the same problem:

           XWPFDocument document = new XWPFDocument();
            XWPFParagraph paragraph = document.CreateParagraph();
            XWPFRun run = paragraph.CreateRun();

            byte[] pictureData = ResourceHelper.GetEmbeddedResourceAsBytes("logo.jpeg");

            run.AddPicture(new MemoryStream(pictureData), (int)PictureType.JPEG, "image1.jpeg", 229, 74);

            // count = 1
            var count = run.GetEmbeddedPictures().Count;

            var stream = new FileStream("D:\\addPicture.docx", FileMode.Create);
            document.Write(stream);
            stream.Close();

I also saw your Test "TestAddPicture":

          XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFParagraph p = doc.GetParagraphArray(2);
            XWPFRun r = p.Runs[0];

            Assert.AreEqual(0, doc.AllPictures.Count);
            Assert.AreEqual(0, r.GetEmbeddedPictures().Count);

            r.AddPicture(new MemoryStream(new byte[0]), (int)PictureType.JPEG, "test.jpg", 21, 32);

            Assert.AreEqual(1, doc.AllPictures.Count);
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);

But in result file, we don't get any images...
Thanks!

@ssementsov
Copy link

Hi,
I'm trying to add a picture into the footer of a word document, using the method AddPicture of Run object.
The word document is generated, but open the document the image is not available.

Using the same method with a Paragraph and a Run contained into the body of the document it work well.

How can I fix this problem?

Thanks

Hi,
can you show me please your code with a Paragraph and a Run that work well?
Thanks!

@WagnerSereia
Copy link

Hi @tonyqus, did you manage to solve this bug?
I am getting the same problem in my tests, following the same code suggestions.

@tonyqus tonyqus added this to the NPOI 2.5.2 milestone Jun 10, 2020
@tonyqus tonyqus added the docx label Sep 13, 2020
@tonyqus
Copy link
Member

tonyqus commented Sep 27, 2020

duplicate with #373

@jorgelodev
Copy link

jorgelodev commented Mar 12, 2021

Hi,
I'm trying to add a picture into the footer of a word document, using the method AddPicture of Run object.
The word document is generated, but open the document the image is not available.

Using the same method with a Paragraph and a Run contained into the body of the document it work well.

How can I fix this problem?

Thanks

Hello, the problem tha I'm facing is: when I add a image first into the header, and after into the body, the image from the body is not available. And if I add the image first into the body, and after into the header, the image from the body is not available...

How can I fix this problem?

Thanks

@tonyqus tonyqus reopened this Mar 15, 2021
@tonyqus
Copy link
Member

tonyqus commented Jun 2, 2021

@jorgelodev can you open a new issue for your case? This one is closed and released with NPOI 2.5.2.

@tonyqus tonyqus closed this as completed Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants