Working with Word for WPF / Basic Level Working / Adding Quotes
Adding Quotes

You can add quotes from another file in your document using C1Word. You can use following code to add quotes to your document from a text file:

Note that two classes named WordUtils and DataAccess are used in the code given below. These classes are available in the product sample located at the following location on your system:
Documents\ComponentOne Samples\WPF\WordCreator
You can use these classes in your application from the mentioned location.

Dim titleFont As New Font("Arial", 24, RtfFontStyle.Bold)
Dim txtFont As New Font("Times New Roman", 10, RtfFontStyle.Italic)

' add title
Dim rcTop = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc)
rc = rcTop
' build document
For Each s As String In GetQuotes()
        Dim authorQuote As String() = s.Split(ControlChars.Tab)

        ' render header (author)
        Dim author = authorQuote(0)
        rc.Y += 25
        rc = WordUtils.RenderParagraph(word, author, hdrFont, rcPage, rc, True)

        ' render body text (quote)
        Dim text As String = authorQuote(1)
        rc.X = rcPage.X + 36
        ' << indent body text by 1/2 inch
        rc.Width = rcPage.Width - 40
        rc = WordUtils.RenderParagraph(word, text, txtFont, rcPage, rc)
        rc.X = rcPage.X
        ' << restore indent
        rc.Width = rcPage.Width
        rc.Y += 12
        ' << add 12pt spacing after each quote
        If rc.Y > rcPage.Height Then
                word.PageBreak()
                rc = rcTop
        End If
Next

Private Shared Function GetQuotes() As List(Of String)
        Dim list = New List(Of String)()

        Using sr = New StreamReader(DataAccess.GetStream("quotes.txt"))
                Dim quotes = sr.ReadToEnd()
                For Each quote As String In quotes.Split("*"C)
                        Dim pos As Integer = quote.IndexOf(vbCr & vbLf)
                        If pos > -1 Then
                                Dim q = String.Format("{0}" & vbTab & "{1}", quote.Substring(0, pos), quote.Substring(pos + 2).Trim())
                                list.Add(q)
                        End If
                Next
        End Using

        Return list
End Function

The above code reads the quotes from a text file and writes them in a document. It adds title to the document at first, then renders the header and body text, and writes the text in the document.

The output of the above code will look similar to the image given below: