View and Print Office Files in Windows Forms Application

Nowadays, we are most likely to receive or create electronic reports in office file formats, such as .docx, .xlsx, .pptx and .pdf. It’s quite often that programmers are requested to build a .NET application that can view and print office files, so that their colleagues or customers can access the documents too.

This article presents how to view and print Word, Excel, PowerPoint and PDF Documents in a Windows Forms Application via a powerful component Spire.OfficeViewer. To create your own Office document viewer,  you need firstly integrate Spire.OfficeViewer controls to Toolbox in your Visual Studio. Here is how to.

  • Download Spire.OfficeViewer and install it on system
  • Right-click on Toolbox panel, click ‘Add Tab’ to add a new tab
  • Right-click the new tab and select ‘Choose Items…’
  • Choose ‘.NET Framework Components’ tab
  • Click ‘Browse…’ button
  • Choose ‘Spire.OfficeViewer.Forms.dll’ in open file dialog
  • Click ‘OK’, then the controls will be successfully added to Toolbox

Drag ‘ OfficeViewer’ control to form1 and it looks like:

2016-11-07_160901

Run the program, you’re able to get an Office viewer like below screenshot. Look at the toolbar, it provides many more functions other than Open and Print.

2016-11-07_162015

Click Open button to open an office file from disk.

2016-11-07_163716

It is extremely easy to build an Offer viewer using this control, right? If you want to custom the toolbar with specified functions, just remove ‘OfficeViewer’ control and drag ‘DocumentViewer’ control to the form1, and then create your own toolbar using the Common Controls provided by VS, but you need to figure out the code inside the toolbar.

How to Create a Signature Field in PDF in C#

A signature field enables the user to place his signature in a form. This field type allows instant validation from a customer through his digital signature, eliminating the need of paper work. To prepare a document for digital signing, add one or more digital signature fields (depending on how many signatures are needed). This article presents how to create signature fields in a PDF document using Spire.PDF in C#.

Adding Necessary Namespaces

using Spire.Pdf;
using Spire.Pdf.Fields;
using Spire.Pdf.Graphics;
using System.Drawing;

Using the code

STEP 1. Create a PDF document, create a blank page.

PdfDocument pdfdoc = new PdfDocument();
PdfPageBase page = pdfdoc.Pages.Add();

STEP 2. Draw some text in PDF.

PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10f);
PdfSolidBrush brush = new PdfSolidBrush(Color.Black);
string s1 = "Representative #1";
string s2 = "Representative #2";
SizeF size = font.MeasureString(s1);
int x = 180;
int y1 = 120, y2 = 180;
page.Canvas.DrawString(s1, font, brush, new PointF(x, y1));
page.Canvas.DrawString(s2, font, brush, new PointF(x, y2));

STEP 3. Draw two line shapes on PDF.

PdfPen pen = new PdfPen(Color.Black, 0.5f);
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y1 +size.Height+ 2), new PointF(x + size.Width + 126, y1 + size.Height + 2));
page.Canvas.DrawLine(pen, new PointF(x + size.Width + 2, y2 + size.Height + 2), new PointF(x + size.Width + 126, y2 + size.Height + 2));

STEP 4. Create two signature fields in PDF and set the properties respectively.

PdfSignatureField signaturefield1 = new PdfSignatureField(page, "Signature1");
signaturefield1.BorderWidth = 1.0f;
signaturefield1.BorderStyle = PdfBorderStyle.Solid;
signaturefield1.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield1.HighlightMode = PdfHighlightMode.Outline;
signaturefield1.Bounds = new RectangleF(x+size.Width+5, y1 - 50 + size.Height, 120, 50);

PdfSignatureField signaturefield2 = new PdfSignatureField(page, "Signature2");
signaturefield2.BorderWidth = 1.0f;
signaturefield2.BorderStyle = PdfBorderStyle.Solid;
signaturefield2.BorderColor = new PdfRGBColor(System.Drawing.Color.White);
signaturefield2.HighlightMode = PdfHighlightMode.Outline;
signaturefield2.Bounds = new RectangleF(x+size.Width+5, y2 - 50 + size.Height, 120, 50);

doc.Form.Fields.Add(signaturefield1);
doc.Form.Fields.Add(signaturefield2);

STEP 5. Save to file.

doc.SaveToFile("SignatureField.pdf", FileFormat.PDF);

Output

addsignature

Programmatically Accept or Reject Tracked Changes in Word

Before a business contract is signed, the two parties involved may have modified the document several times so as the both side can reach to an agreement. In order to know who’re changing what in your Word document, you can turn on Track Changes, and Accept or Reject the changes to get the tracked changes out of the document.

This article provides an easy solution in C# to accept or reject the tracked changes. Check below code snippets:

using Spire.Doc;
using Spire.Doc.Documents;

namespace AcceptOrRejectChanges
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            Section sec = doc.Sections[0];
            Paragraph para = sec.Paragraphs[0];

            //accept changes
            para.Document.AcceptChanges();

            ////reject changes 
            //para.Document.RejectChanges();

            doc.SaveToFile("result.docx", FileFormat.Docx);
        }
    }

Result

2016-08-30_150234

How to Programmatically Mark up Text in an Existing PDF Document in C#

Users often face the situation that you may need to emphasize the most important information from all text on a PDF page. Highlight text is such a functionality which allow users to give prominence to key words or sentence. In this tip, I will introduce how to mark up text in an existing PDF document programmatically in C#.

This scenario depends on .NET PDF component.  Please download it and add Spire.Pdf.dll as reference in your project. Following code snippets demonstrate how to accomplish this task using the classes provided by this component.

Step 1: Load the sample PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
PdfPageBase page = doc.Pages[0];

Step 2: Find the text (words or sentence) that you want to mark up.

PdfTextFind ptf = page.FindText("Highlight text").Finds[0];

Step 3: Create a Markup Annotation based on the text.

Font font = ptf.Font;
string markupText = "Higlight text";
PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Adminstrator", "Text Markup Annotation", markupText, ptf.Position, new PdfTrueTypeFont(font));
annotation.Border = new PdfAnnotationBorder(0.75f);
annotation.TextMarkupColor = Color.Yellow;

Step 4: Add the annotation to PDF page.

page.AnnotationsWidget.Add(annotation);
doc.SaveToFile("Markup.pdf", FileFormat.PDF);

 Screenshot of Result

gorge_636052500447758198_2016-07-26_151655

 

C# – Add Content Controls to Word Documents

Content controls provide a user interface (UI) that has controlled input like a form, preventing users from editing or deleting parts of a document. This is useful if you have information in a document or template that users should be able to read but not edit, or if you want users to be able to edit content controls but not delete them.

There are nine different types of content controls that you can add to documents. This post will introduce how to create most commonly used content controls in Word documents using .NET Word API in C#.

Spire.Doc provides a StructureDocumentTagInline class to work with all types of content controls. First initialize an object of the class, choose the SDTType as DatePicker, then you’re able to create a date picker content control like this:

paragraph = section.AddParagraph();
StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
paragraph.ChildObjects.Add(sd);
sd.SDTProperties.SDTType = SdtType.DatePicker;
SdtDate date = new SdtDate();
date.CalendarType = CalendarType.Default;
date.DateFormat = "yyyy.MM.dd";
date.FullDate = DateTime.Now;
sd.SDTProperties.ControlProperties = date;
TextRange rt = new TextRange(document);
rt.Text = "1990.02.08";
sd.SDTContent.ChildObjects.Add(rt);

2016-07-05_152012

For more types, please check below code snippet:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;

namespace ContentControls
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a new word document
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //Add Combo Box Content Control
            StructureDocumentTagInline sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.ComboBox;
            SdtComboBox cb = new SdtComboBox();
            cb.ListItems.Add(new SdtListItem("Cat"));
            cb.ListItems.Add(new SdtListItem("Dog"));
            sd.SDTProperties.ControlProperties = cb;
            TextRange rt = new TextRange(document);
            rt.Text = cb.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Text Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Text;
            SdtText text = new SdtText(true);
            text.IsMultiline = true;
            sd.SDTProperties.ControlProperties = text;
            rt = new TextRange(document);
            rt.Text = "Text";
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Picture Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.Picture;
            DocPicture pic = new DocPicture(document) { Width = 10, Height = 10 };
            pic.LoadImage(Image.FromFile(@"D:\Pictures\logo.png"));
            sd.SDTContent.ChildObjects.Add(pic);

            //Add Date Picker Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DatePicker;
            SdtDate date = new SdtDate();
            date.CalendarType = CalendarType.Default;
            date.DateFormat = "yyyy.MM.dd";
            date.FullDate = DateTime.Now;
            sd.SDTProperties.ControlProperties = date;
            rt = new TextRange(document);
            rt.Text = "1990.02.08";
            sd.SDTContent.ChildObjects.Add(rt);

            //Add Drop-Down List Content Control
            paragraph = section.AddParagraph();
            sd = new StructureDocumentTagInline(document);
            paragraph.ChildObjects.Add(sd);
            sd.SDTProperties.SDTType = SdtType.DropDownList;
            SdtDropDownList sddl = new SdtDropDownList();
            sddl.ListItems.Add(new SdtListItem("Harry"));
            sddl.ListItems.Add(new SdtListItem("Jerry"));
            sd.SDTProperties.ControlProperties = sddl;
            rt = new TextRange(document);
            rt.Text = sddl.ListItems[0].DisplayText;
            sd.SDTContent.ChildObjects.Add(rt);

            //Save and launch the file
            string resultfile = "sample.docx";
            document.SaveToFile(resultfile, FileFormat.Docx);
            System.Diagnostics.Process.Start(resultfile);

        }
    }
}