This is a migrated thread and some comments may be shown as answers.

RadDocument - programatically add watermark to MS Word document

9 Answers 429 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
C
Top achievements
Rank 1
C asked on 01 Jul 2014, 04:55 PM
How would I programmatically add a text watermark to a Word document?

We are using C#, .NET 4.5 for a WPF desktop application on Windows 7.  Telerik UI for WPF version 2014.2.617.45 is used.

This needs to
1. Create a new word document
2. Add several pages of text, tables and images to the word document
3. Add a user entered watermark such as "For Internal Use Only" to each page at 44 point and angled from bottom left to top right.
4. The watermark should appear on all pages
5. Save the document in MS Word 2010 format if possible

It needs to be done in C# since there is no RichTextBox control in the application.


9 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 03 Jul 2014, 08:48 AM
Hi,

It is possible to apply a watermark through code either using RadRichTextBox's API or a RadDocumentEditor instance. This article demonstrates how to apply a watermark using RadRichTextBox and the below snippet demonstrates the use of RadDocumentEditor in the same scenario.
RadDocumentEditor editor = new RadDocumentEditor(myDocument);
 
WatermarkTextSettings textSettings = new WatermarkTextSettings();
textSettings.Text = "For Internal Use Only";
textSettings.RotateAngle = 30;
textSettings.Opacity = 1;
textSettings.ForegroundColor = Colors.Gray;
 
DocumentWatermarkSettings settings = new DocumentWatermarkSettings() { Type=WatermarkType.TextWatermark };
settings.TextSettings = textSettings;
 
editor.SetWatermark(settings);

As to exporting to DOCX, here you can learn how you can use the format providers that come out of the box with the control. In this case you should use the DocxFormatProvider class.

I hope this is helpful.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
C
Top achievements
Rank 1
answered on 03 Jul 2014, 03:53 PM
Thank you for the information.

When adding the watermark before adding the first section, it adds a section break/blank page before the first page with text content.
It works OK when you do the following
 1) add the first page of the document (text, images, etc.),
  2) add the watermark
  3) add the second page, third page, ...
  4) Export to word

One thing that is confusing is how to switch between adding content text spans and paragraphs to the document  using  Document.Sections.Add() and using a RadDocumentEditor object.

Here is a simplified version of it.  How can we achieve the simple step 2 for text color, size?

1. Create RadDocument, RadDocumentEditor, paper size, margin and page orientation
        RadDocument doc = new RadDocument()
        RadDocumentEditor ed = new RadDocumentEditor(doc)
        doc.SectionDefauiltPageSize = PaperTypeConverter.ToSize(PaperTypes.Letter);
        doc.SectionDefauiltPageOrientation = PageOrientation.Portrait;
2.  Create document body
         Insert "ABC" in Calibri 8 point red text followedeby a line break
         Insert "DEF" in Arial 12 point blue text - bold followed by a line break
         Insert "GHI" in Calibri 12 point black italic text followed by a line break
3. Export to Word docx file
   using (Stream outStream = new FileStream("outputFile.docx", FileMode.Create))
   {
     DocxFormatProvider provider = new DocxFormatProvider();
     provider.Export(document, outStream);
   }
 
​
0
C
Top achievements
Rank 1
answered on 03 Jul 2014, 05:07 PM
Clarified question:
   How can we achieve the simple step 2 using a RadDocumentEditor for text color, size?


0
Petya
Telerik team
answered on 07 Jul 2014, 12:29 PM
Hello,

The Document.Sections.Add(), Block.Inlines.Add(), etc. method are intended for non-measured documents. As opposed to that, the Insert~() methods are intended for measured documents (usually, but not exclusively shown in RadRichTextBox). Basically, when creating a document from code you should choose one of the approaches - either build it with the collections or use the methods of the editor, and stick to it. The behavior you explained seems to be caused by mixing the two approaches.

My previous reply shows how to apply a watermark through RadDocumentEditor. In case you are having troubles with this please provide more information on the matter.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
C
Top achievements
Rank 1
answered on 07 Jul 2014, 03:30 PM
Thank  you for the response.

Q1: Is it possible to add a watermark without using the RadDocumentEditor?   Something like document.SetWatermark( DocumentWatermarkSettings wmk) would be helpful.

We're generating the Word docx document offline without a RichTextBox control.  Each page of the word document is generated in sequential order; this is generating a software execution report combined of about 10 sections with a mix of text, tables, images/graphs, etc.  It has a header, footer and a watermark.

So far, only the table of contents field and watermark appear to need a RadDocumentEditor.

This could follow three tracks in RichTextBox and RadDocument functionality:
  1. Visible RichTextBox with interactive user editing and programmatically changing text (e.g, button click to change font bold style)
           1a. Document defined largely in XAML
            1b. Document created in C# code
  2. Offline generation of a Word document without a RichTextBox.  Doing so without a RadDocumentEditor would help
  3. Modifying an existing document. 
           3a. Read in the document, insert new section, insert table, write back out.  
           3b. File conversion, convert from one doucment type to another in C# code

0
Petya
Telerik team
answered on 10 Jul 2014, 10:30 AM
Hello,

Watermarks are properties of a section's header and it is possible to set them without a document editor.
WatermarkTextSettings textSettings = new WatermarkTextSettings();
textSettings.Text = "For Internal Use Only";
textSettings.RotateAngle = 30;
textSettings.Opacity = 1;
textSettings.ForegroundColor = Colors.Gray;
 
DocumentWatermarkSettings settings = new DocumentWatermarkSettings() { Type = WatermarkType.TextWatermark };
settings.TextSettings = textSettings;
 
Section s = new Section();
s.Headers.Default.Body = new RadDocument();
s.Headers.Default.WatermarkSettings = settings;

Please note that in this case the document you add the section to should be in its non-measured state.

When it comes to fields, some of them, especially the more complex ones like TableOfContentsField, can only be evaluated properly in a measured document which is why there is no straightforward way to add them without using the InsertField() method of a document editor.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
C
Top achievements
Rank 1
answered on 10 Jul 2014, 02:33 PM
Please add this to the help for the WaterMarkTextSettings() object.   These code examples will greatly help out developers.

A watermark may be added to the section header as follows.

WatermarkTextSettings textSettings = new WatermarkTextSettings();
textSettings.Text = "For Internal Use Only";
textSettings.RotateAngle = 30;
textSettings.Opacity = 1;
textSettings.ForegroundColor = Colors.Gray; 
DocumentWatermarkSettings settings = new DocumentWatermarkSettings()  { Type = WatermarkType.TextWatermark };
settings.TextSettings = textSettings; 
Section s = new Section();
s.Headers.Default.Body = new RadDocument();
s.Headers.Default.WatermarkSettings = settings;
0
C
Top achievements
Rank 1
answered on 01 Oct 2014, 10:30 PM
Please consider a small API change to let the watermark be set on the section and not on the Headers.  
Word uses the headers but this is misleading as the watermark applies to the section.

This apparently is far too obscured in the Word object model documentation to be easily found.
0
Petya
Telerik team
answered on 06 Oct 2014, 01:00 PM
Hello,

Thank you for the suggestion. At this point we are not considering the implementation of this change in the API.

RadRichTextBox's document model allows to apply different headers/footers to the first and latter pages of a section and since watermarks are preserved in the header, this practically means you can set a different watermark for the first page of each section. Since this is the behavior according to the DOCX specification, and as you pointed out the behavior in other editors such as MS Word, we are not planning a change.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
RichTextBox
Asked by
C
Top achievements
Rank 1
Answers by
Petya
Telerik team
C
Top achievements
Rank 1
Share this question
or