Appending hyperlink into Docx file using docx4j library
Download docx4j library.
filepath = valid path to .docx file
url = hyperlink to append into end of the file
public boolean appendUrlDocx() {
try {
File inputFile = new File(filepath);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputFile);
// Create hyperlink
P.Hyperlink link = createHyperlink(wordMLPackage, url);
// Add it to a paragraph
org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P paragraph = wmlFactory.createP();
paragraph.getParagraphContent().add(link);
wordMLPackage.getMainDocumentPart().addObject(paragraph);
// Now save it
wordMLPackage.save(inputFile);
return true;
} catch (InvalidFormatException ex) {
ex.printStackTrace();
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public static P.Hyperlink createHyperlink(WordprocessingMLPackage wordMLPackage, String url) {
try {
// We need to add a relationship to word/_rels/document.xml.rels
// but since its external, we don't use the
// usual wordMLPackage.getMainDocumentPart().addTargetPart
// mechanism
org.docx4j.relationships.ObjectFactory factory =
new org.docx4j.relationships.ObjectFactory();
org.docx4j.relationships.Relationship rel = factory.createRelationship();
rel.setType(Namespaces.HYPERLINK);
rel.setTarget(url);
rel.setTargetMode("External");
wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
// addRelationship sets the rel's @Id
String hpl = ""
+ ""
+ ""
+ " " + // TODO: enable this style in the document!
" "
+ ""+url+" "
+ " "
+ " ";
// return (Hyperlink)XmlUtils.unmarshalString(hpl, Context.jc, P.Hyperlink.class);
return (P.Hyperlink) XmlUtils.unmarshalString(hpl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
filepath = valid path to .docx file
url = hyperlink to append into end of the file
public boolean appendUrlDocx() {
try {
File inputFile = new File(filepath);
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputFile);
// Create hyperlink
P.Hyperlink link = createHyperlink(wordMLPackage, url);
// Add it to a paragraph
org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory();
org.docx4j.wml.P paragraph = wmlFactory.createP();
paragraph.getParagraphContent().add(link);
wordMLPackage.getMainDocumentPart().addObject(paragraph);
// Now save it
wordMLPackage.save(inputFile);
return true;
} catch (InvalidFormatException ex) {
ex.printStackTrace();
return false;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
public static P.Hyperlink createHyperlink(WordprocessingMLPackage wordMLPackage, String url) {
try {
// We need to add a relationship to word/_rels/document.xml.rels
// but since its external, we don't use the
// usual wordMLPackage.getMainDocumentPart().addTargetPart
// mechanism
org.docx4j.relationships.ObjectFactory factory =
new org.docx4j.relationships.ObjectFactory();
org.docx4j.relationships.Relationship rel = factory.createRelationship();
rel.setType(Namespaces.HYPERLINK);
rel.setTarget(url);
rel.setTargetMode("External");
wordMLPackage.getMainDocumentPart().getRelationshipsPart().addRelationship(rel);
// addRelationship sets the rel's @Id
String hpl = "
+ "
+ "
+ "
"
+ "
+ "
+ "
// return (Hyperlink)XmlUtils.unmarshalString(hpl, Context.jc, P.Hyperlink.class);
return (P.Hyperlink) XmlUtils.unmarshalString(hpl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
Comments
Post a Comment