Page 1 of 1

Insert PDF and update TOC

PostPosted: Fri Jul 28, 2017 10:01 pm
by castillo

I am a newbie with docx4j and I have two questions.

I need to open a docx file and fill some sections. One of the things that I have to do is insert a PDF. I searched it and i found how insert like OLE object but the generated docx produces the message of wrong format. The code is:

FileInputStream is = new FileInputStream(pdfFile);
OleObjectBinaryPart olePart = new OleObjectBinaryPart();
Relationship relOleObject = template.getMainDocumentPart().addTargetPart(olePart);

// The image the user sees, that they click on to open the object
FileInputStream isImage = new FileInputStream(image);
OleObjectBinaryPart imagePart = new OleObjectBinaryPart();
Relationship relImage = template.getMainDocumentPart().addTargetPart(imagePart); // imagePart defined outside this snippet

// Contains ${ImageId}, ${OLEShapeID}, ${OLEObjectID}, ${OLEObjectRid}
String ml = "<w:p xmlns:w=\"\" xmlns:r=\"\" xmlns:wp=\"\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><w:r><w:object w:dxaOrig=\"11881\" w:dyaOrig=\"9181\"><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\" style=\"width:594pt;height:459pt\" o:ole=\"\"><v:imagedata r:id=\"${ImageId}\" o:title=\"\"/></v:shape><o:OLEObject Type=\"Embed\" ProgID=\"AcroExch.Document.7\" ShapeID=\"${OLEShapeID}\" DrawAspect=\"Content\" ObjectID=\"${OLEObjectID}\" r:id=\"${OLEObjectRid}\"/></w:object></w:r></w:p>";

java.util.HashMap<String, String>mappings = new java.util.HashMap<String, String>();
mappings.put("ImageId", relImage.getId() );
mappings.put("OLEShapeID", "_x0000_i1025" );
mappings.put("OLEObjectID", "_1291469606" );
mappings.put("OLEObjectRid", relOleObject.getId() );

try {
org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings ) );
} catch (JAXBException e) {
// TODO Auto-generated catch block

ContentTypeManager ctm = template.getContentTypeManager();

// There as an override, try adding as a default!

Is it necessary the Ole Helper to do it?

The second question is over Table of Contents. I add text and images in docx, and the sections number is update. How modify the TOC? I did not find how to do it.

Thank you very much.

Re: Insert PDF and update TOC

PostPosted: Fri Jul 28, 2017 10:07 pm
by castillo
More info:
The document is created from other docx with table of contents and sections.

I tested this:
TocGenerator tocGenerator = new TocGenerator(template);
tocGenerator.updateToc( false);

and it generates a exception:
org.docx4j.toc.TocException: No ToC content control found
at org.docx4j.toc.TocGenerator.updateToc(
at org.docx4j.toc.TocGenerator.updateToc(
at org.docx4j.toc.TocGenerator.updateToc(

Thanks for your help.

Re: Insert PDF and update TOC

PostPosted: Wed Aug 02, 2017 11:45 am
by jason
Hi Castillo

Regarding inserting a PDF in a docx: using the OLE Helper in Plutext's Docx4j Enterprise Edition is certainly the easiest way to do it, and tested across various versions of Word. That said, there is nothing stopping you from doing it yourself using docx4j (after all, that's what the Enterprise Ed builds on). To start with, you might want to check whether the PDF is stored as-is in the OLE part, or whether Word requires expects something different in the OLE part.

Regarding updating ToC: please see the Toc* samples, at ... j/samples/

You need something like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
tocGenerator.generateToc( 0,    "TOC \\o \"1-3\" \\h \\z \\u ", true);
Parsed in 0.014 seconds, using GeSHi

to create the ToC, before you call updateToc

cheers .. Jason
ps in future, please create separate topics for distinct subjects, thanks.