Hi,
I am creating documents being saved as PDF through Aspose.Words, which is incorporating bookmarks.
The bookmarks are replaced with text, which can be HTML. I don’t know which bookmarks may be html beforehand.
Once I’ve updated my bookmarks, I want to loop back through them and convert the HTML tags (i.e. underline etc.) so that it appears as intended on the PDF. How can I do this with Aspose.Words?
Thanks,
Jon
Hi Jon,
Thanks for your inquiry. Please use the following code snippet to achieve your requirements. Following code example extract the bookmark contents (the html contents) and insert the HTML into document by using DocumentBuilder.InsertHtml method.
// open the document
Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach(Bookmark bm in doc.Range.Bookmarks)
{
// Get the text enclosed in the bookmark
string html = bm.Text;
bm.Text = "";
// Move cursor to bookmark and isnert html
builder.MoveToBookmark(bm.Name);
builder.InsertHtml(html);
// InsertHtmlWithBuilderFormatting(builder, html);
}
doc.Save(MyDir + "Out.docx");
Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/document-builder-overview/
https://docs.aspose.com/words/net/working-with-bookmarks/
Moreover, the content inserted by DocumentBuilder.InsertHtml** method does not inherit formatting specified in DocumentBuilder options. Whole formatting is taken from HTML snippet. If you insert HTML with no formatting specified, then default formatting is used for inserted content.
If this is the case, you may use InsertHtmlWithBuilderFormatting instead of the InsertHtml method as shown in above code (the commented line). I have attached the code related to InsertHtmlWithBuilderFormatting with this post. Hope this helps you.
Excellent, thanks!
Hi Jon,
Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.
**Hi Tahir, do you by chance have the code in Java, or even, are there plans to allow insertHTML() to inherit from a style other than Normal?
Unfortunately we are inserting HTML for which there isn’t support for formatting.
Thank you.**
Hi Chris,
Thanks for your inquiry. I have attached the Java equivalent of InsertHtmlWithBuilderFormatting here with this post. Please try it and let us know if you have any more queries.
Document doc = new Document("C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
DocumentBuilderHelper helper = new DocumentBuilderHelper(builder);
helper.insertHtmlWithBuilderFormatting("formatted html string");
doc.save("C:\Temp\outJava.docx");
I hope, this helps.