.Word found unreadable content in ....
- Code: Select all
import java.io.File;
import java.math.BigInteger;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTBorder;
import org.docx4j.wml.CTColumns;
import org.docx4j.wml.CTCompat;
import org.docx4j.wml.CTDocGrid;
import org.docx4j.wml.CTHeight;
import org.docx4j.wml.CTLanguage;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.CTShd;
import org.docx4j.wml.CTTblLook;
import org.docx4j.wml.CTTblPrBase;
import org.docx4j.wml.CTTblPrBase.TblStyle;
import org.docx4j.wml.CTVerticalJc;
import org.docx4j.wml.Color;
import org.docx4j.wml.Document;
import org.docx4j.wml.FooterReference;
import org.docx4j.wml.Ftr;
import org.docx4j.wml.HeaderReference;
import org.docx4j.wml.HpsMeasure;
import org.docx4j.wml.Jc;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.P.Hyperlink;
import org.docx4j.wml.PPr;
import org.docx4j.wml.PPrBase;
import org.docx4j.wml.PPrBase.NumPr;
import org.docx4j.wml.PPrBase.NumPr.Ilvl;
import org.docx4j.wml.PPrBase.NumPr.NumId;
import org.docx4j.wml.PPrBase.OutlineLvl;
import org.docx4j.wml.PPrBase.PStyle;
import org.docx4j.wml.PPrBase.Spacing;
import org.docx4j.wml.ParaRPr;
import org.docx4j.wml.R;
import org.docx4j.wml.RFonts;
import org.docx4j.wml.RPr;
import org.docx4j.wml.RStyle;
import org.docx4j.wml.SectPr;
import org.docx4j.wml.SectPr.PgMar;
import org.docx4j.wml.SectPr.PgSz;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.TblBorders;
import org.docx4j.wml.TblGrid;
import org.docx4j.wml.TblGridCol;
import org.docx4j.wml.TblPr;
import org.docx4j.wml.TblWidth;
import org.docx4j.wml.Tc;
import org.docx4j.wml.TcPr;
import org.docx4j.wml.TcPrInner;
import org.docx4j.wml.TcPrInner.GridSpan;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
import org.docx4j.wml.TrPr;
import org.docx4j.wml.U;
public class Foo {
public static void main(String[] args) throws Exception {
// // Create the package
// WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
//
// // Create the main document part (word/document.xml)
// MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart();
//
// // Compatibility
// DocumentSettingsPart dsp = wordDocumentPart.getDocumentSettingsPart(true);
// CTCompat compat = Context.getWmlObjectFactory().createCTCompat();
// dsp.getContents().setCompat(compat);
// compat.setCompatSetting("compatibilityMode", "http://schemas.microsoft.com/office/word", "15");
//
// // Create main document part content
// // ObjectFactory factory = Context.getWmlObjectFactory();
// // org.docx4j.wml.Body body = factory.createBody();
// //org.docx4j.wml.Document wmlDocumentEl = createIt();
// // wmlDocumentEl.setBody(body);
//
// // Put the content in the part
// //wordDocumentPart.setJaxbElement(wmlDocumentEl);
//
// // Add the main document part to the package relationships// (creating it if
// // necessary)
// //wordMLPackage.addTargetPart(wordDocumentPart);
// wordDocumentPart.addParagraphOfText("Test 123");
// //wordDocumentPart.addTargetPart(createIt());
//
// // Save it
// wordMLPackage.save(new java.io.File("helloworld.docx"));
// Create the package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// Create the main document part (word/document.xml)
MainDocumentPart wordDocumentPart = wordMLPackage.getMainDocumentPart();
ObjectFactory factory = Context.getWmlObjectFactory();
// Compatibility
DocumentSettingsPart dsp = wordDocumentPart.getDocumentSettingsPart(true);
CTCompat compat = factory.createCTCompat();
dsp.getContents().setCompat(compat);
compat.setCompatSetting("compatibilityMode", "http://schemas.microsoft.com/office/word", "15");
// Create main document part content
org.docx4j.wml.Body body = factory.createBody();
Document wmlDocumentEl = factory.createDocument();
wmlDocumentEl.setBody(body);
wmlDocumentEl.getContent().add(createIt2(factory));
// Put the content in the part
wordDocumentPart.setJaxbElement(wmlDocumentEl);
// Add the main document part to the package relationships
// (creating it if necessary)
wordMLPackage.addTargetPart(wordDocumentPart);
wordMLPackage.save(new java.io.File("helloworld.docx") );
}
public static Document createIt(ObjectFactory wmlObjectFactory) {
Document document = wmlObjectFactory.createDocument();
// Create object for body
Body body = wmlObjectFactory.createBody();
document.setBody(body);
// Create object for sectPr
SectPr sectpr = wmlObjectFactory.createSectPr();
body.setSectPr(sectpr);
// Create object for pgSz
SectPr.PgSz sectprpgsz = wmlObjectFactory.createSectPrPgSz();
sectpr.setPgSz(sectprpgsz);
sectprpgsz.setW(BigInteger.valueOf(12240));
sectprpgsz.setH(BigInteger.valueOf(15840));
// Create object for pgMar
SectPr.PgMar sectprpgmar = wmlObjectFactory.createSectPrPgMar();
sectpr.setPgMar(sectprpgmar);
sectprpgmar.setFooter(BigInteger.valueOf(708));
sectprpgmar.setLeft(BigInteger.valueOf(1440));
sectprpgmar.setRight(BigInteger.valueOf(1440));
sectprpgmar.setTop(BigInteger.valueOf(1440));
sectprpgmar.setBottom(BigInteger.valueOf(1440));
sectprpgmar.setGutter(BigInteger.valueOf(0));
sectprpgmar.setHeader(BigInteger.valueOf(708));
// Create object for headerReference
HeaderReference headerreference = wmlObjectFactory.createHeaderReference();
sectpr.getEGHdrFtrReferences().add(headerreference);
headerreference.setType(org.docx4j.wml.HdrFtrRef.EVEN);
headerreference.setId("rId6");
// Create object for headerReference
HeaderReference headerreference2 = wmlObjectFactory.createHeaderReference();
sectpr.getEGHdrFtrReferences().add(headerreference2);
headerreference2.setType(org.docx4j.wml.HdrFtrRef.DEFAULT);
headerreference2.setId("rId7");
// Create object for footerReference
FooterReference footerreference = wmlObjectFactory.createFooterReference();
sectpr.getEGHdrFtrReferences().add(footerreference);
footerreference.setType(org.docx4j.wml.HdrFtrRef.EVEN);
footerreference.setId("rId8");
// Create object for footerReference
FooterReference footerreference2 = wmlObjectFactory.createFooterReference();
sectpr.getEGHdrFtrReferences().add(footerreference2);
footerreference2.setType(org.docx4j.wml.HdrFtrRef.DEFAULT);
footerreference2.setId("rId9");
// Create object for headerReference
HeaderReference headerreference3 = wmlObjectFactory.createHeaderReference();
sectpr.getEGHdrFtrReferences().add(headerreference3);
headerreference3.setType(org.docx4j.wml.HdrFtrRef.FIRST);
headerreference3.setId("rId10");
// Create object for footerReference
FooterReference footerreference3 = wmlObjectFactory.createFooterReference();
sectpr.getEGHdrFtrReferences().add(footerreference3);
footerreference3.setType(org.docx4j.wml.HdrFtrRef.FIRST);
footerreference3.setId("rId11");
// Create object for cols
CTColumns columns = wmlObjectFactory.createCTColumns();
sectpr.setCols(columns);
columns.setSpace(BigInteger.valueOf(708));
// Create object for docGrid
CTDocGrid docgrid = wmlObjectFactory.createCTDocGrid();
sectpr.setDocGrid(docgrid);
docgrid.setLinePitch(BigInteger.valueOf(360));
// Create object for p
P p = wmlObjectFactory.createP();
body.getContent().add(p);
p.setParaId("2F6BFB4B");
p.setTextId("0AD718C0");
document.setIgnorable("w14 w15 w16se w16cid w16 w16cex wp14");
return document;
}
public static Ftr createIt2(ObjectFactory wmlObjectFactory) {
Ftr ftr = wmlObjectFactory.createFtr();
ftr.setIgnorable("w14 w15 w16se w16cid w16 w16cex wp14");
// Create object for p
P p = wmlObjectFactory.createP();
ftr.getContent().add(p);
// Create object for pPr
PPr ppr = wmlObjectFactory.createPPr();
p.setPPr(ppr);
// Create object for pStyle
PPrBase.PStyle pprbasepstyle = wmlObjectFactory.createPPrBasePStyle();
ppr.setPStyle(pprbasepstyle);
pprbasepstyle.setVal("Footer");
p.setParaId("25EE6A9E");
p.setTextId("77777777");
return ftr;
}
}