How to force Docx4j to refresh a replaced image file


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?


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 <a href="http://webapp.docx4java.org/OnlineDemo/PartsList.html" rel="nofollow">PartsList webapp</a>, 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 <a href="https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/samples/ImageAdd.java" rel="nofollow">AddImage sample</a>.


  • WSO2 analytics datasource
  • Automatic update of parent record updated_at field (Elixir-Ecto)
  • Can I Modify Hibernate/JPA Attribute Dynamically?
  • yii2 create translated URLs
  • disablinging autorecover option for powepoint
  • Iterate twice through a DataReader
  • ZipList with Scalaz
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Synchronize windows folders
  • HttpClient: disabling chunked encoding
  • Python ImageIO Gif Set Delay Between Frames
  • Clear fused location provider's location for testing
  • configure: error: no acceptable C compiler found in $PATH
  • Invalid Date on validation Date of js
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Eloquent paginate function in Slim 3 project using twig
  • Eclipse CDT error: Unable to compile
  • How integrated is Collada to OpenGL ES
  • Assign variable to the value in HTML
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • How to use carriage return with multiple line?
  • azure media services - The request body is too large and exceeds the maximum permissible limit
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • What is Eclipse's Declaration View used for?
  • Excel - Autoshape get it's name from cell (value)
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Perl system calls when running as another user using sudo
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Symfony2: How to get request parameter
  • Jquery - Jquery Wysiwyg return html as a string
  • Return words with double consecutive letters
  • SVN: Merging two branches together
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Bitwise OR returns boolean when one of operands is nil
  • Append folder name and increment by 1 using batch script
  • MATLAB: Piecewise function in curve fitting toolbox using fittype