Page 1 of 1

Signature Line in Document

PostPosted: Thu Jan 07, 2016 3:55 am
by TimSimpson
I am trying to implement a Signature Block in a docx file using DocX4J. We have already implemented the rest of the document generation based on content using DocX4J and now we would like to add signature blocks. In docx a signature block looks like the following:

<w:p w:rsidR="00A82DB1" w:rsidRDefault="006071C7">
<w:r>
<w:pict>
<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype>
<v:shape id="_x0000_i1025" type="#_x0000_t75" alt="Microsoft Office Signature Line..." style="width:192pt;height:96pt">
<v:imagedata r:id="rId8" o:title=""/>
<o:lock v:ext="edit" ungrouping="t" rotation="t" cropping="t" verticies="t" text="t" grouping="t"/>
<o:signatureline v:ext="edit" id="{923ef6ed-871e-4024-8365-0c7ba97a28fe}" provid="{00000000-0000-0000-0000-000000000000}"
o:suggestedsigner="Dynamic Name" o:suggestedsigner2="Dynamic Title" o:suggestedsigneremail="email" issignatureline="t"/>
</v:shape>
</w:pict>
</w:r>
</w:p>

The picture has a shape which contains the image data with a relationship to an Image part stored at "\word\media" within the docx archive. I used the webapp to decompose this but the portion that has to do with the imagedata tag just had the following:

CTImageData imagedata = vmlObjectFactory.createCTImageData();
JAXBElement<org.docx4j.vml.CTImageData> imagedataWrapped = vmlObjectFactory.createImagedata(imagedata);
shape.getEGShapeElements().add( imagedataWrapped);
imagedata.setTitle( "");
imagedata.setId( "rId8");

While this creates the tag and sets the Id, it doesn't load the image that it refers too which is a bitmap that I create dynamically with the name and title on it. I have looked and found ImageBmpPart which extends BinaryPartAbstractImage. I have found many examples of how to use this to create an inline image but I don't think that will work here. Is using ImageBmpPart the right approach to use here to import the image, store it in "\word\media\", create the relationship Id and relationship, and then input that Id into the above code so that the imagedata tag correctly points to the signature line bitmap that I have created? What is the correct order of calls to do the above?

Re: Signature Line in Document

PostPosted: Fri Jan 08, 2016 8:13 pm
by jason
Yes, essentially you create an image part, add it as a rel of the main document part (using addTargetPart), then set @r:id to the rel id that call returns.

By the way, Enterprise v3.3 will handle signing.

Re: Signature Line in Document

PostPosted: Sat Jan 09, 2016 6:56 am
by TimSimpson
Thank you very much. When is Enterprise v3.3 due out?

Re: Signature Line in Document

PostPosted: Wed Jan 13, 2016 7:21 am
by TimSimpson
OK. So close. I did what you told me. Code is below.

Code: Select all
ImageBmpPart sigImagePart = (ImageBmpPart)ImageBmpPart.createImagePart(mainDocumentPart, imageByteData);
Relationship signImageRel = mainDocumentPart.addTargetPart(sigImagePart);
      
// Create object for imagedata (wrapped in JAXBElement)
CTImageData imagedata = vmlObjectFactory.createCTImageData();
JAXBElement<org.docx4j.vml.CTImageData> imagedataWrapped = vmlObjectFactory.createImagedata(imagedata);
shape.getEGShapeElements().add( imagedataWrapped);
imagedata.setTitle("");
imagedata.setId(signImageRel.getId());


However when I look at the document archive after it is created it has the id in the document.xml as rdI4 but the relationship file has the image as rId2? I step through the code and the relationship does have an id of rdI4 and I even looked at the mainDocumentPart under the relationship array. It has the image in the index as 4. Why does it write to file as rdI2 then? How can I resolve issue? If I open the file it of course has an issue. If I go into the archive and change the document.xml imagepart from rdI4 to rdI2 then it works exactly as I want it to.

Re: Signature Line in Document

PostPosted: Wed Jan 13, 2016 7:45 am
by TimSimpson
Nevermind. Stepped through DocX4J code and figured it out. For others, the following works:

Code: Select all
ImageBmpPart sigImagePart = (ImageBmpPart)ImageBmpPart.createImagePart(mainDocumentPart, imageByteData);
String relId = sigImagePart.getRels().get(0).getId();
      
// Create object for imagedata (wrapped in JAXBElement)
CTImageData imagedata = vmlObjectFactory.createCTImageData();
JAXBElement<org.docx4j.vml.CTImageData> imagedataWrapped = vmlObjectFactory.createImagedata(imagedata);
shape.getEGShapeElements().add( imagedataWrapped);
imagedata.setTitle("");
imagedata.setId(relId);


The createImagePart static function creates the relationship already so I was creating actually creating 2 relationships and evidently it removed the second one since it was redundant. So I just grabbed the relationship id from the rels ArrayList in the ImageBmpPart and didn't have to do the addTargetPart as that was already done as well in createImagePart. This works perfectly. Looking forward to seeing your version of the Signature Line stuff in v3.3 to see if it makes this easier.

Re: Signature Line in Document

PostPosted: Sat Jan 16, 2016 1:56 pm
by jason
TimSimpson wrote:When is Enterprise v3.3 due out?


Community 3.3.0 will be first; we'll go through beta / RC process over coming weeks, with the release targeting last week Feb / first week March.

Enterprise 3.3.0 will follow very soon after that (2nd or 3rd week of March).