Page 1 of 1

Images have different percentage size after binding.

PostPosted: Tue Oct 16, 2012 12:49 am
by re6exp
Hello, Jason!

I have two questions.

1. I've successfully bound set of images in template (http://www.docx4java.org/forums/docx-java-f6/databinding-repeats-no-corresponded-xpath-t1215.html.
The image set contains images with different original size.
When I attempt to set image size during template loading, after binding I get images with different percentage of original size. For example, the first image may be 107% of original size, but the second is 99%, etc. Both of them are in limits of document margins.

I apply changes in template before binding by using TraversalUtil and implementing Callback interface. Inside the apply(Object) method I'm setting the Anchor's effectExtent and extent properties to zero. So I try to set image size to get 100%.

Code: Select all
if (object instanceof Anchor) {
   Anchor anchor = (Anchor) object;
   anchor.getEffectExtent().setB(0L);
   anchor.getEffectExtent().setT(0L);
   anchor.getEffectExtent().setL(0L);
   anchor.getEffectExtent().setR(0L);

   anchor.getExtent().setCx(0L);
   anchor.getExtent().setCy(0L);
}

How can I achieve size of every image to be exactly 100% if original size? May be, there is a way to do some postprocessing of multiplied image containers and their properties after fiiling the template? But when I tried to do that with help of TraversUtil, I've got an exception...

2. Second question: I have to set border to image. But after filling the template the images doesn't have boards as I prepare them in template before. Should I do this "by hand" in my code, for example, in TraversalUtil or some different way?

regards,
Anton

Re: Images have different percentage size after binding.

PostPosted: Tue Oct 16, 2012 3:19 am
by re6exp
I found solution. First time I've used the xpath-way to get drawing object, and the using it second time produced an exception, may be there was my mistake. With implementing Callback interface the second run through WordprocessingMLPackage works OK after filling template.

Re: Images have different percentage size after binding.

PostPosted: Tue Oct 16, 2012 5:55 pm
by jason
re6exp wrote:I found solution. First time I've used the xpath-way to get drawing object, and the using it second time produced an exception, may be there was my mistake.


The issue you had with XPath is likely the bug/limitation in JAXB. This is documented in the code and elsewhere.

re6exp wrote:With implementing Callback interface the second run through WordprocessingMLPackage works OK after filling template.


Good to hear. Perhaps you could post your code for the benefit of others? Surround it in square brackets containing 'java'. thanks!

Re: Images have different percentage size after binding.

PostPosted: Thu Oct 18, 2012 7:33 pm
by re6exp
There is another problem. The inner sdt around the bound image is not removed after using RemovalHandler.
The result file contains:
Code: Select all
        <w:sdt>
            <w:sdtPr>
                <w:rPr>
                    <w:vanish/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
                <w:tag w:val="od:rptd=imagesPath&amp;od:RptInst=1"/>
                <w:id w:val="865337542"/>
                <w:temporary/>
            </w:sdtPr>
            <w:sdtEndPr/>
            <w:sdtContent>
                <w:p w:rsidR="008A4ACC" w:rsidRPr="0095298F" w:rsidRDefault="00727829" w:rsidP="002024E4">
                    <w:pPr>
                        <w:keepNext/>
                        <w:pageBreakBefore/>
                        <w:spacing w:before="240" w:after="0" w:line="240" w:lineRule="auto"/>
                        <w:contextualSpacing/>
                        <w:rPr>
                            <w:vanish/>
                            <w:lang w:val="en-US"/>
                        </w:rPr>
                    </w:pPr>
                    <w:r>
                        <w:rPr>
                            <w:noProof/>
                            <w:lang w:eastAsia="ru-RU"/>
                        </w:rPr>
                        <w:drawing>
                            <wp:inline distT="0" distB="0" distL="0" distR="0">
                                <wp:extent cx="6370320" cy="8737600"/>
                                <wp:effectExtent l="0" t="0" r="0" b="0"/>
                                <wp:docPr id="2" name="Рисунок 2"/>
                                <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="1" name=""/>
                                                <pic:cNvPicPr/>
                                            </pic:nvPicPr>
                                            <pic:blipFill>
                                                <a:blip r:embed="rId13"/>
                                                <a:stretch>
                                                    <a:fillRect/>
                                                </a:stretch>
                                            </pic:blipFill>
                                            <pic:spPr>
                                                <a:xfrm>
                                                    <a:off x="0" y="0"/>
                                                    <a:ext cx="6370320" cy="8737600"/>
                                                </a:xfrm>
                                                <a:prstGeom prst="rect">
                                                    <a:avLst/>
                                                </a:prstGeom>
                                                <a:ln w="6350" cap="sq">
                                                    <a:solidFill>
                                                        <a:srgbClr val="FF0000"/>
                                                    </a:solidFill>
                                                    <a:miter lim="800000"/>
                                                </a:ln>
                                            </pic:spPr>
                                        </pic:pic>
                                    </a:graphicData>
                                </a:graphic>
                            </wp:inline>
                        </w:drawing>
                    </w:r>
                </w:p>
            </w:sdtContent>
        </w:sdt>

od:rptd=imagesPath&amp;od:RptInst=1
As I saw in RemovalHandler.xslt, there is only 'repeat' variant. And repeat sdts remove OK. How can I remove these inner sdt around image? This container make it impossible to set image size exactly I have to.

Re: Images have different percentage size after binding.

PostPosted: Thu Oct 18, 2012 7:40 pm
by re6exp
The template looks like:
Code: Select all
<w:sdt>
            <w:sdtPr>
                <w:rPr>
                    <w:vanish/>
                    <w:lang w:val="en-US"/>
                </w:rPr>
                <w:tag w:val="od:repeat=imagesPath"/>
                <w:id w:val="-1478296238"/>
                <w:placeholder>
                    <w:docPart w:val="DefaultPlaceholder_1082065158"/>
                </w:placeholder>
                <w:temporary/>
            </w:sdtPr>
            <w:sdtEndPr/>
            <w:sdtContent>
                <w:p w:rsidR="008A4ACC" w:rsidRPr="0095298F" w:rsidRDefault="00727829" w:rsidP="002024E4">
                    <w:pPr>
                        <w:keepNext/>
                        <w:pageBreakBefore/>
                        <w:spacing w:before="240" w:after="0" w:line="240" w:lineRule="auto"/>
                        <w:contextualSpacing/>
                        <w:rPr>
                            <w:vanish/>
                            <w:lang w:val="en-US"/>
                        </w:rPr>
                    </w:pPr>
                    <w:sdt>
                        <w:sdtPr>
                            <w:rPr>
                                <w:vanish/>
                                <w:lang w:val="en-US"/>
                            </w:rPr>
                            <w:tag w:val="od:xpath=imagePath"/>
                            <w:id w:val="-347787958"/>
                            <w:showingPlcHdr/>
                            <w:dataBinding w:xpath="/report/images/image" w:storeItemID="{451999E6-E9E3-4A74-8D14-FF6B74CC63F6}"/>
                            <w:picture/>
                        </w:sdtPr>
                        <w:sdtEndPr/>
                        <w:sdtContent>
                            <w:r w:rsidR="00780DCC">
                                <w:rPr>
                                    <w:noProof/>
                                    <w:vanish/>
                                    <w:lang w:eastAsia="ru-RU"/>
                                </w:rPr>
                                <w:drawing>
                                    <wp:anchor distT="0" distB="0" distL="114300" distR="114300" simplePos="0" relativeHeight="251658240" behindDoc="1" locked="1" layoutInCell="1" allowOverlap="1" wp14:anchorId="0839C6A1" wp14:editId="2B85C5C9">
                                        <wp:simplePos x="0" y="0"/>
                                        <wp:positionH relativeFrom="column">
                                            <wp:posOffset>-36195</wp:posOffset>
                                        </wp:positionH>
                                        <wp:positionV relativeFrom="topMargin">
                                            <wp:posOffset>775747</wp:posOffset>
                                        </wp:positionV>
                                        <wp:extent cx="1904400" cy="1904400"/>
                                        <wp:effectExtent l="19050" t="19050" r="19685" b="19685"/>
                                        <wp:wrapThrough wrapText="bothSides">
                                            <wp:wrapPolygon edited="0">
                                                <wp:start x="-216" y="-216"/>
                                                <wp:lineTo x="-216" y="21607"/>
                                                <wp:lineTo x="21607" y="21607"/>
                                                <wp:lineTo x="21607" y="-216"/>
                                                <wp:lineTo x="-216" y="-216"/>
                                            </wp:wrapPolygon>
                                        </wp:wrapThrough>
                                        <wp:docPr id="2" name="Рисунок 1"/>
                                        <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="Picture 1"/>
                                                        <pic:cNvPicPr>
                                                            <a:picLocks noChangeAspect="1" noChangeArrowheads="1"/>
                                                        </pic:cNvPicPr>
                                                    </pic:nvPicPr>
                                                    <pic:blipFill>
                                                        <a:blip r:embed="rId12">
                                                            <a:extLst>
                                                                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                                                    <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0"/>
                                                                </a:ext>
                                                            </a:extLst>
                                                        </a:blip>
                                                        <a:stretch>
                                                            <a:fillRect/>
                                                        </a:stretch>
                                                    </pic:blipFill>
                                                    <pic:spPr bwMode="auto">
                                                        <a:xfrm>
                                                            <a:off x="0" y="0"/>
                                                            <a:ext cx="1904400" cy="1904400"/>
                                                        </a:xfrm>
                                                        <a:prstGeom prst="rect">
                                                            <a:avLst/>
                                                        </a:prstGeom>
                                                        <a:noFill/>
                                                        <a:ln w="6350" cap="sq">
                                                            <a:solidFill>
                                                                <a:srgbClr val="FF0000"/>
                                                            </a:solidFill>
                                                            <a:miter lim="800000"/>
                                                        </a:ln>
                                                    </pic:spPr>
                                                </pic:pic>
                                            </a:graphicData>
                                        </a:graphic>
                                        <wp14:sizeRelH relativeFrom="margin">
                                            <wp14:pctWidth>0</wp14:pctWidth>
                                        </wp14:sizeRelH>
                                        <wp14:sizeRelV relativeFrom="margin">
                                            <wp14:pctHeight>0</wp14:pctHeight>
                                        </wp14:sizeRelV>
                                    </wp:anchor>
                                </w:drawing>
                            </w:r>
                        </w:sdtContent>
                    </w:sdt>
                </w:p>
            </w:sdtContent>
        </w:sdt>

Re: Images have different percentage size after binding.

PostPosted: Fri Oct 19, 2012 9:24 am
by re6exp
The RemovalHandler.xslt needs to be extended by smth like this:

Code: Select all
<xsl:param name="rptd" select="$all or contains($types, 'rptd')" />

and this:
Code: Select all
test="$xpath and contains(w:sdtPr/w:tag/@w:val, 'od:xpath=') or $repeat and contains(w:sdtPr/w:tag/@w:val, 'od:repeat=') or $condition and contains(w:sdtPr/w:tag/@w:val, 'od:condition=') or $rptd and contains(w:sdtPr/w:tag/@w:val, 'od:rptd=')">


In Java code, both
Code: Select all
RemovalHandler rh = new RemovalHandler();
rh.removeSDTs(wordMLPackage.getMainDocumentPart(), Quantifier.ALL);
and
Code: Select all
rh.removeSDTs(wordMLPackage, Quantifier.NAMED,"rptd");
will be delete sdt.
This is neccesary to remove sdt container around picture box.

Re: Images have different percentage size after binding.

PostPosted: Fri Oct 19, 2012 6:11 pm
by jason
Hello Anton

Thanks for taking the time to post your fix. I'd like to apply it.

Assuming you have a fork on github, would you mind sending a pull request?

That will keep a better history of how the change happened.

thanks again .. Jason