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

DocPr Element - ID attribute is always 1 when adding multiple images #2719

Open
ameydesh opened this issue Aug 27, 2024 · 8 comments
Open

DocPr Element - ID attribute is always 1 when adding multiple images #2719

ameydesh opened this issue Aug 27, 2024 · 8 comments

Comments

@ameydesh
Copy link

Hi,

In Word 2019 and certain versions of Microsoft 365, if you insert a textbox into a document's footer and subsequently add an image within the same document, there can be an issue where both elements receive an ID attribute value of 1. This conflict occurs because the DocPr element already has an ID assigned (1) in the footer, while the new image also receives the same ID. As a result, when attempting to open the document users encounter an error message stating "Unreadable content..."

Is it possible to assign the DocPr element an incremental value.

I can see in the code the value is hard-coded to 1, see the screenshot below

image

@anti-the-social
Copy link
Contributor

Hello,

The file you found is a test. There is only one image so id is set to 1
Id isn't hardcoded to 1 according to what I see here
https://github.com/dolanmiu/docx/blob/962795743c4f21d84c52e47e4f82dcf4ca536dc3/src/file/drawing/doc-properties/doc-properties.ts#L37C33-L37C61

So basically its sort of post processing, where lib goes through all images and does counter starting from 1 and +1 for each img it finds.

https://github.com/dolanmiu/docx/blob/962795743c4f21d84c52e47e4f82dcf4ca536dc3/src/util/convenience-functions.ts#L21C14-L21C45

@ameydesh
Copy link
Author

Thanks for your reply, do you know when was this change introduced? In my version of Docx it always assign an ID 1 to all the images in the document

@anti-the-social
Copy link
Contributor

In the initial post you mentioned that image is in the footer. I might guess its considered as one image for all of them.
Footer/header are like repetitive blocks that can be the reason.
Have you tried putting images into body of the document and check if ID stay 1 for all of them?

@ameydesh
Copy link
Author

ameydesh commented Aug 28, 2024

I mentioned if there's an textbox in the footer that contains a DocPr element with ID 1 and then if I add the images to the main body then the ID of DocPr element for these images is always set to 1 and this is causing issues when opening word document

@anti-the-social
Copy link
Contributor

anti-the-social commented Aug 28, 2024

Oh yeah, makes sense. They could overlap. I remember fixing similar overlapping in the past for something.
Can you create code example to run to reproduce? That would help

@ameydesh
Copy link
Author

Ok, I can give you a JSON object which contains the word document (with some tags/bookmarks) and the images to be populated in a bookmark. will that suffice?

@anti-the-social
Copy link
Contributor

Well, better if that's js script we can run in node, like

const { Document, Packer, Paragraph} = require('docx');

const doc = new Document({
  sections: [{
    children: [
      new Paragraph({
        text: "Some random text"
      })
    ]
  }]
});

// Used to export the file into a .docx file
Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("myTest.docx", buffer);
});

@ameydesh
Copy link
Author

Ok, I will try and get it done in next couple of days. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants