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.

Wed Aug 08, 2018 2:03 pm

Hello,

I think to have found a bug related to the appendrtf function.
If I use AppendRTF on an existing paragraph of an existing word document, I can see a new Item and ChilderObject for the paragraph.
Instead if I use AppendRTF on a new Paragraph, I cannot see any new Item or ChilderObject, but finally the generated word contains the expected RTF part converted in word.
My problem is that I cannot manipulate the just added RTF if I cannot see the related Item.

ex.
...
int oldCount = p.Items.Count;
Paragraph p = txtrange.OwnerParagraph;
p.AppendRTF(rtf_string);
int newCount = p.Items.Count;
//here I can see the new appended item oldCount < newCount

instead if I use this code:
int oldCount = p.Items.Count;
Paragraph p = document.Sections[0].AddParagraph();
p.AppendRTF(rtf_string);
int newCount = p.Items.Count;
//here I CANNOT see the new appended items. oldCount = newCount == 0

Moreover, I cannot find a suitable way to INSERT an RTF document between two textrange. The idea was to create a new paragraph, append the RTF to it, then copy all the items in the new paragraph, to the required position between the textrange in another paragraph. The above described problem regarding the missing items, does not permit such approach.

Can you help me?
Regards

suyecufi
 
Posts: 8
Joined: Wed Aug 08, 2018 1:48 pm

Thu Aug 09, 2018 8:27 am

Dear suyecufi,

Thanks for your inquiry.
I did notice the behavior you mentioned. Actually, it is not a bug, the truth is that the AppendRTF method represents a paragraph (suppose your RTF only has one paragraph) itself, once you append it in a new paragraph, the AppendRTF method will remove the new empty paragraph automatically, therefore, the p.Items only returns 0. One suitable way to achieve your requirement is to append the RTF to a new paragraph, get the paragraph by getting the last paragraph of your document, finally, insert the childobjects of the paragraph at specific index of textrange. Below is sample code for your kind reference. If there is any question, please share your RTF string and expected output, we will then provide a corresponding solution.
Code: Select all
//Load sample document
Document doc = new Document("sample.docx");
Section section=doc.Sections[0];

//Get the index of specific text
TextSelection selection = doc.FindString("test", true, true);
TextRange txtrange = selection.GetAsOneRange();
int TextIndex = txtrange.OwnerParagraph.ChildObjects.IndexOf(txtrange);

//RTF string
String rtf_string = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 Hello, World}";

//Add a new paragraph and append RTF
Paragraph p1 = section.AddParagraph();
p1.AppendRTF(rtf_string);

//Insert the ChildObject at specific index
for (int i = 0; i < section.Body.LastParagraph.ChildObjects.Count; i++)
{
    txtrange.OwnerParagraph.ChildObjects.Insert(TextIndex, section.Body.LastParagraph.ChildObjects[i]);
    TextIndex = TextIndex + 1;
}

//Save the file
doc.SaveToFile("result.docx");

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Thu Aug 09, 2018 11:15 am

Thank you, your workaround works.
It's a bit confusing and unexpected to find a Paragraph at the end of the section, if I call the AppendRTF method of a paragraph, and obtain at the final document with the rtf code at the expected position.

Now I've a new problem. If I use AppendRTF on a Paragraph of table cell, I cannot find where the new RTF Paragraph ends.

Can you help me again?

Regards

suyecufi
 
Posts: 8
Joined: Wed Aug 08, 2018 1:48 pm

Fri Aug 10, 2018 8:20 am

Dear suyecufi,

Thanks for your feedback.
1) I previously supposed that your RTF has only one paragraph. Once append the RTF in a new paragraph, the paragraph is added to the end of the existing contents. Hence, the LastParagraph is the RTF paragraph you added. But if your RTF has more than one paragraph, getting the RTF paragraphs through the LastParagraph is not enough. Below code shows how to get complete RFT contents and insert them at specific index of textrange.
Code: Select all
Document doc = new Document("Sample.docx");
Section section = doc.Sections[0];

//Get the index of specific text
TextSelection selection = doc.FindString("test", true, true);
TextRange txtrange = selection.GetAsOneRange();
int TextIndex = txtrange.OwnerParagraph.ChildObjects.IndexOf(txtrange);

//RTF string (contains three paragraphs)
String rtf_string = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 One\par\fs28 Two\par\fs28 Three}";

//Get previous ChildObjects count
int beforeCount = section.Body.ChildObjects.Count;

//Add a new paragraph and append RTF
Paragraph p1 = section.AddParagraph();
p1.AppendRTF(rtf_string);

//Get the ChildObjects count after appending RTF
int afterCount = section.Body.ChildObjects.Count;

for (int i = beforeCount; i < afterCount; i++)
{
    for (int j = 0; j < section.Body.ChildObjects[i].ChildObjects.Count; j++)
    {
        txtrange.OwnerParagraph.ChildObjects.Insert(TextIndex, section.Body.ChildObjects[i].ChildObjects[j].Clone());
        TextIndex++;
    }
    txtrange = selection.GetAsOneRange();
    TextIndex = txtrange.OwnerParagraph.ChildObjects.IndexOf(txtrange);
}

//Save the file
doc.SaveToFile("result.docx",FileFormat.Docx2013);

2) About finding the appended RTF in table cell, the process is consistent with the above method. Sample code as shown below. If there is any misunderstanding, please provide further details.
Code: Select all
...
Table table = section.Tables[0] as Table;

//Get previous ChildObjects count
int beforeCount = table.Rows[0].Cells[0].ChildObjects.Count;
               
//Add a new paragraph in table cell and append RTF
Paragraph p1 = table.Rows[0].Cells[0].AddParagraph();
p1.AppendRTF(rtf_string);

//Get the ChildObjects count after appending RTF
int afterCount = table.Rows[0].Cells[0].ChildObjects.Count;

for (int i = beforeCount; i < afterCount; i++)
{
    for (int j = 0; j < table.Rows[0].Cells[0].ChildObjects[i].ChildObjects.Count; j++)
    {
        //...
    }
}
...

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Fri Aug 10, 2018 4:30 pm

My experience about the answer 2) is different. On appendRTF i cannot find any paragraph.
My solution was to add the paragraph to the main document, then move the content into the cell.

I'll investigate further my implementation, and I'll follow your suggestions.

Thank you!

Regards

suyecufi
 
Posts: 8
Joined: Wed Aug 08, 2018 1:48 pm

Mon Aug 13, 2018 2:57 am

Dear suyecufi,

Thanks for your feedback.
I attached the sample code to add RTF and move it into table cell for your reference. If there is any problem, please share your RTF string and expected output, then we will provide the corresponding solution for you.
Code: Select all
Document doc = new Document("Sample.docx");
Section section = doc.Sections[0];

Table table = section.Tables[0] as Table;

//RTF string (contains three paragraphs)
String rtf_string = @"{\rtf1\ansi\deff0 {\fonttbl {\f0 hakuyoxingshu7000;}}\f0\fs28 One\par\fs28 Two\par\fs28 Three}";

//Get previous ChildObjects count
int beforeCount = section.Body.ChildObjects.Count;

//Add a new paragraph and append RTF
Paragraph p1 = section.AddParagraph();
p1.AppendRTF(rtf_string);

//Get the ChildObjects count after appending RTF
int afterCount = section.Body.ChildObjects.Count;

Paragraph para = table.Rows[0].Cells[0].Paragraphs[0];

for (int i = beforeCount; i < afterCount; i++)
{
    for (int j = 0; j < section.Body.ChildObjects[i].ChildObjects.Count; j++)
    {
        //Move the RTF contents into table.Rows[0].Cells[0]
        para.ChildObjects.Add(section.Body.ChildObjects[i].ChildObjects[j]);
    }
}

//Save the file
doc.SaveToFile("result.docx", FileFormat.Docx2013);

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Wed Aug 15, 2018 6:23 am

Hello,

Greetings from E-iceblue.
How is your issue going?
Thanks in advance for your valuable feedback and time.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Mon Aug 20, 2018 9:22 am

Thank you for your help.
I ended up using my solution to avoid to mess up the already working workaround.

Sincerely thank you for your suggestions. You were decisive to get thinks working.

Regards
Last edited by suyecufi on Tue Aug 21, 2018 7:09 am, edited 1 time in total.

suyecufi
 
Posts: 8
Joined: Wed Aug 08, 2018 1:48 pm

Tue Aug 21, 2018 6:58 am

Hi,

Thanks for your feedback.
If you have other questions, just feel free to contact us.
Have a nice day!

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.Doc