Page 1 of 1

Insert PDF and update TOC

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

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();
olePart.setBinaryData(is);
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();
imagePart.setBinaryData(isImage);
Relationship relImage = template.getMainDocumentPart().addTargetPart(imagePart); // imagePart defined outside this snippet

// Contains ${ImageId}, ${OLEShapeID}, ${OLEObjectID}, ${OLEObjectRid}
String ml = "<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" 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 {
template.getMainDocumentPart().addObject(
org.docx4j.XmlUtils.unmarshallFromTemplate(ml, mappings ) );
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ContentTypeManager ctm = template.getContentTypeManager();

// There as an override, try adding as a default!
ctm.addDefaultContentType("bin",
org.docx4j.openpackaging.contenttype.ContentTypes.OFFICEDOCUMENT_OLE_OBJECT);


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.
Castillo

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(TocGenerator.java:535)
at org.docx4j.toc.TocGenerator.updateToc(TocGenerator.java:509)
at org.docx4j.toc.TocGenerator.updateToc(TocGenerator.java:489)

Thanks for your help.
Regards.

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 https://github.com/plutext/docx4j/blob/ ... 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.015 seconds, using GeSHi 1.0.8.4


to create the ToC, before you call updateToc

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