dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.44k stars 486 forks source link

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

Open ameydesh opened 2 months ago

ameydesh commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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