I'm using Aspose (Java) with LINQ reporting syntax to build a report dynamically. This piece of documentation explains how to insert hyperlinks into a document. Which, most generally, looks like this
<<link [uri_or_bookmark_expression] [display_text_expression]>>
The last paragraph states "While building a report..." which I am interpreting as executing buildReport(). We need to execute buildReport anyway for obvious reasons. But the output when we use buildReport() is invalid and therefore not properly converted to a hyperlink in the Word doc.
_engine = new ReportingEngine();
_engine.getKnownTypes().add(Helper.class);
_engine.setOptions(ReportBuildOptions.ALLOW_MISSING_MEMBERS | ReportBuildOptions.REMOVE_EMPTY_PARAGRAPHS);
Document document = new Document;
DocumentBuilder builder = new DocumentBuilder(document);
builder.writer("<<link [LinkLabel]>>");
//document.getText(): <<link [LinkLabel]>>
_engine.buildReport(document, dataSource, "NewDoc");
//document.getText() now: HYPERLINK \l "LinkLabel"
Before build:
<<link LinkLabel>>
After build:
HYPERLINK \l "LinkLabel"
So it appears an attempt is made to convert the LINQ reporting syntax to Word-friendly code. But the Word-friendly results are invalid.
When I create a hyperlink in Word manually it generates this code (when I reveal it using Alt+F9): { HYPERLINK \l "LinkLabel" }
. So it seems like the squiggly brackets ({ and }
) were dropped in buildReport. Why did it drop them? Or are one/some of my assumptions off?
LinkLabel
in your expression should be surrounded with square brackets. So the expression before building the report should look like this:
<<link [LinkLabel]>>
where LinkLabel
is the name of variable that contains the url or bookmark name. For example see the following code:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
builder.write("<<link [LinkLabel]>>");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(document, "https://www.aspose.com/", "LinkLabel");
document.save("C:\\Temp\\out.docx");
In this case url is also used as displayed text. If it is required to use different value as displayed text, the second parameter should be used:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
// Define variables with url and displayed text.
// In real case the data will come from your data source.
builder.write("<<var [url=\"https://www.aspose.com/\"]>>");
builder.write("<<var [displayed_text=\"Link To Aspose Web Site\"]>>");
builder.write("<<link [url] [displayed_text]>>");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(document, new Object());
document.save("C:\\Temp\\out.docx");
EIDT: The curly brackets you have mentioned are not actually a simple curly brackets, they are special nodes - FieldStart, FieldSeparator and FieldEnd. See the following article to learn more about representing field in MS Word and Aspose.Words: https://docs.aspose.com/words/net/fields-overview/
After producing the document using the following code:
DocumentBuilder builder = new DocumentBuilder();
builder.write("<<link [LinkLabel]>>");
ReportingEngine engine = new ReportingEngine();
engine.buildReport(builder.getDocument(), "label", "LinkLabel");
builder.getDocument().save("C:\\Temp\\out.docx");
The field is constructed properly: Internally such field is represented like this:
<w:hyperlink w:anchor="label" w:history="1">
<w:r>
<w:rPr>
<w:rStyle w:val="Hyperlink" />
</w:rPr>
<w:t>label</w:t>
</w:r>
</w:hyperlink>
But the method Document.getText()
does not return "curly brackets" for special field nodes, that is why you do not see them in the string output.