Comments (11)
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.
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.
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.
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.
@dolanmiu,
I checked the code and as it looks like: it will duplicate the image on two places.
Lines 119 to 127 in 9f591c4
and here is the issue (note
this.addDrawing(paragraph);
) which adds to the bodydocx/src/file/document/document.ts
Lines 60 to 72 in 9f591c4
from docx.
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.
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.
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.
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.
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.
OK. Thanks for the explanation @h4buli.
from docx.
Related Issues (20)
- How can I convert latex to Word format HOT 6
- Can open in libre, not in word. OOXML-Validator has a bunch of errors HOT 8
- Adding a validator HOT 4
- Patcher does not replace content in Footer HOT 2
- Repeat header in row HOT 1
- Attempted import error: 'Packer' is not exported from 'docx' (imported as 'Packer'). HOT 7
- Packer.toBuffer error in browser: nodebuffer not supported by this platform HOT 1
- Lists - instance not working with level greater than 0
- ExternalHyperlink renders empty line HOT 3
- tableHeader repeats row only if declared on the first row in a table
- el.elements not iterable when patching comments
- can not set indent by chars HOT 2
- Adding different numbering within the same line of paragraph HOT 12
- Field Codes in SimpleField Formulas HOT 1
- Corrupt Word document from patching with an XML attribute with an ampersand HOT 3
- After Patching, Following Text is Wrong Font Family HOT 1
- Possibility of adding hanging without affecting left indent of the paragraph.
- `PageNumber.TOTAL_PAGES_IN_SECTION` not documented and not functional, but within TS enum HOT 2
- Colors are not showing properly HOT 2
- When I open a document using WPS office, the width of the table cannot fill the entire page HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from docx.