I would like to convert files from docx format to pdf. I made a pilot project to test it.
The source docx is p1 (I replace things and write sample1.docx with the new content).
This is what I have:
-jdk-1.6 update 21
-docx4j-nightly-20100914.jar
- Code: Select all
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hu.bluesystem.docman;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.JAXBElement;
import org.docx4j.XmlUtils;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Body;
/**
* Creates a WordprocessingML document from scratch.
*
* @author Jason Harrop
* @version 1.0
*/
public class CreateWordprocessingMLDocument extends AbstractSample {
public static void main(String[] args) throws Exception {
Calendar c2 = Calendar.getInstance();
FileOutputStream outxml = new FileOutputStream (new File("c:\\out.xml"));
inputfilepath = "c:\\p1.docx";
outputfilepath = "c:\\sample1.docx";
WordprocessingMLPackage wmin = WordprocessingMLPackage.load(new File(inputfilepath));
MainDocumentPart documentPart = wmin.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
Body body = wmlDocumentEl.getBody();
String xml = XmlUtils.marshaltoString(body, true);
outxml.write(xml.getBytes());
outxml.close();
List<Object> bodyChildren = body.getEGBlockLevelElts();
walkJAXBElements(bodyChildren);
// Mapper fontMapper = new IdentityPlusMapper();
//WordprocessingMLPackage.setFontMapper(fontMapper);
SaveToZipFile saver = new SaveToZipFile(wmin);
saver.save(outputfilepath);
org.docx4j.convert.out.pdf.PdfConversion c
= new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wmin);
// = new org.docx4j.convert.out.pdf.viaIText.Conversion(wordMLPackage);
((org.docx4j.convert.out.pdf.viaXSLFO.Conversion) c).setSaveFO(new java.io.File("c:\\sample.fo"));
OutputStream os = new java.io.FileOutputStream("c:\\sample.pdf");
c.output(os);
Calendar c1 = Calendar.getInstance();
System.out.println("Time is : " + (c1.getTime().getTime() - c2.getTime().getTime()));
//System.out.println(xml);
}
static void walkJAXBElements(List<Object> bodyChildren) {
for (Object o : bodyChildren) {
if (o instanceof org.docx4j.wml.P) {
walkList(((org.docx4j.wml.P) o).getParagraphContent());
}
if (o instanceof javax.xml.bind.JAXBElement) {
if (((JAXBElement) o).getDeclaredType().toString().equals("class org.docx4j.wml.Tbl")) {
org.docx4j.wml.Tbl t = (org.docx4j.wml.Tbl) ((JAXBElement) o).getValue();
listtbl(t);
}
}
}
}
static void listtbl (org.docx4j.wml.Tbl tbl) {
for (Object o : tbl.getEGContentRowContent()) {
if (o instanceof org.docx4j.wml.Tr) {
org.docx4j.wml.Tr tr = (org.docx4j.wml.Tr) o;
for (Object o2 : tr.getEGContentCellContent()) {
//System.out.println(" " + o2.getClass().getName());
if (o2 instanceof javax.xml.bind.JAXBElement) {
if (((JAXBElement) o2).getDeclaredType().getName().equals("org.docx4j.wml.Tc")) {
org.docx4j.wml.Tc tc = (org.docx4j.wml.Tc) ((JAXBElement) o2).getValue();
// Look at the paragraphs in the tc
walkJAXBElements(tc.getEGBlockLevelElts());
} else {
// What is it, if it isn't a Tc?
System.out.println(" " + ((JAXBElement) o).getName());
System.out.println(" " + ((JAXBElement) o).getDeclaredType().getName());
}
} else {
System.out.println(" " + o.getClass().getName());
}
}
} else {
System.out.println(" " + o.getClass().getName());
}
}
}
static void walkList(List children) {
HashMap hm = new HashMap();
hm.put("[CEGNEVE]",".......");
hm.put("[CEGCIME]","hggg");
hm.put("[CEGADOSZAM]","21111");
Set set = hm.entrySet();
for (Object o : children) {
if (o instanceof javax.xml.bind.JAXBElement) {
// System.out.println(((JAXBElement) o).getDeclaredType().getName());
if (((JAXBElement) o).getDeclaredType().getName().equals("org.docx4j.wml.Text")) {
org.docx4j.wml.Text t = (org.docx4j.wml.Text) ((JAXBElement) o).getValue();
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me=(Map.Entry)i.next();
if (t.getValue().equals(me.getKey())) {
t.setValue(me.getValue().toString());
}
}
}
} else if (o instanceof org.docx4j.wml.R) {
org.docx4j.wml.R run = (org.docx4j.wml.R) o;
walkList(run.getRunContent());
}
}
}
}
If I run this I get the following:
run:
JAXB: RI not present. Trying Java 6 implementation.
JAXB: Using Java 6 implementation.
log4j:WARN No appenders could be found for logger (org.docx4j.jaxb.Context).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.docx4j.openpackaging.exceptions.Docx4JException: FOP issues
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:370)
at hu.bluesystem.docman.CreateWordprocessingMLDocument.main(CreateWordprocessingMLDocument.java:71)
Caused by: java.lang.NullPointerException
at org.docx4j.model.PropertyResolver.init(PropertyResolver.java:193)
at org.docx4j.model.PropertyResolver.<init>(PropertyResolver.java:161)
at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getPropertyResolver(MainDocumentPart.java:177)
at org.docx4j.openpackaging.packages.WordprocessingMLPackage.getDefaultFont(WordprocessingMLPackage.java:368)
at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.fontsInUse(MainDocumentPart.java:341)
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.declareFonts(Conversion.java:145)
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:250)
... 1 more
Java Result: 1
I really appreciate if someone could point to the problem.
Thank you in advance!
Sandor