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 Jul 03, 2019 2:03 pm

hi
i use the function appendRTF for this type of rtf data :
Code: Select all
exemple rtf :
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e  en l'\'e9tat.\par\b0\i0\f1\par\par}


my problem is in word directly in rtf mode see file wordoriginal.png
wordoriginal.png

with spire.doc function appendRTF there is an error on char font see the result in file functionappendrtf.png
functionappendrtf.png

i use the code
Code: Select all
  Dim sectmp As Section = DocReceveur.AddSection()
                            sectmp.AddParagraph().AppendRTF(Codertfexemple)

                            Dim replacementFirstItem As ParagraphBase = sectmp.Paragraphs(0).Items.FirstItem
                            Dim replacementLastItem As ParagraphBase = sectmp.Paragraphs(sectmp.Paragraphs.Count - 1).Items.LastItem
                            Dim sel As TextBodySelection = New TextBodySelection(replacementFirstItem, replacementLastItem)
                            Dim part As TextBodyPart = New TextBodyPart(sel)
                            bookmarkNavigator.MoveToBookmark(ligneData(1).Trim(), True, True)
                            bookmarkNavigator.ReplaceBookmarkContent(part, True)   '[b]i try TRUE and FALSE for the second param[/b]

                           ....



i dump the result paragraph of appendRTF : sectmp.Paragraphs i don't find the arial font specified in rtf code...
i need the same word result with the appendRTF spire.doc .
ps i tested also with wordpad and it the same result than word.

thank's for your help
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Thu Jul 04, 2019 8:22 am

Hi,

Thanks for your inquiry.
I have tested your case with the latest Spire.Doc Pack(hot fix) Version:7.6.16, but did't face the issue you mentioned, the font of text in generated Word is "Arial". Here I attached my generated document for your reference. I would suggest that you download the latest version to try again. If the issue still occurs, please provide the following information for further investigation.
1. Your testing environment, such as your OS information (e.g. Windows 7, 64bit) as well as Region setting (e.g. France, French).
2. Did you load an input Word document? If any, please share it with us.

Sincerely,
Nina
E-iceblue support team
User avatar

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

Thu Jul 04, 2019 10:57 am

hi thank for your answer.
following your answer i have researched the problem more precisely, and i found :D
it is the copy code of the text rtf in the bookmark which is in question, it s don't copy all format infromations
I created a project to show exactly the problem :
execute it on the model or see directly the result in attached rar archive (tmp.rar contain model and result).
Code: Select all
class Program
    {
        static void Main(string[] args)
        {
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";

            Document DocReceveur = new Document(@"C:\tmp\_Model5719113.docx");
                       Bookmark BookmrakCurrUnique2 = DocReceveur.Bookmarks.FindByName("test");
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);

            Section sectmp = DocReceveur.AddSection();
            sectmp.AddParagraph().AppendRTF(rtf);
         

            ParagraphBase replacementFirstItem = sectmp.Paragraphs[0].Items.FirstItem as ParagraphBase;
            ParagraphBase replacementLastItem = sectmp.Paragraphs[0].Items.LastItem as ParagraphBase;

            TextBodySelection sel = new TextBodySelection(replacementFirstItem, replacementLastItem);
            TextBodyPart part = new TextBodyPart(sel);
                   
            bookmarkNavigator.MoveToBookmark("test");
            bookmarkNavigator.ReplaceBookmarkContent(part, false);  // i  also test ed bookmarkNavigator.ReplaceBookmarkContent(part, true);

            DocReceveur.Sections.Remove(sectmp);
            DocReceveur.Bookmarks.Remove(BookmrakCurrUnique2);


            //other method
            //Paragraph p = sectmp.AddParagraph();
            Paragraph p =  DocReceveur.Sections[0].AddParagraph();
            p.AppendRTF(rtf);


            DocReceveur.SaveToFile(@"C:\tmp\result.docx");


can you give me my error or a solution to copy the result of appendRTF in bookmark with sucess ?
thank's for your help
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Fri Jul 05, 2019 9:17 am

Hi,

Thanks for your detailed information.
I have reproduced the issue and logged it into our bug tracking system. If it is fixed or there is any update for you, I will let you know. Apologize for the inconvenience caused.

Sincerely,
Nina
E-iceblue support team
Last edited by Nina.Tang on Mon Jul 08, 2019 1:32 am, edited 1 time in total.
User avatar

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

Fri Jul 05, 2019 12:34 pm

hi,
there is no other way to copy rtf code on bookmark :shock:
"If it is fixed"
my year work is blocked by this problem can you please give an other solution to write rtf to bookmark ? (even if it is temporary while waiting )
I am very desappointed and please i need a solution :(
thank's
FH
Last edited by labaffagaston on Mon Jul 08, 2019 7:27 am, edited 1 time in total.

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Mon Jul 08, 2019 6:07 am

Hi,

Thanks for your feedback and sorry for late reply as weekend.
Our Dev team is investigating your issue now. I will keep you informed regarding any available update. As a temporary workaround, you could get the RTF paragraph and insert it at the place where the bookmark locates. Sample code is given as follows.
Code: Select all
string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
Document DocReceveur = new Document(@"_Model5719113.docx");

Section sectmp = DocReceveur.AddSection();
sectmp.AddParagraph().AppendRTF(rtf);

Bookmark BookmrakCurrUnique2 = DocReceveur.Bookmarks.FindByName("test");
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
bookmarkNavigator.MoveToBookmark("test");

//get the paragraph of the bookmark
Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
//get the section of bookmark
Section sec = para.Owner.Owner as Section;
//get the index of paragraph
int paraIndex= sec.Body.ChildObjects.IndexOf(para);
//insert the RTF paragraph at the paraIndex
sec.Body.ChildObjects.Insert(paraIndex, sectmp.Paragraphs[0]);
//remove the previous paragraph
sec.Body.ChildObjects.Remove(para);

DocReceveur.Sections.Remove(sectmp);
DocReceveur.Bookmarks.Remove(BookmrakCurrUnique2);

Paragraph p = DocReceveur.Sections[0].AddParagraph();
p.AppendRTF(rtf);

DocReceveur.SaveToFile("result.docx");
Sincerely,
Nina
E-iceblue support team
User avatar

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

Mon Jul 08, 2019 6:29 am

thank's
i try your code an it's work's on exemple simple model
i can write RTF code to bookmark content it's very good news for me. :D
I must now integrate this solution....
fh

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Mon Jul 08, 2019 1:20 pm

Hi, after a lot of tests :
the problem and unfortunately still present
the result is always same wrong result but i don't knows if it is same bug ,
when you replace a single bookmark, or all the bookmarks of destination doc with one RTF code the the result is GOOD (see the file result_withone_rtfcode.docx)
but if you have multiple RTF code to put on document the result is wrong : (see the file result_multiple_RTF.docx)

I made a code and archive file to locate the problem very precisely :

Code: Select all
           // arial 12
            string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
            // TimeNewRoman 12
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";

            Document DocReceveur = new Document(@"c:\tmp\_Model2957505.docx");
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);   
            string tempo = string.Empty;
            for (int i = DocReceveur.Bookmarks.Count - 1; i >=0 ; i--)
                {
                Bookmark b = DocReceveur.Bookmarks[0];
                Section sectmp = DocReceveur.AddSection();
                if (b.Name == "IRTF_PROJET_10") tempo = rtf10; else tempo = rtf;

 ///////////////////////replace the bottom line by -> sectmp.AddParagraph().AppendRTF(rtf10) for single data rtf/////////////////////////////////////

                sectmp.AddParagraph().AppendRTF(tempo); 
                bookmarkNavigator.MoveToBookmark(b.Name);
                Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph;
                Section sec = para.Owner.Owner as Section;
                //get the index of paragraph
                int paraIndex = sec.Body.ChildObjects.IndexOf(para);
                //insert the RTF paragraph at the paraIndex
                sec.Body.ChildObjects.Insert(paraIndex, sectmp.Paragraphs[0]);
             
                //remove the previous paragraph
                sec.Body.ChildObjects.Remove(para);
                DocReceveur.Sections.Remove(sectmp);
                DocReceveur.Bookmarks.Remove(b);
                }
            DocReceveur.SaveToFile(@"c:\tmp\result.docx");



thank' for your help.
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Tue Jul 09, 2019 3:58 am

Hi,

Thanks for your feedback.
The code you used has some issues, please correct it as follows. And I tested your file with the latest Spire.Doc Pack(hot fix) Version:7.6.16, but didn't encounter the format issue. Attached is my generated Word for your kind reference. If there is any question, please feel free to write back.
Code: Select all
 string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
 string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
 Document DocReceveur = new Document(@"_Model2957505.docx");
 Section addSection = DocReceveur.AddSection();
 addSection.AddParagraph().AppendRTF(rtf10);
 addSection.AddParagraph().AppendRTF(rtf);
 BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
 for (int i = DocReceveur.Bookmarks.Count - 1; i >= 0; i--)
 {
     Bookmark b = DocReceveur.Bookmarks[i];
     string tempo = string.Empty;
     Section sectmp = DocReceveur.AddSection();
     if (b.Name == "IRTF_PROJET_10")
     { tempo = rtf10; }
     else
     { tempo = rtf; }
     sectmp.AddParagraph().AppendRTF(tempo);
     bookmarkNavigator.MoveToBookmark(b.Name);
     Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
     Section sec = para.Owner.Owner as Section;
     //get the index of paragraph
     int paraIndex = sec.Body.ChildObjects.IndexOf(para);
     //insert the RTF paragraph
     sec.Body.ChildObjects.Insert(paraIndex+1, sectmp.Paragraphs[0]);
     //delete bookmark content
     bookmarkNavigator.DeleteBookmarkContent(true);
     DocReceveur.Sections.Remove(sectmp);
     DocReceveur.Bookmarks.Remove(b);
 }
 DocReceveur.Sections.Remove(addSection);
 DocReceveur.SaveToFile("MyResult.docx");
Sincerely,
Nina
E-iceblue support team
Last edited by Nina.Tang on Wed Jul 10, 2019 6:55 am, edited 1 time in total.
User avatar

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

Tue Jul 09, 2019 8:28 am

hi,
i test your code but there is always the wrong format result .
since the beginning of this post i use 7.6.16 spire.doc installed and updated with NuGet ( i test also spire.doc without and with registred with my licene).

my Dev environement :
windows pro 64b N in french version 1803 french language
visual studio Entreprise 19.9.13
i try to compile in X86 or 64 same issue.
i use this code (your last version)

Code: Select all

           String fileName = Spire.License.LicenseProvider.GetLicenseFileName();

            string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
            Document DocReceveur = new Document(@"c:\tmp\_Model2957505.docx");
            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
            for (int i = DocReceveur.Bookmarks.Count - 1; i >= 0; i--)
                {
                Bookmark b = DocReceveur.Bookmarks[i];
                string tempo = string.Empty;
                Section sectmp = DocReceveur.AddSection();
                if (b.Name == "IRTF_PROJET_10")
                    { tempo = rtf10; }
                else
                    { tempo = rtf; }
                sectmp.AddParagraph().AppendRTF(tempo);
                bookmarkNavigator.MoveToBookmark(b.Name);
                Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
                Section sec = para.Owner.Owner as Section;
                //get the index of paragraph
                int paraIndex = sec.Body.ChildObjects.IndexOf(para);
                //insert the RTF paragraph
                sec.Body.ChildObjects.Insert(paraIndex + 1, sectmp.Paragraphs[0]);
                //delete bookmark content
                bookmarkNavigator.DeleteBookmarkContent(true);
                DocReceveur.Sections.Remove(sectmp);
                DocReceveur.Bookmarks.Remove(b);
                }
            DocReceveur.SaveToFile(@"c:\tmp\MyResult.docx");



to have exactly the same code -> i add my licence (see code) (because i dont see the waning license on your result)

there is a difference on file docx your and mine genrerated (90 octect) :
compare.png


the file result is in archive attached to this post.
i try to send an archive of the complete solution .sln in c# test rtf but is 175 Mo rar files and i think it's too for the forum.
if you need this you can give me a link to send you this file.

I do not know what to do more :|


thank 's for your help
fh

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Tue Jul 09, 2019 10:44 am

Hi,

Thanks for your quick reply.
1) The "wrong format" issue you mentioned referred to the extra empty paragraphs (see the attached screenshot), right? If so, please refer to the following code to remove them. If it is not what you want, please share your desired output for a better reference.
2) As for the file size issue, my attached Word is not exactly the same as yours. It contains three pages. Sorry for my mistake. Here I attached the new Word which was generated with the following code for your reference.
3) The forum has size limitation, if you need to send your large solution to us, please upload it via DropBox or OneDrive and then share us with the download link.
Code: Select all
string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
Document DocReceveur = new Document(@"_Model2957505.docx");
Section addSection = DocReceveur.AddSection();
addSection.AddParagraph().AppendRTF(rtf10);
addSection.AddParagraph().AppendRTF(rtf);
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
for (int i = DocReceveur.Bookmarks.Count - 1; i >= 0; i--)
{
    Bookmark b = DocReceveur.Bookmarks[i];
    string tempo = string.Empty;
    Section sectmp = DocReceveur.AddSection();
    if (b.Name == "IRTF_PROJET_10")
    { tempo = rtf10; }
    else
    { tempo = rtf; }
    sectmp.AddParagraph().AppendRTF(tempo);
    bookmarkNavigator.MoveToBookmark(b.Name);
    Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
    Section sec = para.Owner.Owner as Section;
    int paraIndex = sec.Body.ChildObjects.IndexOf(para);
    sec.Body.ChildObjects.Insert(paraIndex + 1, sectmp.Paragraphs[0]);
    DocReceveur.Sections.Remove(sectmp);
    //remove bookmark content
    bookmarkNavigator.DeleteBookmarkContent(true);
    //get the current paragraph
    Paragraph paranow = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
    //remove bookmark
    DocReceveur.Bookmarks.Remove(b);
    if (String.IsNullOrEmpty(paranow.Text))
    {
        //remove the paragraph if empty
        sec.Body.Paragraphs.Remove(paranow);
    }
}
DocReceveur.Sections.Remove(addSection);
DocReceveur.SaveToFile("MyResultNew.docx");
Sincerely,
Nina
E-iceblue support team
User avatar

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

Tue Jul 09, 2019 11:01 am

hi,
i answer to yours first question in last post
1) The "wrong format" issue you mentioned referred to the extra empty paragraphs (see the attached screenshot), right?

NO its always the same problem of convert RTF wrong.
sorry for my poor english ... :oops:

the desired format is the same that your result ....

i try to explain again
with the same code (see picture) my result and yours result are not the same:



ERR.png



the real char font of Var rtf10 is Arial 12
when i use the present code
the result of the on my pc contain rtf10 in time new roman 12 ...

this the same problem since beginning. you can view all the details in the post and files attached before
(we change the methode to put data in bookmarks because with one bookmark the appendrtf fucntion work but when i have integrated the code the appendRTF function dont work with a lot of traitement )

when i write only one bookmark with th rtf10 code (arial font 12) its ok it is in char font arial (i redid the test with only one content rtf code for bookmarks see attached file MyResult_with_one_rtfcode)
but when there is a lot of bookmark to fill (the last code c# we use ) there is this error char font (time new roman instead of arial) .
see post( text and files) in this previous post in, this discution (date Mon Jul 08, 2019 1:20 pm) :

i found more precision :
exemple A :
with one source rtf -> the variable RTF10 -> code rtf font arial objects values
after
Code: Select all
sectmp.AddParagraph().AppendRTF(rtf10)

the result :
onesourcertf.png


Exemple B:
when a lot of sources rtf code are convert
after an another one rtf code with time new roman, char font
Code: Select all
sectmp.AddParagraph().AppendRTF(rtf10)

the same rtf code the same function than the exemple A
the variable RTF10 -> code rtf font arial objects values
the result :

multisourcertf.png


thank's
FH

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Wed Jul 10, 2019 7:26 am

Hello,

Thanks for your feedback and sorry that I misunderstood your meaning before.
According to your screenshot, I know that you were not using the latest code I provided. Actually, the addSection.AddParagraph().AppendRTF(rtf10) plays a decisive role in the "Arial" font. Please add it. And here I uploaded my testing application to help you have a better reference.
On the other hand, here is another way which could achieve the same effect you want. Please refer to the following code.
Code: Select all
string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
Document DocReceveur = new Document(@"../../_Model2957505.docx");
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
for (int i = DocReceveur.Bookmarks.Count - 1; i >= 0; i--)
{
    Bookmark b = DocReceveur.Bookmarks[i];
    string tempo = string.Empty;
    Section sectmp = DocReceveur.AddSection();
    if (b.Name == "IRTF_PROJET_10")
    { tempo = rtf10; }
    else
    { tempo = rtf; }
    sectmp.AddParagraph().AppendRTF(tempo);
    bookmarkNavigator.MoveToBookmark(b.Name);
    Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
    Section sec = para.Owner.Owner as Section;
    int startIndex = para.ChildObjects.IndexOf(bookmarkNavigator.CurrentBookmark.BookmarkEnd);

    //loop through the ChildObjects
    foreach (DocumentObject obj in sectmp.Paragraphs[0].ChildObjects)
    {
        if (obj is TextRange)
        {
            //set the font and size of textrange again
            TextRange range = obj as TextRange;
            if (tempo == rtf10)
            {
                range.CharacterFormat.FontName = "Arial";
                range.CharacterFormat.FontSize = 12f;
            }
            //insert the textrange at the place where the bookmark locates
            para.ChildObjects.Insert(startIndex+1, range.Clone());
        }
        startIndex++;
    }

    //remove bookmark content
    bookmarkNavigator.DeleteBookmarkContent(true);
    //remove bookmark
    DocReceveur.Bookmarks.Remove(b);
    DocReceveur.Sections.Remove(sectmp);
}
DocReceveur.SaveToFile(@"../../20190710_2.docx");
Sincerely,
Nina
E-iceblue support team
User avatar

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

Wed Jul 10, 2019 10:09 am

hi,
actually your solution works but it does not solve my problem
because my exemple include only two rtf code but in the reality the rtf code is stock in bd and can be a la lot (40,45,80...) and write on differents bookmarks .
this a reason who i use a loop.
I need to use appendRTF in loop

with yours code before loop its work for 2 code rtf with constant name rtf10 and rtf :
Code: Select all
 string rtf10 = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
            string rtf = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
            Document DocReceveur = new Document(@"../../_Model2957505.docx");
            Section addSection = DocReceveur.AddSection();
            addSection.AddParagraph().AppendRTF(rtf10);
            addSection.AddParagraph().AppendRTF(rtf);     
           //and loop traitement


i can not code like this for all codes rtf :

addSection.AddParagraph().AppendRTF(rtf10);
addSection.AddParagraph().AppendRTF(rtf);
or
if (tempo == rtf10) -> to change font police because i don't know the police of var rtf10 in the prod context

because the number of rtf code stock in bd, and destination document bookmarks in my app can be more than 80 and it isn't constant may be 85, may be 45 and use differents fonts char ....

i replace your code with this one ( warning ! the array of string rtf length in follow code set to 2 for lighten the example but is variable (may be 85, may be 45, may be more)) :

Code: Select all
 
            // for inforation ->  string namebookmark[] = new string[2]; (same quantity rtf code)
            string[] rtf = new string[2];
           
             rtf[1] = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
             rtf[0] = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
            Document DocReceveur = new Document(@"c:/tmp/_Model2957505.docx");
            Section addSection = DocReceveur.AddSection();
            for (int i = 0; i <= 1;i++)
            {
                addSection.AddParagraph().AppendRTF(rtf[i]);           
                }
 //and loop treatment
// for information - > i replace bookmark[i] content by  rtf[i]


this loop Dont work ...
i add information in code for give you the context

please imagine the array length is 40 or more ...

to resume
i need to write a variable quantity of differents rtf code in same variable quantity of differents bookmarks contents ....

thank's for your help
FH
Last edited by labaffagaston on Wed Jul 10, 2019 4:13 pm, edited 2 times in total.

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Wed Jul 10, 2019 2:06 pm

re
your code to change the char font give me an idea ...
I think I found a temporary solution Can you give me your opinion? :idea:

i use .net RichTextBox to avoid appendrtf bug of recognizing the font of character in loop .
it seems to work ... :?
Code: Select all
 string[] rtf = new string[3];
           
             rtf[1] = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\fs24 \b\i Le contrat joint \'e0 la convocation de la pr\'e9sente assembl\'e9e en l'\'e9tat.\par\b0\i0\f1\par\par}";
             rtf[0] = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}}{\*\generator Riched20 6.3.9600}\viewkind4\uc1 \pard\f0\fs24 Election du Pr\'e9sident de s\'e9ance\par}";
             rtf[2] = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1036{\fonttbl{\f0\fnil\fcharset0 Calibri;}}{\*\generator Riched20 10.0.17134}\viewkind4\uc1\pard\sa200\sl276\slmult1\f0\fs28\lang12 test en calibri \b 14 et gras \i italic\b0\i0\fs22\par}";

            Document DocReceveur = new Document(@"c:/tmp/_Model2957505.docx");

            Section addSection = DocReceveur.AddSection();

            RichTextBox rBox = new RichTextBox();
           
            //for (int i = 0; i <= 2;i++)
            //{
            //   
            //    string[] txt = rBox.Lines;
            //    int vide = rBox.Text.Trim().Length;
            //    addSection.AddParagraph().AppendRTF(rtf[i]);
            //    }

           // addSection.AddParagraph().AppendRTF(rtf10);
           // addSection.AddParagraph().AppendRTF(rtf);

            BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(DocReceveur);
            for (int i = DocReceveur.Bookmarks.Count - 1; i >= 0; i--)
            {
                Bookmark b = DocReceveur.Bookmarks[i];
                string tempo = string.Empty;
                Section sectmp = DocReceveur.AddSection();

                if (b.Name == "IRTF_PROJET_10")
                { tempo = rtf[1]; }

                else if (b.Name == "IRTF_PROJET_9")
                    { tempo = rtf[2]; }
                        else
                    { tempo = rtf[0]; }

                rBox.Rtf = tempo;
                sectmp.AddParagraph().AppendRTF(tempo);
                bookmarkNavigator.MoveToBookmark(b.Name);
                Paragraph para = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
                Section sec = para.Owner.Owner as Section;
                int paraIndex = sec.Body.ChildObjects.IndexOf(para);

                foreach (object it in sectmp.Paragraphs[0].Items)
                    {
                    if (it is Spire.Doc.Fields.TextRange)
                        {
                        Spire.Doc.Fields.TextRange t = (Spire.Doc.Fields.TextRange)it;
                        t.CharacterFormat.FontName = rBox.SelectionFont.Name;
                        }
                    }

                sec.Body.ChildObjects.Insert(paraIndex + 1, sectmp.Paragraphs[0]);
                DocReceveur.Sections.Remove(sectmp);
                //remove bookmark content
                bookmarkNavigator.DeleteBookmarkContent(true);
                //get the current paragraph
                Paragraph paranow = bookmarkNavigator.CurrentBookmark.BookmarkEnd.OwnerParagraph;
                //remove bookmark
                DocReceveur.Bookmarks.Remove(b);
                if (String.IsNullOrEmpty(paranow.Text))
                {
                    //remove the paragraph if empty
                    sec.Body.Paragraphs.Remove(paranow);
                }
            }
          // DocReceveur.Sections.Remove(addSection);
            DocReceveur.SaveToFile(@"c:/tmp/newdocx.docx");
        }
    }

labaffagaston
 
Posts: 42
Joined: Sun Dec 24, 2017 7:38 pm

Return to Spire.Doc