Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Hyperlink in docx4j(I'm trying to make a hyperlink inside a Paragraph, but I fail)
icon5.gif  Hyperlink in docx4j [message #1738510] Wed, 20 July 2016 13:16 Go to next message
Steven Engelen is currently offline Steven EngelenFriend
Messages: 4
Registered: April 2015
Junior Member
I'm trying to get this as a result as a footer in my word document:

In toepassing van de wet tot bescherming van de persoonlijke levenssfeer in verband met de verwerking van persoonsgegevens, informeert de hogervermelde licentiehouder u, als verantwoordelijke verwerker van deze gegevens, dat de door u meegedeelde persoonlijke gegevens, uitsluitend verwerkt worden om aan uw verzoek te voldoen tot het opstellen van een fiscale raming. Verder wordt verwezen naar de Privacybepaling van de licentiehouder.

with the "privacybepaling" linking to a website.
The problems are:
* the link isn't to www.google.com but to c:\users\steven\downloads\test.docx#www.google.com

*I want the link in another style than the rest of the Paragraph, but it's not.

my code to add the part:

String disclaimer = "In toepassing van de wet tot bescherming van de persoonlijke levenssfeer in verband met de verwerking van persoonsgegevens, informeert de hogervermelde licentiehouder u, als verantwoordelijke verwerker van deze gegevens, dat de door u meegedeelde persoonlijke gegevens, uitsluitend verwerkt worden om aan uw verzoek te voldoen tot het opstellen van een fiscale raming. Verder wordt verwezen naar de "
String buildDate = formatDate(service.getBuildDate());
disclaimer = disclaimer.replace("$BUILD_DATE$", buildDate);
P paragraph = createParagraph(disclaimer, DocxStyle.DISCLAIMER);

if (addHyperlink) {
String url = "www.google.com";
Hyperlink hyperlink = createHyperlink("Privacybepaling", url);
paragraph.getContent().add(hyperlink);

String disclaimerPart2 = "van de licentiehouder.";
P paragraph2 = createParagraph(disclaimerPart2, DocxStyle.DISCLAIMER);
paragraph.getContent().add(paragraph2);
}

lst.add(paragraph);
addObjects(lst, true);


protected Hyperlink createHyperlink(String displayText, String url) {
String hpl = "<w:hyperlink w:anchor=\"" + url
+ "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " +
"w:history=\"1\" >" +
"<w:r>" +
"<w:rPr>" +
"<w:rStyle w:val=\"Intensieve verwijzing\" />" + // TODO: enable this style in the document!
"</w:rPr>" +
"<w:t xml:space=\"preserve\">" + " " + displayText + " " + "</w:t>" +
"</w:r>" +
"</w:hyperlink>";

try {
return (Hyperlink) XmlUtils.unmarshalString(hpl);
}
catch (JAXBException e) {
// Shouldn't happen
e.printStackTrace();
return null;
}
}

protected void addObjects(List<Object> lst, boolean keepNext) {
if (lst != null && !lst.isEmpty()) {
setKeepNext(lst);
for (int i = 0; i < lst.size(); i++) {
if (lst.get(i) != null) {
getMainDocumentPart().getContent().add(lst.get(i));
}
}
}
}

protected P createParagraph(String text, DocxStyle style) {
ObjectFactory factory = Context.getWmlObjectFactory();
P par = getMainDocumentPart().createStyledParagraphOfText(style.getStyleName(), "");

if (getCompanySettingsID() != null && !getCompanySettingsID().equals(-1L)) {
try {
if (!SERVICES.getService(ICompanySettingsAccessService.class).getCompanySettingsByID(getCompanySettingsID()).isJustifyDisclaimerText()) {
PPr paragraphProperties = par.getPPr();
Jc justification = factory.createJc();
justification.setVal(JcEnumeration.LEFT);
paragraphProperties.setJc(justification);
}
}
catch (ProcessingException e) {
LOG.error("Error getting companySettings " + e.getMessage());
}
}
List<R> runs = getAllElementFromObject(par, R.class);
if (runs != null && !runs.isEmpty()) {
R run = runs.get(0);
Text t = null;
for (Object o : run.getContent()) {
if (o.getClass().equals(Text.class)) {
t = (Text) o;
break;
}
}

String[] lines = text.split("\n");
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
if (t != null) {
t.setValue(line);
t = null;
}
else {
Text txt = factory.createText();
txt.setValue(line);
run.getContent().add(txt);
}

if (i < lines.length - 1) {
Br br = factory.createBr();
br.setType(STBrType.TEXT_WRAPPING);
run.getContent().add(br);
}
}
}

return par;
}

[Updated on: Wed, 20 July 2016 13:17]

Report message to a moderator

Re: Hyperlink in docx4j [message #1738583 is a reply to message #1738510] Thu, 21 July 2016 06:54 Go to previous message
Beat Schwarzentrub is currently offline Beat SchwarzentrubFriend
Messages: 88
Registered: November 2010
Member
This seems to be a completely Docx4j related problem. Have you tried adding "http://" as a prefix to the URL? For an answer from the experts, you should probably ask your question in the Docx4j forum. Or have look at this test code from the official Docx4j repository: http://www.docx4java.org/svn/docx4j/trunk/docx4j/src/main/java/org/docx4j/samples/HyperlinkTest.java

Regards,
Beat
Previous Topic:Old Scout Download
Next Topic:AbstractPageWithNodes with default page
Goto Forum:
  


Current Time: Wed Sep 20 07:46:44 GMT 2017

Powered by FUDForum. Page generated in 0.03988 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software