Create table in word doc using docx4j in specific bookmark b
Posted:
Wed Mar 04, 2015 8:50 pm
by tariq
Hello Jason,
I’m creating table in specific bookmark location (means its display table in word doc), but after converting word to PDF it can't show table in PDF.
Thanks in Advance.
Re: Create table in word doc using docx4j in specific bookma
Posted:
Wed Mar 04, 2015 10:20 pm
by jason
What does the resulting XML look like?
I hope your table is not nested inside a paragraph, since that's not allowed...
Re: Create table in word doc using docx4j in specific bookma
Posted:
Wed Mar 04, 2015 11:41 pm
by tariq
table is not nested, its just single bookmark replace into one table in single paragraph.
my code is here.
- Code: Select all
private void replaceTextWithBookmark() throws Exception {
Map<DataFieldName, String> map = new HashMap<DataFieldName, String>();
map.put( new DataFieldName("abc"), "Testing");
wordMLPackage = WordprocessingMLPackage.load(new File("C:\\BookMarks.docx"));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
Document wmlDocumentEl = (Document) documentPart.getJaxbElement();
Body body = wmlDocumentEl.getBody();
replaceBookmarkContents(body.getContent(), map);
wordMLPackage.save(new File("C:\\HelloWorld.docx"));
ConvertOutPDF.convertPdf(wordMLPackage);
System.out.println("Done");
}
private void replaceBookmarkContents(List<Object> paragraphs, Map<DataFieldName, String> data) throws Exception {
Tbl table = TblFactory.createTable(3,3,9600);
RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange");
new TraversalUtil(paragraphs, rt);
for (CTBookmark bm : rt.getStarts()) {
// do we have data for this one?
if (bm.getName()==null) continue;
String value = data.get(new DataFieldName(bm.getName()));
if (value==null) continue;
try {
// Can't just remove the object from the parent,
// since in the parent, it may be wrapped in a JAXBElement
List<Object> theList = null;
if (bm.getParent() instanceof P) {
System.out.println("OK!");
theList = ((ContentAccessor)(bm.getParent())).getContent();
} else {
continue;
}
int rangeStart = -1;
int rangeEnd=-1;
int i = 0;
for (Object ox : theList) {
Object listEntry = XmlUtils.unwrap(ox);
if (listEntry.equals(bm)) {
if (DELETE_BOOKMARK) {
rangeStart=i;
} else {
rangeStart=i+1;
}
} else if (listEntry instanceof CTMarkupRange) {
if ( ((CTMarkupRange)listEntry).getId().equals(bm.getId())) {
if (DELETE_BOOKMARK) {
rangeEnd=i;
} else {
rangeEnd=i-1;
}
break;
}
}
i++;
}
if (rangeStart>0) {
// Delete the bookmark range
for (int j=rangeStart; j>0; j--) {
theList.remove(j);
}
// now add a run
org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(table);
t.setValue(value);
theList.add(rangeStart, run);
}
} catch (ClassCastException cce) {
log.error(cce.getMessage(), cce);
}
}
}
public static void convertPdf(WordprocessingMLPackage wordMLPackage) throws Exception {
String regex = null;
PhysicalFonts.setRegex(regex);
// System.out.println("Loading file from " + inputfilepath);
// wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font = PhysicalFonts.get("Arial Unicode MS");
if (font!=null) {
fontMapper.put("Times New Roman", font);
fontMapper.put("Arial", font);
}
fontMapper.put("Libian SC Regular", PhysicalFonts.get("SimSun"));
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
String outputfilepath = "c:\\HelloWord4.pdf";
OutputStream os = new java.io.FileOutputStream(outputfilepath);
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
System.out.println("Saved: " + outputfilepath);
}