Search in sources :

Example 1 with DocumentSettingsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart 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 DocumentSettingsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart 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)

Example 3 with DocumentSettingsPart

use of org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart in project flexmark-java by vsch.

the class DocxContextImpl method getFootnotesPart.

// @formatter:off
// static String footnotePartXML = "<w:footnotes mc:Ignorable=\"w14 wp14\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\">"
// + "<w:footnote w:id=\"-1\" w:type=\"separator\">"  // matching CTFtnDocProps below
// + "<w:p>"
// + "<w:pPr>"
// + "<w:spacing w:after=\"0\" w:line=\"240\" w:lineRule=\"auto\"/>"
// +"</w:pPr>"
// + "<w:r>"
// + "<w:separator/>"
// +"</w:r>"
// +"</w:p>"
// +"</w:footnote>"
// + "<w:footnote w:id=\"0\" w:type=\"continuationSeparator\">"
// + "<w:p>"
// + "<w:pPr>"
// + "<w:spacing w:after=\"0\" w:line=\"240\" w:lineRule=\"auto\"/>"
// +"</w:pPr>"
// + "<w:r>"
// + "<w:continuationSeparator/>"
// +"</w:r>"
// +"</w:p>"
// +"</w:footnote>"
// +"</w:footnotes>";
// @formatter:on
@Override
public FootnotesPart getFootnotesPart() throws Docx4JException {
    // Setup FootnotesPart if necessary,
    // along with DocumentSettings
    FootnotesPart footnotesPart = myDocumentPart.getFootnotesPart();
    if (footnotesPart == null) {
        // that'll be the case in this example
        // initialise it
        footnotesPart = new FootnotesPart();
        myDocumentPart.addTargetPart(footnotesPart);
        // CTFootnotes footnotes = null;
        // footnotes = (CTFootnotes) XmlUtils.unwrap(XmlUtils.unmarshalString(footnotePartXML));
        // footnotesPart.setJaxbElement(footnotes);
        CTFootnotes footnotes = myFactory.createCTFootnotes();
        // JAXBElement<CTFootnotes> footnotesWrapped = myFactory.createFootnotes(footnotes);
        // Create object for footnote
        CTFtnEdn ftnedn = myFactory.createCTFtnEdn();
        footnotes.getFootnote().add(ftnedn);
        ftnedn.setId(BigInteger.valueOf(-1));
        ftnedn.setType(org.docx4j.wml.STFtnEdn.SEPARATOR);
        // Create object for p
        P p = myFactory.createP();
        ftnedn.getContent().add(p);
        // Create object for r
        R r = myFactory.createR();
        p.getContent().add(r);
        // Create object for separator (wrapped in JAXBElement)
        Separator rseparator = myFactory.createRSeparator();
        JAXBElement<Separator> rseparatorWrapped = myFactory.createRSeparator(rseparator);
        r.getContent().add(rseparatorWrapped);
        // Create object for footnote
        CTFtnEdn ftnedn2 = myFactory.createCTFtnEdn();
        footnotes.getFootnote().add(ftnedn2);
        ftnedn2.setId(BigInteger.valueOf(0));
        ftnedn2.setType(org.docx4j.wml.STFtnEdn.CONTINUATION_SEPARATOR);
        // Create object for p
        P p2 = myFactory.createP();
        ftnedn2.getContent().add(p2);
        // Create object for r
        R r2 = myFactory.createR();
        p2.getContent().add(r2);
        // Create object for continuationSeparator (wrapped in JAXBElement)
        ContinuationSeparator rcontinuationseparator = myFactory.createRContinuationSeparator();
        JAXBElement<ContinuationSeparator> rcontinuationseparatorWrapped = myFactory.createRContinuationSeparator(rcontinuationseparator);
        r2.getContent().add(rcontinuationseparatorWrapped);
        footnotesPart.setJaxbElement(footnotes);
        // Usually the settings part contains footnote properties;
        // so add these if not present
        DocumentSettingsPart dsp = myDocumentPart.getDocumentSettingsPart();
        if (dsp == null) {
            // create it
            dsp = new DocumentSettingsPart();
            myDocumentPart.addTargetPart(dsp);
        }
        CTSettings settings = dsp.getContents();
        if (settings == null) {
            settings = myFactory.createCTSettings();
            dsp.setJaxbElement(settings);
        }
        CTFtnDocProps ftndocprops = settings.getFootnotePr();
        if (ftndocprops == null) {
            // String openXML = "<w:footnotePr xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
            // + "<w:footnote w:id=\"-1\"/>" // these 2 numbers are special, and correspond with string footnotePartXML above
            // + "<w:footnote w:id=\"0\"/>"
            // + "</w:footnotePr>";
            // settings.setFootnotePr((CTFtnDocProps) XmlUtils.unmarshalString(openXML, Context.jc, CTFtnDocProps.class));
            ftndocprops = myFactory.createCTFtnDocProps();
            CTFtnEdnSepRef sepRef = myFactory.createCTFtnEdnSepRef();
            sepRef.setId(BigInteger.valueOf(-1));
            ftndocprops.getFootnote().add(sepRef);
            sepRef = myFactory.createCTFtnEdnSepRef();
            sepRef.setId(BigInteger.valueOf(0));
            ftndocprops.getFootnote().add(sepRef);
            settings.setFootnotePr(ftndocprops);
        }
    }
    return footnotesPart;
}
Also used : ContinuationSeparator(org.docx4j.wml.R.ContinuationSeparator) DocumentSettingsPart(org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart) FootnotesPart(org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart) ContinuationSeparator(org.docx4j.wml.R.ContinuationSeparator) Separator(org.docx4j.wml.R.Separator)

Aggregations

DocumentSettingsPart (org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart)3 FootnotesPart (org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart)3 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 ContinuationSeparator (org.docx4j.wml.R.ContinuationSeparator)1 Separator (org.docx4j.wml.R.Separator)1 Text (org.docx4j.wml.Text)1