How to insert RTF text in a document

Technical support for Spire.Doc

Moderator: iceblue support

How to insert RTF text in a document

Postby cgueldre » Tue Jun 21, 2011 9:31 am

Hi everybody,

My problem :
1.1.1.1. Art.1 OBJET ET ETENDUE DE L'ENTREPRISE
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\fnil\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue0;}
\viewkind4\uc1\pard\qj\lang1036\f0\fs24 L\rquote entreprise a pour objet la r\'e9alisation du projet de construction et/ou des travaux d\'e9finis par les diff\'e9rents documents du march\'e9.\par
Selon les dispositions du Cahier Sp\'e9cial des Charges, l\rquote entreprise est g\'e9n\'e9rale ou est adjug\'e9e par lots s\'e9par\'e9s. \par
\pard\cf1\lang1033\f1\fs18\par
}

I read
"1.1.1.1. Art.1 OBJET ET ETENDUE DE L'ENTREPRISE "
in a .txt file and insert it into my document :OK
attached to this I have a description saved in a .rtf file that I must include in my document after "1.1.1.1. Art.1 OBJET ET ETENDUE DE L'ENTREPRISE" as shown.
How to insert the RTF text(file) to have a result such the next one :
"En cas de contradiction entre les documents du marché, la primauté sera réglée dans l’ordre suivant :
- le contrat ;
- l’offre acceptée par le maître de l’ouvrage ;
- les indications du plan ;
- les indications du métré ;
- les conditions particulières ;
- les conditions générales."

Thanks
Regards
Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby cgueldre » Tue Jun 21, 2011 10:02 am

Hi,
I find a solution
There is maybe better, but it's running.
Code: Select all
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
                If Dir(CheminFichier) <> "" Then
                    paragraph_Renamed = section.AddParagraph()
                    Dim document_RTF As New Document(CheminFichier)
                    paragraph_Renamed.AppendText(document_RTF.GetText)
                    paragraph_Renamed.ApplyStyle("Normal")
                    document_RTF = Nothing
                End If


PS : how can I mark this as solved
Regards
Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby Justin Weng » Tue Jun 21, 2011 10:17 am

Dear cgueldre,
Thanks for your solution.
We will research it.
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: How to insert RTF text in a document

Postby cgueldre » Tue Jun 21, 2011 5:05 pm

Hi Justin,

I'm coming back because I'm not completely satisfied by the solution.
There are some colored text in the .rtf files and after import into my document the colors are lost.
Code: Select all
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
                If Dir(CheminFichier) <> "" Then
                    paragraph_Renamed = section.AddParagraph()
                    Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
                    paragraph_Renamed.AppendText(document_RTF.GetText())
                    paragraph_Renamed.ApplyStyle("Normal")
                    document_RTF = Nothing
                End If

The next step would be : find the text colored in (128,0,0) and replace it by nothing (for example)
Here is the VBA code
Code: Select all
With Selection.Find
      .Font.Color = RGB(128, 0, 0)
      .Replacement.Text = ""
End With
Selection.Find.Execute Replace:=wdReplaceAll


What's the solution?

Regards

Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby Justin Weng » Wed Jun 22, 2011 10:01 am

Dear cgueldre,
Thanks for your inquiry.
Your solution do miss text formating, because you just copy the text.
You want to merge file? If so, here is a solution.
Code: Select all
//Create word document
                Document document = new Document();
                document.LoadFromFile(fileName,FileFormat.Doc);

                Document documentMerge = new Document();
                documentMerge.LoadFromFile(fileMerge, FileFormat.Rtf);

                foreach( Section sec in documentMerge.Sections)
                {
                    document.Sections.Add(sec.Clone());
                }

                //Save doc file.
                document.SaveToFile("Sample.doc", FileFormat.Doc);
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: How to insert RTF text in a document

Postby cgueldre » Wed Jun 22, 2011 10:50 am

Dear Justin,

with your solution the format is preserved but the texts are append at the end of the document each time as a new section with page break.

Here attached an example of the result I have to produce.
In grey the article Index, code and title read in a .txt file and after the article's title I have to insert the description coming from a .rtf or .doc file.

Thanks

Regards

Charles
You do not have the required permissions to view the files attached to this post.
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby cgueldre » Wed Jun 22, 2011 12:41 pm

Hi Justin,

With the following coding the result is nearly correct
Code: Select all
Dim document_Renamed As New Document(CheminDuCDCType & NomDuCDCType)
Dim section As Section = document_Renamed.Sections(2)
Dim paragraph_Renamed As Paragraph
.
.
.
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
Dim strFichierTrouve As String = Dir(CheminFichier)
If   strFichierTrouve <> "" Then
        paragraph_Renamed = section.AddParagraph()
        Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
        For Each Sec As Section In document_RTF.Sections
                  For Each P As Paragraph In Sec.Paragraphs
                            NoParagraphe = section.Paragraphs.Add(P.Clone)
                            paragraph_Renamed = section.Paragraphs(NoParagraphe)
                            paragraph_Renamed.ApplyStyle("Normal")
                  Next
        Next
        document_RTF = Nothing
End If

but <paragraph_Renamed.ApplyStyle("Normal")> do nothing
The Normal style must the one of the document <CheminDuCDCType & NomDuCDCType> called to open document_Renamed

we advance and approach the final goal.

In my application I use MSWord and OOWriter macros to generate .doc documents.
I want to replace those 2 // dev par spire.office
but for that I must solve all the problems.

I realize something similar with MSExcel et OOCalc.

Thanks for your help

Regards

Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby Jhesan » Thu Jun 23, 2011 4:49 am

Hope this will work in my software.Thanks.
Jhesan
 
Posts: 5
Joined: Thu Jun 23, 2011 3:33 am

Re: How to insert RTF text in a document

Postby Justin Weng » Thu Jun 23, 2011 6:30 am

Dear cgueldre,
Thanks for your inquiry.
We can't reproduce your problem.
But now, the problem is style.
I checked your code, maybe you did wrong order.
You should apply firstly, and then add to doc.
like this.
Code: Select all
Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
Dim tempParagraph As Paragraph
        For Each Sec As Section In document_RTF.Sections
                  For Each P As Paragraph In Sec.Paragraphs
                            tempParagraph=P.Clone
                            tempParagraph.ApplyStyle("Normal")
                            section.Paragraphs.Add(tempParagraph)
                   Next
        Next

If you still have some problems, please upload your sample demo and your rtf file so that we can reproduce your problem.
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: How to insert RTF text in a document

Postby cgueldre » Thu Jun 23, 2011 8:16 am

Hi Justin,

sorry but the problem is not solved.
I want to apply my own style to the added paragraph from the .rtf file.
When I try as you suggest:
Code: Select all
Dim document_Renamed As New Document
document_Renamed.LoadFromFile(CheminDuCDCType & NomDuCDCType, FileFormat.Doc)
Dim section As Section = document_Renamed.Sections(2)
Dim paragraph_Renamed As Paragraph
.
.
.
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
Dim strFichierTrouve As String = Dir(CheminFichier)
If strFichierTrouve <> "" Then
        paragraph_Renamed = section.AddParagraph()
        paragraph_Renamed.ApplyStyle("Normal_CDC")
        Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
        For Each Sec As Section In document_RTF.Sections
                 For Each P As Paragraph In Sec.Paragraphs
                         Dim TempParagraph As Paragraph
                         TempParagraph = P.Clone
                         TempParagraph.ApplyStyle("Normal_CDC")
                         section.Paragraphs.Add(TempParagraph)
                 Next
         Next
         document_RTF = Nothing
End If

Message is : specified par(t)agraph style not found
But if I do the following, the style result for the added paragraph is
<Normal_CDC + 12 pt Justified Line spacing: At least 0 pt>
or
<Normal_CDC + Symbol (symbol) 12 pt>
or
...


Code: Select all
Dim document_Renamed As New Document
document_Renamed.LoadFromFile(CheminDuCDCType & NomDuCDCType, FileFormat.Doc)
Dim section As Section = document_Renamed.Sections(2)
Dim paragraph_Renamed As Paragraph
.
.
.
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
Dim strFichierTrouve As String = Dir(CheminFichier)
If strFichierTrouve <> "" Then
        paragraph_Renamed = section.AddParagraph()
        paragraph_Renamed.ApplyStyle("Normal_CDC")
        Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
        For Each Sec As Section In document_RTF.Sections
                 For Each P As Paragraph In Sec.Paragraphs
                         section.Paragraphs.Add(P.Clone)
                         paragraph_Renamed = document_Renamed.LastParagraph()
                         paragraph_Renamed.ApplyStyle("Normal_CDC")
                 Next
         Next
         document_RTF = Nothing
End If


In fact , I want just change the font name and the font size of the added paragraph according to the font name and font size of the Normal_CDC style.

Thanks

Regards

Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby Justin Weng » Thu Jun 23, 2011 8:50 am

Dear cgueldre,
Thanks for your inquiry.
Could you give your style code?
So I could have a test.
Sorry for long waiting....
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: How to insert RTF text in a document

Postby cgueldre » Thu Jun 23, 2011 8:56 am

Hi Justin,

what do you mean by Style Code.

Regards

Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am

Re: How to insert RTF text in a document

Postby Justin Weng » Thu Jun 23, 2011 9:25 am

Dear cgueldre,
Thanks for your inquiry.
I found a solution. Sorry for C# code, I'm not fimiliar with VB.NET.
What we did before was wrong, because when write the docoment it still use Original format.
Code: Select all
  foreach (Paragraph p in sec.Paragraphs)
                    {
                        para = document.LastSection.AddParagraph();
                        para = (Paragraph)p.Clone();                       
                        document.LastSection.Paragraphs.Add(para);
                        //para.ApplyStyle("myStyle");
                        foreach (ParagraphBase item in para.Items)
                        {
                            if (item.DocumentObjectType == DocumentObjectType.TextRange)
                            {
                               TextRange range=item as TextRange;
                                range.CharacterFormat.FontSize=12;
                                range.CharacterFormat.FontName="Arial Unicode MS";
                                range.CharacterFormat.TextColor = Color.Red;
                            }
                        }
                        //document.LastSection.Paragraphs.Add((Paragraph)p.Clone());
                    }
Justin
Technical Support / Developer,
e-iceblue Support Team
User avatar
Justin Weng
 
Posts: 110
Joined: Mon Mar 28, 2011 5:54 am

Re: How to insert RTF text in a document

Postby cgueldre » Thu Jun 23, 2011 9:57 am

Hi Justin,

It's OK.

Here my final code for that which that can help.
Code: Select all
Dim document_Renamed As New Document
document_Renamed.LoadFromFile(CheminDuCDCType & NomDuCDCType, FileFormat.Doc)
Dim NomPolice As String, TaillePolice As Single
For Each BS As Style In document_Renamed.Styles
      If BS.Name = "Normal_CDC" Then
              NomPolice = BS.CharacterFormat.FontName
              TaillePolice = BS.CharacterFormat.FontSize
              Exit For
       End If
Next
Dim section As Section = document_Renamed.Sections(2)
Dim paragraph_Renamed As Paragraph
.
.
.
CheminFichier = Environ("Temp") & "\" & IdDossier & "\RTF" & Niveaux & ".rtf"
Dim strFichierTrouve As String = Dir(CheminFichier)
If strFichierTrouve <> "" Then
        paragraph_Renamed = section.AddParagraph()
        paragraph_Renamed.ApplyStyle("Normal_CDC")
        Dim document_RTF As New Document(CheminFichier, FileFormat.Rtf)
        For Each Sec As Section In document_RTF.Sections
                  For Each P As Paragraph In Sec.Paragraphs
                            section.Paragraphs.Add(P.Clone)
                            paragraph_Renamed = document_Renamed.LastParagraph()
                            paragraph_Renamed.ApplyStyle("Normal_CDC")
                            For Each item As ParagraphBase In paragraph_Renamed.Items
                                If (item.DocumentObjectType = DocumentObjectType.TextRange) Then
                                    Dim range As TextRange = item
                                    range.CharacterFormat.FontSize = TaillePolice
                                    range.CharacterFormat.FontName = NomPolice
                                End If
                            Next
                  Next
        Next
        document_RTF = Nothing
End If


Thanks a lot

Regards

Charles
cgueldre
 
Posts: 11
Joined: Tue Jun 14, 2011 11:00 am


Return to Spire.Doc

Who is online

Users browsing this forum: No registered users and 1 guest