Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Aug 24, 2021 9:05 pm

Hi guys,

I have paragraphs like this:

----------------------------------------------------------
Blablbla [[Html]] blablabla
----------------------------------------------------------

Now I would like to replace [[Html]] by real HTML code.

I know you are having paragraph.AppendHtml(), but in this case, I do not want to append html to a paragraph.
Instead I would like to replace some text with Html.

Are you guys supporting this scenario at all?

There is a help topic on your site, and it provides some code, but this code has nothing to do with HTML as far as I can tell.
https://www.e-iceblue.com/Tutorials/Spi ... -HTML.html

Thanks for your help, guys!
Ingmar

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Wed Aug 25, 2021 6:48 am

Hi,

Thank you for your inquiry.
Our Spire.Doc doesn't provide a direct way to replace text with html in Replace method at present. We will consider adding it as a new feature into our future upgrade, once it is achieved, I will inform you. In addition, if you write your HTML code into an HTML file, you can use the following code to directly replace it.
Code: Select all
            Document document = new Document();
            document.LoadFromFile(inputFile);
            Document htmldoc = new Document();
            htmldoc.LoadFromFile(htmlpath, FileFormat.Html,XHTMLValidationType.None);
            document.Replace("[[Html]]", htmldoc, false, true);
            document.SaveToFile("output.docx");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Wed Aug 25, 2021 8:36 am

Thank you, Annika.

Yes, please keep me in the loop once such a feature should be available.

The workaround you suggested (document.Replace("[[Html]]", htmldoc, false, true);) doesn't really fit my needs unfortunately.
I have several paragraphs which all contain an [[Html]]-Placeholder. However, all these placeholders need to be replaced with paragraph-specific html content.
In other words: I must not replace all [[Html]] placeholders with the same html content - and this is what document.Replace() would do.

So, I will have to think about my own workaround. And also hope, you guys will add such a feature in the near future.

Thanks again!

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Wed Aug 25, 2021 11:32 am

Hello,

Thank you for your feedback.
I have submitted the requirement to our Dev team to implement,if it is available, I will inform you.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Tue Dec 28, 2021 8:34 am

Hello Annika,

any news on this? I am still looking for a solution for replacing Text with HTML.

I found https://www.e-iceblue.com/Tutorials/Spi ... -HTML.html in your help section. But I am not sure if this is what I need, since the sample is VBA (and I really have a hard time reading VBA code).

Thanks for your help
Ingmar

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Tue Dec 28, 2021 10:18 am

Hello,

Thank you for your follow-up.
Please refer to the attachment for the code mentioned in your link. In addition, it has not been implemented to directly replace text with html in the replace method. In any case, I have asked our development team to deal with it as soon as possible. Once it is achieved, I will inform you in time.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Wed Dec 29, 2021 9:52 am

Hello Annika,

thank you very much for your support and the sample code you provided.

However, you guys seem to have a different scenario than me. Attached you will find a little console project that I created as a demo for you.
Would it be too much to ask if you guys could have a short look and let me know what I need to change to make it work?

Within the zip you will find my template document (Input.docx) and the result document that I would expect after replacing all the placeholders (Result.docx).

Thank you so much!
Ingmar

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Wed Dec 29, 2021 10:04 am

Hi,

Okay. I will give you feedback tomorrow after I investigate.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Wed Dec 29, 2021 1:35 pm

Aweseome. Thank you, Annika. Can't wait to see how you fix my code :)

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Thu Dec 30, 2021 11:12 am

Hello,

Thanks for your patience.
For the template file and the result file you provide, please refer to the code in the attachment to achieve your needs. In addition, our free version of Spire.Doc does not support .NET6.0, please use our latest commercial version (Spire.Doc Pack(hot fix) Version:9.12.12) to run .NET6.0 projects.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Mon Jan 03, 2022 7:38 am

Good morning Annika,

Happy New Year :) and thank you again for your support last week. I am really grateful.

However, I am really sorry, but we probably had a little misunderstanding. What you did, was taking the "data part" of my sample application and you integrated it in your solution (which is based on [TextSelection] and your custom [TextRangeLocation : IComparable]). Actually I needed this the other way around:

If you could download my attached sample one more time and search for "Annika", you will find the piece of code where I am stuck. In fact, all I have at this point is a [Paragraph]. And what I do not know is how to replace the text of a [Paragraph] with HTML. Or thinking in terms of your solution: how can I transform a [Paragraph] to a [TextSelection] (and then use your [TextRangeLocation : IComparable] with this [TextSelection])?

I would be more than happy if you could point me in the right direction inside *my* code. The sample is "ready to run". Just press F5 and it should generate a Word document.

And don't you worry: I am still using the free version of SPIRE.DOC in my sample because this is a public forum. In our production applications of course we are using the commercial version.

I hope you can help me one more time. Thank you again!!

Ingmar

spire@softwarea.de
 
Posts: 7
Joined: Thu May 03, 2018 8:56 pm

Tue Jan 04, 2022 10:44 am

Hello,

Thank you for your feedback.
Please modify the code in the "Paragraph ReplaceWithHtml" method in "Utilities.cs" to the following code.
Code: Select all
...
if (paragraph.Text.Contains(placeholder)) {
    paragraph.Text = "";
    paragraph.AppendHTML(placeholder);
}       
return paragraph;

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Tue Jan 04, 2022 2:31 pm

Hello Annika,

hehe, yes, your solution solved my problem with the word template that I provided you. But paragraph.Text = ""; paragraph.AppendHTML(placeholder); was too easy after all, because it replaces the whole paragraph that contained a placeholder. But I wanted to replace only my placeholder - not the whole paragraph.

Anyways: With the help of your solution in one of your previous mails (based on [TextRangeLocation : IComparable]) I finally found a way to achieve what I need.
For everybody interested, please find my final version in the attachment. It contains my code + a demo Word template. Just hit F5 and it will return a Word document that has all relevant placeholders replaced with HTML.

@Annika, thank you so much. I still don't fully understand all the details of your code, but it is working for me :)

Best
Ingmar

softwarea
 
Posts: 8
Joined: Sun Apr 17, 2016 1:59 pm

Wed Jan 05, 2022 1:16 am

Hello,

Glad to hear that your issue has been solved.
If you encounter other issues related to our products in the future, please feel free to contact us.

Have a nice day!

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1651
Joined: Wed Apr 07, 2021 2:50 am

Thu Jun 02, 2022 11:28 am

I know that "softwarea" already has it fixed, but I also needed to replace text with html. As long as there is no direct option with the replace and reviewing the indicated example, I have found an option that is also very valid. Instead of using a disk file, converting the final string to inputstream and using loadFromStream. This solution works for me and in the end it does the same as a direct replace would do. I'll post it if anyone is interested.

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Return to Spire.Doc