Search in sources :

Example 1 with CTFootnotes

use of org.docx4j.wml.CTFootnotes in project TranskribusCore by Transkribus.

the class DocxBuilder method createFootnote.

public static void createFootnote(String fnComment, R r, MainDocumentPart mdp) throws Exception {
    // Setup FootnotesPart if necessary,
    // along with DocumentSettings
    FootnotesPart footnotesPart = mdp.getFootnotesPart();
    if (footnotesPart == null) {
        // that'll be the case in this example
        // initialise it
        footnotesPart = new FootnotesPart();
        mdp.addTargetPart(footnotesPart);
        CTFootnotes footnotes = (CTFootnotes) XmlUtils.unwrap(XmlUtils.unmarshalString(footnotePartXML));
        footnotesPart.setJaxbElement(footnotes);
        // Usually the settings part contains footnote properties;
        // so add these if not present
        DocumentSettingsPart dsp = mdp.getDocumentSettingsPart();
        if (dsp == null) {
            // create it
            dsp = new DocumentSettingsPart();
            mdp.addTargetPart(dsp);
        }
        CTSettings settings = dsp.getContents();
        if (settings == null) {
            settings = wmlObjectFactory.createCTSettings();
            dsp.setJaxbElement(settings);
        }
        CTFtnDocProps ftndocprops = settings.getFootnotePr();
        if (ftndocprops == null) {
            String openXML = "<w:footnotePr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + // these 2 numbers are special, and correspond with string footnotePartXML below
            "<w:footnote w:id=\"-1\"/>" + "<w:footnote w:id=\"0\"/>" + "</w:footnotePr>";
            settings.setFootnotePr((CTFtnDocProps) XmlUtils.unmarshalString(openXML, Context.jc, CTFtnDocProps.class));
        }
    }
    mdp.getPropertyResolver().activateStyle("FootnoteReference");
    // mdp.getPropertyResolver().activateStyle("FootnoteText");
    // OK, add a footnote
    addFootnote(footnoteCounter++, fnComment, footnotesPart, r);
// Note: your footnote ids must be distinct; they don't need to be ordered (though Word will do that when you open the docx)
}
Also used : DocumentSettingsPart(org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart) CTFootnotes(org.docx4j.wml.CTFootnotes) FootnotesPart(org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart) CTFtnDocProps(org.docx4j.wml.CTFtnDocProps) CTSettings(org.docx4j.wml.CTSettings)

Example 2 with CTFootnotes

use of org.docx4j.wml.CTFootnotes in project TranskribusCore by Transkribus.

the class DocxBuilder method main.

public static void main(String[] args) throws Exception {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
    // Setup FootnotesPart if necessary,
    // along with DocumentSettings
    FootnotesPart footnotesPart = mdp.getFootnotesPart();
    if (footnotesPart == null) {
        // that'll be the case in this example
        // initialise it
        footnotesPart = new FootnotesPart();
        mdp.addTargetPart(footnotesPart);
        CTFootnotes footnotes = (CTFootnotes) XmlUtils.unwrap(XmlUtils.unmarshalString(footnotePartXML));
        footnotesPart.setJaxbElement(footnotes);
        // Usually the settings part contains footnote properties;
        // so add these if not present
        DocumentSettingsPart dsp = mdp.getDocumentSettingsPart();
        if (dsp == null) {
            // create it
            dsp = new DocumentSettingsPart();
            mdp.addTargetPart(dsp);
        }
        CTSettings settings = dsp.getContents();
        if (settings == null) {
            settings = wmlObjectFactory.createCTSettings();
            dsp.setJaxbElement(settings);
        }
        CTFtnDocProps ftndocprops = settings.getFootnotePr();
        if (ftndocprops == null) {
            String openXML = "<w:footnotePr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + // these 2 numbers are special, and correspond with string footnotePartXML below
            "<w:footnote w:id=\"-1\"/>" + "<w:footnote w:id=\"0\"/>" + "</w:footnotePr>";
            settings.setFootnotePr((CTFtnDocProps) XmlUtils.unmarshalString(openXML, Context.jc, CTFtnDocProps.class));
        }
    }
    // Example
    // Create and add p
    org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
    org.docx4j.wml.P p = factory.createP();
    mdp.getContent().add(p);
    // Add a run
    R r = new R();
    p.getContent().add(r);
    org.docx4j.wml.Text t = factory.createText();
    t.setValue("Hello world");
    r.getContent().add(t);
    // OK, add a footnote
    addFootnote(1, "my footnote", footnotesPart, r);
    // Note: your footnote ids must be distinct; they don't need to be ordered (though Word will do that when you open the docx)
    // Save it
    wordMLPackage.save(new java.io.File("C:/Users/Administrator/footnoteTest2.docx"));
    System.out.println("Saved " + "C:/Users/Administrator/footnoteTest2.docx");
/*
		 * add comments example: Kommentar auf der rechten Seite des Dokuments
		 */
// WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// 
// // Create and add a Comments Part
// CommentsPart cp = new CommentsPart();
// wordMLPackage.getMainDocumentPart().addTargetPart(cp);
// 
// // Part must have minimal contents
// Comments comments = factory.createComments();
// cp.setJaxbElement(comments);
// 
// // Add a comment to the comments part
// java.math.BigInteger commentId = BigInteger.valueOf(0);
// Comment theComment = createComment(commentId, "fred", null,
// "my first comment");
// comments.getComment().add(theComment);
// 
// // Add comment reference to document
// //P paraToCommentOn = wordMLPackage.getMainDocumentPart().addParagraphOfText("here is some content");
// P p = new P();
// 
// wordMLPackage.getMainDocumentPart().getContent().add(p);
// 
// // Create object for commentRangeStart
// CommentRangeStart commentrangestart = factory.createCommentRangeStart();
// commentrangestart.setId( commentId );  // substitute your comment id
// 
// 
// // The actual content, in the middle
// p.getContent().add(commentrangestart);
// 
// org.docx4j.wml.Text  t = factory.createText();
// t.setValue("hello");
// 
// org.docx4j.wml.R  run = factory.createR();
// run.getContent().add(t);
// 
// p.getContent().add(run);
// 
// // Create object for commentRangeEnd
// CommentRangeEnd commentrangeend = factory.createCommentRangeEnd();
// commentrangeend.setId( commentId );  // substitute your comment id
// 
// p.getContent().add(commentrangeend);
// 
// p.getContent().add(createRunCommentReference(commentId));
// 
// System.out.println(wordMLPackage.getMainDocumentPart().getXML());
// 
// 
// // ++, for next comment ...
// commentId = commentId.add(java.math.BigInteger.ONE);
// 
// wordMLPackage.save(new java.io.File("C:/Users/Administrator/commentTest.docx") );
// System.out.println("Saved " + "C:/Users/Administrator/commentTest.docx");
// 
// 
// System.out.println("Done.");
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) WordprocessingMLPackage(org.docx4j.openpackaging.packages.WordprocessingMLPackage) File(java.io.File) R(org.docx4j.wml.R) DocumentSettingsPart(org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart) CTFootnotes(org.docx4j.wml.CTFootnotes) FootnotesPart(org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart) CTFtnDocProps(org.docx4j.wml.CTFtnDocProps) CTSettings(org.docx4j.wml.CTSettings) Text(org.docx4j.wml.Text) P(org.docx4j.wml.P)

Aggregations

DocumentSettingsPart (org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart)2 FootnotesPart (org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart)2 CTFootnotes (org.docx4j.wml.CTFootnotes)2 CTFtnDocProps (org.docx4j.wml.CTFtnDocProps)2 CTSettings (org.docx4j.wml.CTSettings)2 File (java.io.File)1 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)1 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)1 P (org.docx4j.wml.P)1 R (org.docx4j.wml.R)1 Text (org.docx4j.wml.Text)1