Hi, I need to insert hyperlink for the StructuredDocumentTag. Currently I’m able to achieve this in vba under developer mode of the document by using
objcc.Range.Hyperlinks.Add objcc.Range, “https://stackoverflow.com/”
But, this is ok, if user adds a control to the document. But when you generate a document with content control, I want it to have hyperlink.
Do you know whether this is possible ?
@manojp1988,
To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:
- Your input Word document
- Your expected Word document showing the desired output. Please create this document by using MS Word.
As soon as you get these pieces of information ready, we will start further investigation into your above issue and provide you more information. Thanks for your cooperation.
I cannot send the document for confidential reasons. I have attached a screenshot of the document here. If you see the screenshot, there are 2 content controls. I want to attach hyperlink for those content control.
Capture.PNG (8.9 KB)
@manojp1988,
Please see these input output Word documents and try running the following code:
Document doc = new Document("D:\\temp\\input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true)[0];
Paragraph para = (Paragraph)sdt.FirstChild;
para.Runs[0].Font.StyleIdentifier = StyleIdentifier.Hyperlink;
builder.MoveTo(para);
FieldHyperlink link = (FieldHyperlink)builder.InsertHyperlink("DT", "https://www.aspose.com/", false);
((CompositeNode)link.Separator.ParentParagraph).InsertAfter(para.FirstChild, link.Separator.NextSibling);
link.Separator.NextSibling.Remove();
doc.Save("D:\\temp\\18.7.docx");
Thank you. I’m using java version of Aspose v16. I have tried the code, it partially works. I have attached both the input and output doc.
- The text for the hyperlink is not printing correctly. (eg: gleGoog)…
- I need to create StructuredDocumentTag programmatically and insert hyperlink. I could not make this work. I am not getting Para inside SDT.
First version 1:
Document doc = new Document(“D:\sample.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
Paragraph para = (Paragraph)sdt.getFirstChild();
Run run = para.getRuns().get(0);
run.getFont().setStyleIdentifier(StyleIdentifier.HYPERLINK);
builder.moveTo(para);
Field link = builder.insertHyperlink("DT", "https://www.google.com/", false);
((CompositeNode)link.getSeparator().getParentParagraph()).insertAfter(para.getFirstChild(), link.getSeparator().getNextSibling());
link.getSeparator().getNextSibling().remove();
doc.save("D:\\new_sample.docx");<a class="attachment" href="/uploads/default/17071">Sample.zip</a> (25.3 KB)
Second Version:
I tried below code to insert new SDT and process, but couldn’t succeed
final StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.INLINE);
builder.insertNode(sdt1);
Paragraph para = builder.insertParagraph();
// Paragraph para = (Paragraph)sdt1.getCi();
para.getRuns().get(0).getFont().setStyleIdentifier(StyleIdentifier.HYPERLINK);
builder.moveTo(para);
Field link = builder.insertHyperlink("DT", "https://www.aspose.com/", false);
((CompositeNode)link.getSeparator().getParentParagraph()).insertAfter(para.getFirstChild(), link.getSeparator().getNextSibling());
link.getSeparator().getNextSibling().remove();
doc.save("D:\\new_sample.docx");
@manojp1988,
To create StructuredDocumentTag programmatically and insert hyperlink inside, please try using the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
sdt.removeAllChildren();
sdt.isShowingPlaceholderText(false);
Paragraph para = new Paragraph(doc);
sdt.appendChild(para);
doc.getFirstSection().getBody().appendChild(sdt);
builder.moveTo(para);
builder.getFont().setStyleIdentifier(StyleIdentifier.HYPERLINK);
builder.insertHyperlink("aspose.com", "https://www.aspose.com/", false);
doc.save("D:\\temp\\awjava-18.7.docx");
Secondly, we do not see any input/output documents (attachments) in your previous post. Please ZIP and upload your input/output documents here for any further testing. We will then investigate the issue on our end and provide you more information.
Sample.zip (25.3 KB)
Missed attachment.
I need little more help. I need to use builder.insertNode() because, when I convert xml to doc, I will traverse through and insert SDT.
In the sample given above, if I use builder.insertNode(sdt) instead of doc.getFirstSection().getBody().appendChild(sdt); I am getting “Cannot insert a node of this type at this location.” exception. How to overcome this issue?
Secondly, if I change the MarkupLevel to INLINE, I’m getting exception even in the sample document. So only of BLOCK level this works?
@manojp1988,
Please try using the following code:
Document doc = new Document("D:\\temp\\Sample\\Sample.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = (StructuredDocumentTag) doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG,
true).get(0);
Paragraph para = (Paragraph) sdt.getFirstChild();
builder.moveTo(para);
FieldHyperlink link = (FieldHyperlink) builder.insertHyperlink("DT", "https://www.aspose.com/", false);
for (int i = 0; i < para.getRuns().getCount(); i++) {
Run run = para.getRuns().get(0);
run.getFont().setStyleIdentifier(StyleIdentifier.HYPERLINK);
((CompositeNode) link.getEnd().getParentParagraph()).insertBefore(para.getFirstChild(), link.getEnd());
}
link.getSeparator().getNextSibling().remove();
doc.save("D:\\temp\\Sample\\awjava-18.7.docx");
Secondly, a StructuredDocumentTag can occur in a document in the following places:
- Block-level - Among paragraphs and tables, as a child of a Body, HeaderFooter, Comment, Footnote or a Shape node.
- Row-level - Among rows in a table, as a child of a Table node.
- Cell-level - Among cells in a table row, as a child of a Row node.
- Inline-level - Among inline content inside, as a child of a Paragraph.
- Nested inside another StructuredDocumentTag.
Please refer to the Node hierarchy of Aspose.Words’ DOM.
Aspose.Words Document Object Model