ComponentOne Word for UWP
Working with Word for UWP / Basic Level Working / Adding Quotes
In This Topic
    Adding Quotes
    In This Topic

    You can add quotes from another file in your document using Word for UWP.

    Note that a class named WordUtils is used in the code given below. It is available in the product sample located at the following location on your system:
    Documents\ComponentOne Samples\UWP\WordSample
    You can use these classes in your application from the mentioned location.

    You can use following code to add quotes to your document from a text file:

    ' calculate page rect (discounting margins)
    Dim rcPage As Rect = WordUtils.PageRectangle(word)
    Dim rc As Rect = rcPage
    
    ' initialize output parameters
    Dim hdrFont As New Font("Arial", 14, RtfFontStyle.Bold)
    Dim titleFont As New Font("Arial", 24, RtfFontStyle.Bold)
    Dim txtFont As New Font("Times New Roman", 10, RtfFontStyle.Italic)
    
    ' add title
    rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc)
    ' 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 += 20
            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 ' << add 12pt spacing after each quote
            rc.Y += 12
    Next
    Private Shared Function GetQuotes() As List
            Dim list = New List()
    
            Using sr = New StreamReader(GetType(BasicTextPage).GetTypeInfo().Assembly.
                       GetManifestResourceStream("WordSamples.Resources.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
    
    // calculate page rect (discounting margins)
    Rect rcPage = WordUtils.PageRectangle(word);
    Rect rc = rcPage;
    
    // initialize output parameters
    Font hdrFont = new Font("Arial", 14, RtfFontStyle.Bold);
    Font titleFont = new Font("Arial", 24, RtfFontStyle.Bold);
    Font txtFont = new Font("Times New Roman", 10, RtfFontStyle.Italic);
    
    // add title
    rc = WordUtils.RenderParagraph(word, word.Info.Title, titleFont, rcPage, rc);
    
    // build document
    foreach(string s in GetQuotes()) {
      string[] authorQuote = s.Split('\t');
    
      // render header (author)
      var author = authorQuote[0];
      rc.Y += 20;
      rc = WordUtils.RenderParagraph(word, author, hdrFont, rcPage, rc, true);
    
      // render body text (quote)
      string text = 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
    }
    
    static List GetQuotes() {
      var list = new List();
    
      using(var sr = new StreamReader(typeof(BasicTextPage).GetTypeInfo().Assembly.
                     GetManifestResourceStream("WordSamples.Resources.quotes.txt"))) {
        var quotes = sr.ReadToEnd();
        foreach(string quote in quotes.Split('*')) {
          int pos = quote.IndexOf("\r\n");
          if (pos > -1) {
            var q = string.Format("{0}\t{1}", quote.Substring(0, pos), quote.Substring(pos + 2).Trim());
            list.Add(q);
          }
        }
      }
    
      return list;
    }
    

    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: