It is working perfect if header/footer contains only text.
But if header/footer contains any shape (roundrect in this case) then headerPart.getJaxbElement() is giving this error
- Code: Select all
12-12 17:21:38.875 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.o*.p*.JaxbXmlPart: Lazily unmarshalling /word/header2.xml
12-12 17:21:38.875 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.o*.p*.JaxbXmlPar*: For org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart, unmarshall (no binder)
[ 12-12 17:21:38.885 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.885 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
[ 12-12 17:21:38.895 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.895 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
[ 12-12 17:21:38.895 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.895 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
[ 12-12 17:21:38.895 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.895 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:38.905 6222-7005/docx4j.org.docx4j4android4 W/o*.d*.j*.JaxbValidatio*: [ERROR] : unexpected element (uri:"http://schemas.openxmlformats.org/markup-compatibility/2006", local:"AlternateContent"). Expect
12-12 17:21:38.905 6222-7005/docx4j.org.docx4j4android4 I/o*.d*.j*.JaxbValidatio*: continuing (with possible element/attribute loss)
12-12 17:21:38.905 6222-7005/docx4j.org.docx4j4android4 I/o*.d*.o*.p*.JaxbXmlPar*: encountered unexpected content in /word/header2.xml; pre-processing
12-12 17:21:38.915 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.u*.ResourceUtils: docx4j.jaxb.JaxbValidationEventHandler resolved to custom-preprocessor.xslt
12-12 17:21:38.915 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.u*.ResourceUtils: Attempting to load: custom-preprocessor.xslt
[ 12-12 17:21:38.915 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.915 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
[ 12-12 17:21:38.915 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.915 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:38.915 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.u*.ResourceUtils: Trying Thread.currentThread().getContextClassLoader()
[ 12-12 17:21:38.925 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 W/o*.d*.u*.ResourceUtils: Couldn't get resource: custom-preprocessor.xslt
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 W/o*.d*.u*.ResourceUtils: custom-preprocessor.xslt: custom-preprocessor.xslt not found via classloader.
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 W/o*.d*.u*.ResourceUtils: Property docx4j.jaxb.JaxbValidationEventHandler resolved to missing resource custom-preprocessor.xslt; using org/docx4j/jaxb/mc-preprocessor.xslt
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 D/o*.d*.u*.ResourceUtils: Attempting to load: org/docx4j/jaxb/mc-preprocessor.xslt
[ 12-12 17:21:38.925 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:38.925 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:39.015 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:39.025 6222-7005/docx4j.org.docx4j4android4 I/org.docx4j.XmlUtils: Using org.apache.xalan.transformer.TransformerImpl
12-12 17:21:39.025 6222-7005/docx4j.org.docx4j4android4 I/org.docx4j.XmlUtils: Working around https://issues.apache.org/jira/browse/XALANJ-2419
12-12 17:21:39.025 6222-7005/docx4j.org.docx4j4android4 I/System.out: method: xml
[ 12-12 17:21:39.045 6222: 7005 W/ ]
Unable to open '/system/framework/qcom.fmradio.jar': No such file or directory
12-12 17:21:39.045 6222-7005/docx4j.org.docx4j4android4 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
12-12 17:21:39.055 6222-7005/docx4j.org.docx4j4android4 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
Process: docx4j.org.docx4j4android4, PID: 6222
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalAccessError: Method 'java.lang.Class org.apache.xalan.extensions.ExtensionHandler.getClassForName(java.lang.String)' is inaccessible to class 'org.apache.xalan.extensions.ExtensionHandlerJavaPackage' (declaration of 'org.apache.xalan.extensions.ExtensionHandlerJavaPackage' appears in /data/app/docx4j.org.docx4j4android4-2/base.apk:classes22.dex)
at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:315)
at org.apache.xalan.extensions.ExtensionHandlerJavaPackage.callFunction(ExtensionHandlerJavaPackage.java:440)
at org.apache.xalan.extensions.ExtensionsTable.extFunction(ExtensionsTable.java:222)
at org.apache.xalan.transformer.TransformerImpl.extFunction(TransformerImpl.java:419)
at org.apache.xpath.functions.FuncExtFunction.execute(FuncExtFunction.java:208)
at org.apache.xpath.XPath.execute(XPath.java:337)
at org.apache.xalan.templates.ElemVariable.getValue(ElemVariable.java:274)
at org.apache.xalan.templates.ElemVariable.execute(ElemVariable.java:245)
at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:370)
at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:175)
at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2225)
at org.apache.xalan.templates.ElemCopy.execute(ElemCopy.java:113)
at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:370)
at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:175)
at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2225)
at org.apache.xalan.templates.ElemCopy.execute(ElemCopy.java:113)
at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:370)
at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:175)
at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2225)
at org.apache.xalan.templates.ElemCopy.execute(ElemCopy.java:113)
at org.apache.xalan.templates.ElemApplyTemplates.transformSelectedNodes(ElemApplyTemplates.java:370)
at org.apache.xalan.templates.ElemApplyTemplates.execute(ElemApplyTemplates.java:175)
at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2225)
at org.apache.xalan.templates.ElemCopy.execute(ElemCopy.java:124)
at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2225)
at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:2098)
at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1230)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:616)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1147)
at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1125)
at org.docx4j.XmlUtils.transform(XmlUtils.java:1355)
at org.docx4j.XmlUtils.transform(XmlUtils.java:1180)
at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.unmarshal(JaxbXmlPartXPathAware.java:508)
at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.unmarshal(JaxbXmlPartXPathAware.java:346)
at org.docx4j.openpackaging.parts.JaxbXmlPart.getCon
This is the code.
- Code: Select all
private void AddHeaderFooterDOCX(WordprocessingMLPackage wordMLPackage) {
try {
Log.e(TAG, "AddHeaderFooterDOCX: started");
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
createHeaderPart(wordMLPackage);
} catch (Exception e) {
Log.e(TAG, "AddHeaderFooterDOCX: Exception->" + e);
}
}
public void createHeaderPart(WordprocessingMLPackage wordprocessingMLPackage) throws Exception {
boolean newHeaderCreated = false;
HeaderPart headerPart = null;
Relationship relationship = null;
headerPart = HeaderFromDocument(wordprocessingMLPackage);
Log.e(TAG, "createHeaderPart: header headerPart->"+headerPart);
if (headerPart == null) {
Log.e(TAG, "createHeaderPart: header created");
newHeaderCreated = true;
headerPart = new HeaderPart();
headerPart.setPackage(wordprocessingMLPackage);
relationship = wordprocessingMLPackage.getMainDocumentPart().addTargetPart(headerPart);
}
if (headerPart.getJaxbElement() == null) { //getting crash over here(if header contains any shape)
Log.e(TAG, "createHeaderPart: HDR created");
headerPart.setJaxbElement(getHdr(wordprocessingMLPackage, headerPart));
} else {
Log.e(TAG, "createHeaderPart: HDR available");
Hdr hdr = headerPart.getJaxbElement();
hdr.getContent().add(getShape());
}
if (newHeaderCreated) {
createHeaderReference(wordprocessingMLPackage, relationship);
}
}
public Hdr getHdr(WordprocessingMLPackage wordprocessingMLPackage,
Part sourcePart) throws Exception {
Hdr hdr = objectFactory.createHdr();
hdr.getContent().add(getShape());
return hdr;
}
private P getShape() {
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
org.docx4j.wml.R r = factory.createR();
Text t = factory.createText();
t.setValue(K_VALUE);
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
Color green = factory.createColor();
green.setVal("green");
rpr.setColor(green);
r.setRPr(rpr);
return p;
}
private HeaderPart HeaderFromDocument(WordprocessingMLPackage wordMLPackage) {
/*
RelationshipsPart relationshipPart = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
List<Relationship> relationships = relationshipPart.getRelationships().getRelationship();
for (Relationship r : relationships) {
Log.e(TAG, "createHeaderPartNEw: Relationship types==" + r.getType() + " id=" + r.getId());
if (r.getType().equals(Namespaces.HEADER)) {
return (HeaderPart) relationshipPart.getPart(r);
}
}
return null;*/
List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel().getSections();
for (SectionWrapper sw : sectionWrappers) {
HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
if (hfp.getDefaultHeader() != null) {
return hfp.getDefaultHeader();
}
/*System.out.println("\n\nSECTION \n");
System.out.println("Headers:");
if (hfp.getFirstHeader()!=null) System.out.println("-first");
if (hfp.getDefaultHeader()!=null) System.out.println("-default");
if (hfp.getEvenHeader()!=null) System.out.println("-even");
System.out.println("\nFooters:");
if (hfp.getFirstFooter()!=null) System.out.println("-first");
if (hfp.getDefaultFooter()!=null) System.out.println("-default");
if (hfp.getEvenFooter()!=null) System.out.println("-even");*/
}
return null;
}
public static void createHeaderReference(
WordprocessingMLPackage wordprocessingMLPackage,
Relationship relationship)
throws InvalidFormatException {
List<SectionWrapper> sections = wordprocessingMLPackage.getDocumentModel().getSections();
SectPr sectPr = sections.get(sections.size() - 1).getSectPr();
// There is always a section wrapper, but it might not contain a sectPr
if (sectPr == null) {
Log.e(TAG, "createHeaderPart: SectPr created");
sectPr = objectFactory.createSectPr();
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
sections.get(sections.size() - 1).setSectPr(sectPr);
}
boolean isHeaderReferenceAvailable = false;
HeaderReference headerReference = null;
List<CTRel> relations = sectPr.getEGHdrFtrReferences();
Iterator<CTRel> relationsItr = relations.iterator();
while (relationsItr.hasNext()) {
CTRel relation = relationsItr.next();
if (relation instanceof HeaderReference) {
isHeaderReferenceAvailable = true;
if (((HeaderReference) relation).getType() == HdrFtrRef.DEFAULT)
headerReference = (HeaderReference) relation;
break;
//relationsItr.remove();
}
}
if (headerReference == null || !isHeaderReferenceAvailable) {
Log.e(TAG, "createHeaderPart: headerReference created");
headerReference = objectFactory.createHeaderReference();
headerReference.setId(relationship.getId());
headerReference.setType(HdrFtrRef.DEFAULT);
sectPr.getEGHdrFtrReferences().add(headerReference);// add header or
// footer references
wordprocessingMLPackage.getMainDocumentPart().addObject(sectPr);
}
}
These are the library used
- Code: Select all
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support:design:26.+'
testCompile 'junit:junit:4.12'
compile 'javax.xml.bind:jaxb-api:2.2.12'
compile 'javax.xml.stream:stax-api:1.0-2'
compile 'com.fasterxml:aalto-xml:1.0.0'
compile 'org.eclipse.birt.runtime.3_7_1:org.apache.xerces:2.9.0'
//compile 'org.codehaus.woodstox:stax2-api:4.0.0'
compile files('libs/awt-bastardised-17v8.jar')
compile files('libs/slf4j-android-1.6.1-RC1.jar')
compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
compile files('libs/jaxb-core-2.3.0-SNAPSHOT-ANDROID.jar')
compile files('libs/jaxb-runtime-2.3.0-SNAPSHOT-ANDROID.jar')
compile files('libs/istack-commons-runtime-3.0.4-SNAPSHOT.jar')
compile files('libs/docx4j-3.3.5-SNAPSHOT2-shaded.jar')
what is the solution/workaround for this error?
i am working with https://github.com/plutext/Docx4j4Android4 sample.
Please help!!!!!
Thanks