While going on with my work, i' Ive found another issue.
When im trying to add a hyperlink to the document it always kills my toc totally.
I'm trying to do something like that:
- Code: Select all
...
if (document.getMainDocumentPart().getRelationshipsPart() != null) {
id = report.getMainDocumentPart().getRelationshipsPart().size() + 1;
} else {
id += 1;
}
P.Hyperlink phyperlink = factory.createPHyperlink();
JAXBElement<P.Hyperlink> phyperlinkWrapped = factory.createPHyperlink(phyperlink);
phyperlink.setId("rId" + id);
id++;
phyperlink.setHistory(true);
// Create object for r
R r = factory.createR();
phyperlink.getContent().add(r);
// Create object for rPr
RPr rprh = factory.createRPr();
r.setRPr(rprh);
// Create object for rStyle
RStyle rstyle = factory.createRStyle();
rprh.setRStyle(rstyle);
rstyle.setVal("Hyperlink");
// Create object for t (wrapped in JAXBElement)
Text text = factory.createText();
JAXBElement<org.docx4j.wml.Text> textWrapped = factory.createRT(text);
r.getContent().add(textWrapped);
text.setValue("www.google.com");
paragraph.getContent().add(phyperlinkWrapped);
org.docx4j.relationships.ObjectFactory relFactory = new org.docx4j.relationships.ObjectFactory();
org.docx4j.relationships.Relationship rel = relFactory.createRelationship();
rel.setId("rId" + id);
rel.setType(Namespaces.HYPERLINK);
rel.setTarget("www.google.com");
rel.setTargetMode("External");
document.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
...
also I've tried just doing:
- Code: Select all
...
String openXML = "<w:hyperlink r:id=\"rId4\" w:history=\"true\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">"
+ "<w:r>"
+ "<w:rPr>"
+ "<w:rStyle w:val=\"Hyperlink\"/>"
+ "</w:rPr>"
+ "<w:t>www.google.com</w:t>"
+ "</w:r>"
+ "</w:hyperlink>";
P.Hyperlink phyperlink = (P.Hyperlink) XmlUtils.unmarshalString(openXML);
paragraph.getContent().add(phyperlink);
...
With both variants I get errors/exceptions when updating my toc.
Origin is: tocGenerator.updateToc(false, true);
Thrown error is:
org.docx4j.toc.TocException: Exception exporting package
at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:812)
at org.docx4j.toc.TocGenerator.getPageNumbersMap(TocGenerator.java:655)
at org.docx4j.toc.TocGenerator.populateToc(TocGenerator.java:402)
at org.docx4j.toc.TocGenerator.generateToc(TocGenerator.java:306)
at org.docx4j.toc.TocGenerator.updateToc(TocGenerator.java:536)
at org.docx4j.toc.TocGenerator.updateToc(TocGenerator.java:484)
...
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:106)
at org.docx4j.Docx4J.toFO(Docx4J.java:712)
at org.docx4j.toc.TocGenerator.getPageNumbersMapViaFOP(TocGenerator.java:784)
... 19 more
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception writing Document to OutputStream: org.apache.fop.fo.ValidationException: Missing attribute on fo:basic-link: Either external-destination or internal-destination must be specified. (Siehe Position 7:236)
at org.docx4j.utils.XmlSerializerUtil.serialize(XmlSerializerUtil.java:56)
at org.docx4j.utils.XmlSerializerUtil.serialize(XmlSerializerUtil.java:18)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.calcResults(FORendererApacheFOP.java:272)
at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:152)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:81)
... 21 more
Caused by: javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: Missing attribute on fo:basic-link: Either external-destination or internal-destination must be specified. (Siehe Position 7:236)
at org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:535)
at org.docx4j.utils.XmlSerializerUtil.serialize(XmlSerializerUtil.java:51)
... 27 more
Caused by: org.apache.fop.fo.ValidationException: Missing attribute on fo:basic-link: Either external-destination or internal-destination must be specified. (Siehe Position 7:236)
at org.apache.fop.events.ValidationExceptionFactory.createException(ValidationExceptionFactory.java:38)
at org.apache.fop.events.EventExceptionManager.throwException(EventExceptionManager.java:58)
at org.apache.fop.events.DefaultEventBroadcaster$1.invoke(DefaultEventBroadcaster.java:173)
at com.sun.proxy.$Proxy234.missingLinkDestination(Unknown Source)
at org.apache.fop.fo.flow.BasicLink.bind(BasicLink.java:95)
at org.apache.fop.fo.FObj.processNode(FObj.java:131)
at org.apache.fop.fo.FOTreeBuilder$MainFOHandler.startElement(FOTreeBuilder.java:293)
at org.apache.fop.fo.FOTreeBuilder.startElement(FOTreeBuilder.java:179)
at org.docx4j.convert.out.fo.PlaceholderReplacementHandler.startElement(PlaceholderReplacementHandler.java:106)
at org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1119)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:510)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2710)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:534)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:888)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:824)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1216)
at org.docx4j.org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:518)
Am i missing something or what do I do wrong? Can somebody please help me with that problem.