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.

Sat Dec 22, 2018 2:47 am

Hello, dear friends.

I Have a Word templates with tags (like <table>) this tags I identify it like normal text. So my doubt is: I have to replace this tags with RTF Formated Text from a DataBase. There's some way to do that?

Regards

andreievich
 
Posts: 1
Joined: Tue Mar 21, 2017 3:34 pm

Mon Dec 24, 2018 7:52 am

Hello Arthur,

Thanks for your inquiry.
Please refer to the following sample code to replace normal text with RTF text, if there is any question, please provide your word template document as well as the RTF text to help further investigate it.
Code: Select all
 Document doc = new Document();
 doc.LoadFromFile(@"test.docx");
 TextSelection[] selections = doc.FindAllString("<tag>", false, true);
 foreach (TextSelection selection in selections)
 {
     TextRange range = selection.GetAsOneRange();
     int index = range.OwnerParagraph.ChildObjects.IndexOf(range);
     //create a temporary section
     Section tempSection = doc.AddSection();
     String rtfString = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";
     //add a paragraph to append rtf
     Paragraph par = tempSection.AddParagraph();
     par.AppendRTF(rtfString);
     foreach (Paragraph p in tempSection.Paragraphs)
     {
         foreach (DocumentObject obj in p.ChildObjects)
         {
             range.OwnerParagraph.ChildObjects.Insert(index, obj.Clone());
         }
     }
     range.OwnerParagraph.ChildObjects.Remove(range);
     //remove the temp section
     doc.Sections.Remove(tempSection);
 }
 doc.SaveToFile("result.docx", FileFormat.Docx);

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Wed Jan 02, 2019 3:04 am

Hello Arthur,

Greetings from E-iceblue.
Did my code help you? Thanks in advance for your valuable feedback and time.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Aug 23, 2021 8:53 pm

Hey,

I think it's better to continue here.

I found out that once the style of the text changes, they get saved in the wrong direction

I fixed this with following line

foreach (Paragraph p in tempSectionTrainingsprogramma.Paragraphs)
{

foreach (DocumentObject obj in p.ChildObjects)
{
rangeTrainingsprogramma.OwnerParagraph.ChildObjects.Insert(i1, obj.Clone());
i1++;
}
}


the only problem now is that every next line is gone, with other words, text gets pasted behind each other.

for example: polsslagen fluctueren tussen 145/150 tot 165/170HFDinsdag :De tempolopen tussen 30’ … 45’ … 1u tot 1u15Vooreerst 5’ tot 10’ inlopen rond 140/145HFBewust tempowisselingen

Best regards

Nicolas

Temptica
 
Posts: 1
Joined: Mon Aug 23, 2021 8:47 pm

Tue Aug 24, 2021 11:04 am

Hello Nicolas,

Thanks for your inquiry.
To help us investigate your issue more quickly and accurately, please provide your input file and your desired output for our reference. You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc

cron