S
S
Sergey c0re2019-11-11 17:22:04
Word
Sergey c0re, 2019-11-11 17:22:04

How to add an image to the required place in the document in a DOCX file?

1. It is necessary to add to the file and position relative to some element (let's say tables) a PNG / JPG image (logo, print scan, etc.), i.e. the image should be on top of the text, not in the text.
2. To replace the image in the Docx template, yes, I understand it is done by elementary substitution of the image file in the archive, but the image sizes are different, how to position it?
Well, conditionally, let's position it in the center of a table cell, which, by the way, also needs to be found (by the text in the cell?).
I tried to google until I found nothing intelligible, except for the banal addition of an image.
how to insert image into xml sheet found:

spoiler
<w:p w:rsidR="00C236FA" w:rsidRDefault="00894C8C" w:rsidP="00C236FA">
            <w:pPr><w:pStyle w:val="a5"/><w:snapToGrid w:val="0"/>
              <w:rPr><w:b/><w:szCs w:val="24"/></w:rPr>
            </w:pPr>
            <w:r>
              <w:rPr><w:b/><w:noProof/><w:szCs w:val="24"/><w:lang w:eastAsia="ru-RU"/></w:rPr>
              <w:drawing>
                <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251676672" behindDoc="0" locked="0" layoutInCell="1" allowOverlap="1"><wp:simplePos x="0" y="0"/>
                  <wp:positionH relativeFrom="column">
                    <wp:posOffset>472440</wp:posOffset>
                  </wp:positionH>
                  <wp:positionV relativeFrom="paragraph">
                    <wp:posOffset>76200</wp:posOffset>
                  </wp:positionV><wp:extent cx="1950720" cy="1539240"/><wp:effectExtent l="0" t="0" r="0" b="0"/><wp:wrapNone/><wp:docPr id="11" name="Рисунок 1" descr="Подпись+печать.bmp"/>
                  <wp:cNvGraphicFramePr><a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/></wp:cNvGraphicFramePr>
                  <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                    <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                      <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                        <pic:nvPicPr><pic:cNvPr id="0" name="Подпись+печать.bmp"/><pic:cNvPicPr/></pic:nvPicPr>
                        <pic:blipFill>
                          <a:blip r:embed="rId8" cstate="print">
                            <a:clrChange>
                              <a:clrFrom><a:srgbClr val="FFFFFF"/></a:clrFrom>
                              <a:clrTo>
                                <a:srgbClr val="FFFFFF"><a:alpha val="0"/></a:srgbClr>
                              </a:clrTo>
                            </a:clrChange>
                          </a:blip>
                          <a:stretch><a:fillRect/></a:stretch>
                        </pic:blipFill>
                        <pic:spPr>
                          <a:xfrm><a:off x="0" y="0"/><a:ext cx="1950720" cy="1539240"/></a:xfrm>
                          <a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
                        </pic:spPr>
                      </pic:pic>
                    </a:graphicData>
                  </a:graphic>
                </wp:anchor>
              </w:drawing>
            </w:r>
          </w:p>

but it is not clear even in what units the displacements and sizes are there:?
and it is not clear how the image scales to normal (the actual size of the picture on paper) having such dimensions:
5dc96e0652f44047653094.png
how is it done in online document generators (contracts, invoices, etc.)?
PS: interested in any answers, in any of PHP, PYTHON, JAVA, NODE.JS, PERL

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2019-11-11
@erge

Hello!
I'm not an expert in this matter, but recently I myself had to programmatically create docx files and searching I came across 2 libraries.
1) docx4j
https://www.docx4java.org/trac/docx4j
2) docx-stamper (based on docx4j, supports SpEL )
https://github.com/thombergs/docx-stamper
at the right place in the document, add the SpEL variable. ( ${user.image} ) and of course add your image to the specified area.
For image positioning, check out these links. Suddenly, they will help)
https://www.docx4java.org/forums/docx-java-f6/how-...
https://stackoverflow.com/questions/24745423/how-t...
https://www .docx4java.org/forums/docx-java-f6/how-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question