use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart 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.");
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project TranskribusCore by Transkribus.
the class CoreUtils method extractTextFromDocx.
/**
* FIXME not sure if this method does extract text from every possible variation of a docx...
*/
public static String extractTextFromDocx(String filename) throws Docx4JException, JAXBException {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(filename));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
String pageTxt = "";
for (Object o : documentPart.getContent()) {
if (o == null)
continue;
pageTxt += o.toString() + "\n";
}
return StringUtils.stripEnd(pageTxt, "\n");
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.
the class ReadFile method main.
public static void main(String[] args) throws Docx4JException, JAXBException {
File doc = new File("output/welcome1.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
// get all text nodes from the main document part
String textNodesXPath = "//w:t";
List<Object> textNodes = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
for (Object obj : textNodes) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
System.out.println(textValue);
}
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.
the class Write_Format method main.
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Welcome To Baeldung");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
BooleanDefaultTrue b = new BooleanDefaultTrue();
rpr.setB(b);
rpr.setI(b);
rpr.setCaps(b);
Color green = factory.createColor();
green.setVal("green");
rpr.setColor(green);
r.setRPr(rpr);
mainDocumentPart.getContent().add(p);
File exportFile = new File("output/welcome2.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
use of org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart in project Java-Tutorial by gpcodervn.
the class Write_HelloWorld method main.
public static void main(String[] args) throws Docx4JException {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung");
File exportFile = new File("output/welcome1.docx");
wordPackage.save(exportFile);
System.out.println("Done!");
}
Aggregations