Code Monkey home page Code Monkey logo

Comments (11)

dolanmiu avatar dolanmiu commented on May 9, 2024 1

A new way of adding images will be like so:

https://github.com/dolanmiu/docx/blob/master/demo/demo24.js

A similar way will also be for Documents:

const image = Media.addImage(doc, "./test.png");
doc.insertImage(image);

The helper methods such as doc.createImage("./test.png") will stay

from docx.

dolanmiu avatar dolanmiu commented on May 9, 2024

Should be possible, I don’t see why not

As that function creates a paragraph, which includes the image

If this doesn’t work, let me know

from docx.

ycj1905 avatar ycj1905 commented on May 9, 2024

Yes. It doesn't work.
table.getCell(2, 2).addContent(doc.createImage('./test.png'));
The above code will crash the result *.docx.

I run it on MAC with Word(15.15)

from docx.

h4buli avatar h4buli commented on May 9, 2024

Hi all,

Just to inform you that there is issue with size and images. Images size are exported using EMUs
emu = pixel * 9525 and if this value turns out to have decimals -> then the resulting docx will be invalid.

Maybe it's not connected with your case, but it might be :-)

from docx.

h4buli avatar h4buli commented on May 9, 2024

@dolanmiu,
I checked the code and as it looks like: it will duplicate the image on two places.

docx/src/file/file.ts

Lines 119 to 127 in 9f591c4

public createImage(image: string): PictureRun {
const mediaData = this.media.addMedia(image, this.nextId++);
this.docRelationships.createRelationship(
mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`,
);
return this.document.createDrawing(mediaData);
}

and here is the issue (note this.addDrawing(paragraph);) which adds to the body
public addDrawing(pictureParagraph: Paragraph): void {
this.body.push(pictureParagraph);
}
public createDrawing(imageData: IMediaData): PictureRun {
const paragraph = new Paragraph();
const run = new PictureRun(imageData);
paragraph.addRun(run);
this.addDrawing(paragraph);
return run;
}

from docx.

dolanmiu avatar dolanmiu commented on May 9, 2024

No, the document one adds the XML etc,

The file one should add it to the Media object, relationships etc, and also call the Document one

The file one is more like the API which handles everything

from docx.

h4buli avatar h4buli commented on May 9, 2024

Then some changes to the file and document methods needs to be done.
I just want to point out that with the current code base, image will be added twice. If we extract to a variable:

const imageP = doc.createImage('./test.png');
table.getCell(2,2).addContent(imageP);

Once when called imageP = doc.createImage():

  • it will create image data
  • add to the Media ... etc
  • call Document,
    • create Paragraph
    • add it to the body
    • return paragraph with the image.

And second, that returned paragraph imageP will be added to the table cell table.getCell(2,2).addContent(imageP)

from docx.

dolanmiu avatar dolanmiu commented on May 9, 2024

Yes you are right,

doc.createImage() is kind of like doc.createParagraph(), I do not want to change the functionality here

They both put content on the word document

Need a way to add an image to the media etc without adding it to the document

The below is how you would create and add paragraphs manually:

const paragraph = new Paragraph();
doc.addParagraph(paragraph)

But it is difficult doing:

const drawing = new Drawing();
doc.media.add(drawing);
doc.Relationships.add(...)
doc.addDrawing(drawing);

Maybe we could add a flag in createImage, to not add it into the document:

doc.createImage('./test.png', false);

Or have a seperate new method?

from docx.

madarche avatar madarche commented on May 9, 2024

Isn't createImageData cf. https://github.com/dolanmiu/docx/blob/master/src/file/file.ts#L129 doing what you are both describing: adding the media into the docx but not inserting it in the document body?

from docx.

h4buli avatar h4buli commented on May 9, 2024

Yes it is, but for a different use case of images.
public createImageData(imageName: string, data: Buffer, width?: number, height?: number): IMediaData
We don't pass the file name, instead we need to pass image name, data(buffer) and optionally size.
This is for the case when we already have the data (Buffer) for the image (we don't read form the file system).

But I agree we can extend or create similar method for the use case when crating image with file path.

from docx.

madarche avatar madarche commented on May 9, 2024

OK. Thanks for the explanation @h4buli.

from docx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.