How to force Docx4j to refresh a replaced image file

杀马特。学长 韩版系。学妹 提交于 2019-12-24 18:57:58

问题


I use Docx4j to generate various documents and I have a problem with the generation of documents containing images.

My program constructs a PNG file with a size of 300x200 pixels and generates a document that contains this image. When I open this document, the image is scaled at 1:1. Then the program overwrites the PNG with an image of 600x400 pixels and generates the document again. When I open this new document, the image is scaled at 1:2. Its dimensions are the same than the previous image.

It seems to me that Docx4j's class BinaryPartAbstractImage caches the informations of the images in a static member and does not update the informations when the file changes.

Currently I have no other solution than stopping the program to clear the cache or to use new image file names when I generate the document. The former solution is impratical and the latter introduces more problems in the parts of the software that expect the file's path not to be changed.

So, do you have another workaround to clear the image cache?


回答1:


The image dimensions aren't persisted in the image part; they are specified in the XML positioning the image in the main document part (or header, footer or whatever).

If you create a docx containing an image then look at your main document part (one way to do this is to upload to the PartsList webapp, then navigate into the main document part), you'll see the relevant XML.

For example:

                    <w:drawing>
                        <wp:inline distT="0" distB="0" distL="0" distR="0">
                            <wp:extent cx="3238500" cy="2362200"/>
                            <wp:effectExtent l="19050" t="0" r="0" b="0"/>
                            <wp:docPr id="1" name="Picture 1" />
                            <wp:cNvGraphicFramePr>
                                <a:graphicFrameLocks noChangeAspect="true"/>
                            </wp:cNvGraphicFramePr>
                            <a:graphic>
                                <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                                    <pic:pic>
                                        <pic:nvPicPr>
                                            <pic:cNvPr id="0" name="Picture 1" />
                                            <pic:cNvPicPr>
                                                <a:picLocks noChangeAspect="true" noChangeArrowheads="true"/>
                                            </pic:cNvPicPr>
                                        </pic:nvPicPr>
                                        <pic:blipFill>
                                            <a:blip cstate="print" r:embed="rId8"/>
                                            <a:srcRect/>
                                            <a:stretch>
                                                <a:fillRect/>
                                            </a:stretch>
                                        </pic:blipFill>
                                        <pic:spPr bwMode="auto">
                                            <a:xfrm>
                                                <a:off x="0" y="0"/>
                                                <a:ext cx="3238500" cy="2362200"/>
                                            </a:xfrm>
                                            <a:prstGeom prst="rect">
                                                <a:avLst/>
                                            </a:prstGeom>
                                            <a:noFill/>
                                            <a:ln w="9525">
                                                <a:noFill/>
                                                <a:miter lim="800000"/>
                                                <a:headEnd/>
                                                <a:tailEnd/>
                                            </a:ln>
                                        </pic:spPr>
                                    </pic:pic>
                                </a:graphicData>
                            </a:graphic>
                        </wp:inline>
                    </w:drawing>

Its this which you'll need to update (cx, cy). You might find it easier to replace this whole structure (taking care with the relId in @r:embed). See the AddImage sample.



来源:https://stackoverflow.com/questions/16605945/how-to-force-docx4j-to-refresh-a-replaced-image-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!