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