4/09/2012

C#/VB.NET —Three Methods to Protect Word Document with Special Restrictions

When we protect the word document from opening, reading and modifying, we often use the encryption function. However, what if we only allow other people to comment, to edit form field or review? Even our document is read by others, they only can use one defined function or two or three certain functions permitted by the owner to change the content. Thus, we need to protect our word document with special restrictions. Spire.Doc meets customer need to protect word document with Special restriction with C#, VB.NET.

Spire.Doc,   as a professional and powerful MS Word component, enables users to directly operate Word document, format, style and insert content to word document and allows developers to perform a wide range of tasks…

Necessary Tools:

Spire.Doc and Visual Studio are necessary to be installed in the system in order to realize the task.

Freely Download Spire.Doc


Three Methods to protect word document with special restrictions via Spire. Doc with C#,VB.NET.
Allow Only Comments
document.Protect(ProtectionType.AllowOnlyComments, "*******");
Allow Only Form Field
document.Protect(ProtectionType.AllowOnlyFormFields, "*******");
Allow Only Revision
document.Protect(ProtectionType.AllowOnlyRevisions, "*******");

Suggest we allow comments, Form Field and Revisions all the three, we can use the following method:
C# Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Protect_Word
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(@"D:\Sample.doc");

            document.Protect(ProtectionType.AllowOnlyComments, "everlasting129");
            document.Protect(ProtectionType.AllowOnlyFormFields, "everlasting129");
            document.Protect(ProtectionType.AllowOnlyRevisions, "everlasting129");

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");
        }

        private void WordDocViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

    }
}


VB.NET Code:
Imports System;
Imports System.Collections.Generic;
Imports System.ComponentModel;
Imports System.Data;
Imports System.Drawing;
Imports System.Linq;
Imports System.Text;
Imports System.Windows.Forms;
Imports Spire.Doc;
Imports Spire.Doc.Documents;

Namespace protectWord
                Partial Public Class Form1
                                Inherits Form
                                Public Sub New()
                                                InitializeComponent()
                                End Sub

                                Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
                                                'Create word document
                                                Dim document As New Document()
                                                document.LoadFromFile("D:\Sample.doc")

            document.Protect(ProtectionType.AllowOnlyComments, "everlasting129");
            document.Protect(ProtectionType.AllowOnlyFormFields, "everlasting129");
            document.Protect(ProtectionType.AllowOnlyRevisions, "everlasting129");

                                                'Save doc file.
                                                document.SaveToFile("Sample.doc", FileFormat.Doc)

                                                'Launching the MS Word file.
                                                WordDocViewer("Sample.doc")
                                End Sub

                                Private Sub WordDocViewer(ByVal fileName As String)
                                                Try
                                                                System.Diagnostics.Process.Start(fileName)
                                                Catch
                                                End Try
                                End Sub

                End Class
End Namespace


 

3/28/2012

How to Add PDF link with C#,VB.NET


Link has already becomes a quickest way for people to reach one webpage from another. When people search something on internet, they only need to insert the keywords in search engine, and click the right link. They can find what they want. But link function is used far from this. Link also can be inserted to different documents of various formats. PDF is just one. When we click the link in the documents, we also can reach another webpage. Link is really marvelous. Now, let us see how to add link in PDF Document with C#,VB.NET.
In order to realize my task smoothly, I choose a PDF document creation component Spire.PDF for .NET to help me, you can:



Procedure
Step1. Create a new project.
1.Create a new project in Console Application.
2.Set the target Framework to be the .NET Framework 4.

Step2. Add reference.
1.Add Spire.PDF Dll and System Drawing as references.
2.Add the following using to the top of the method.

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Actions;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;



Step3. Insert link in PDF Document.
1.Create a PDF Document and set its margin.

C# Code:
      //Create a pdf document.
            PdfDocument doc = new PdfDocument();

            //margin
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;


VB.NET Code:

               'Create a pdf document.
            Dim doc As New PdfDocument()

            'margin
            Dim unitCvtr As New PdfUnitConvertor()
            Dim margin As New PdfMargins()
            margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
            margin.Bottom = margin.Top
            margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
            margin.Right = margin.Left
 

2.Create one page and add links in the page .
C# Code:
            // Create one page
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

            float y = 10;
            float x = 0;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 12));
            String label = "Simple Link: ";
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
            x = font.MeasureString(label, format).Width;
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 12, FontStyle.Underline));
            String url1 = "http://www.e-iceblue.com";
            page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y);
            y = y + font1.MeasureString(url1).Height;

            label = "Web Link: ";
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
            x = font.MeasureString(label, format).Width;
            String text = "e-iceblue";
            PdfTextWebLink link2 = new PdfTextWebLink();
            link2.Text = text;
            link2.Url = url1;
            link2.Font = font1;
            link2.Brush = PdfBrushes.Blue;
            link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
            y = y + font1.MeasureString(text).Height;

            label = "URI Annonationa: ";
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
            x = font.MeasureString(label, format).Width;
            text = "Google";
            PointF location = new PointF(x, y);
            SizeF size = font1.MeasureString(text);
            RectangleF linkBounds = new RectangleF(location, size);
            PdfUriAnnotation link3 = new PdfUriAnnotation(linkBounds);
            link3.Border = new PdfAnnotationBorder(0);
            link3.Uri = "http://www.google.com";
            (page as PdfNewPage).Annotations.Add(link3);
            page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
            y = y + size.Height;

            label = "URI Annonationa Action: ";
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format);
            x = font.MeasureString(label, format).Width;
            text = "JavaScript Action (Click Me)";
            location = new PointF(x, y);
            size = font1.MeasureString(text);
            linkBounds = new RectangleF(location, size);
            PdfUriAnnotation link4 = new PdfUriAnnotation(linkBounds);
            link4.Border = new PdfAnnotationBorder(0.75f);
            link4.Color = Color.LightGray;
            //script
            String script
                = "app.alert({"
                + "    cMsg: \"Hello.\","
                + "    nIcon: 3,"
                + "    cTitle: \"JavaScript Action\""
                + "});";
            PdfJavaScriptAction action = new PdfJavaScriptAction(script);
            link4.Action = action;
            (page as PdfNewPage).Annotations.Add(link4);
            page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y);
            y = y + size.Height;

VB.NET Code

           ' Create one page
            Dim page As PdfPageBase = doc.Pages.Add(PdfPageSize.A4, margin)

            Dim y As Single = 10
            Dim x As Single = 0
            Dim font As New PdfTrueTypeFont(New Font("Arial", 12))
            Dim label As String = "Simple Link: "
            Dim format As New PdfStringFormat()
            format.MeasureTrailingSpaces = True
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
            x = font.MeasureString(label, format).Width
            Dim font1 As New PdfTrueTypeFont(New Font("Arial", 12, FontStyle.Underline))
            Dim url1 As String = "http://www.e-iceblue.com"
            page.Canvas.DrawString(url1, font1, PdfBrushes.Blue, x, y)
            y = y + font1.MeasureString(url1).Height

            label = "Web Link: "
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
            x = font.MeasureString(label, format).Width
            Dim text As String = "e-iceblue"
            Dim link2 As New PdfTextWebLink()
            link2.Text = text
            link2.Url = url1
            link2.Font = font1
            link2.Brush = PdfBrushes.Blue
            link2.DrawTextWebLink(page.Canvas, New PointF(x, y))
            y = y + font1.MeasureString(text).Height

            label = "URI Annonationa: "
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
            x = font.MeasureString(label, format).Width
            text = "Google"
            Dim location As New PointF(x, y)
            Dim size As SizeF = font1.MeasureString(text)
            Dim linkBounds As New RectangleF(location, size)
            Dim link3 As New PdfUriAnnotation(linkBounds)
            link3.Border = New PdfAnnotationBorder(0)
            link3.Uri = "http://www.google.com"
            TryCast(page, PdfNewPage).Annotations.Add(link3)
            page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
            y = y + size.Height

            label = "URI Annonationa Action: "
            page.Canvas.DrawString(label, font, PdfBrushes.OrangeRed, 0, y, format)
            x = font.MeasureString(label, format).Width
            text = "JavaScript Action (Click Me)"
            location = New PointF(x, y)
            size = font1.MeasureString(text)
            linkBounds = New RectangleF(location, size)
            Dim link4 As New PdfUriAnnotation(linkBounds)
            link4.Border = New PdfAnnotationBorder(0.75F)
            link4.Color = Color.LightGray
            'script
            Dim script As String _
                = "app.alert({" _
                & "    cMsg: ""Hello.""," _
                & "    nIcon: 3," _
                & "    cTitle: ""JavaScript Action""" _
                & "});"
            Dim action As New PdfJavaScriptAction(script)
            link4.Action = action
            TryCast(page, PdfNewPage).Annotations.Add(link4)
            page.Canvas.DrawString(text, font1, PdfBrushes.Blue, x, y)
            y = y + size.Height


Step4. Save the PDF file and preview
C# Code:
            //Save pdf file.
            doc.SaveToFile("Link.pdf");
            doc.Close();

            //Launching the Pdf file.
            System.Diagnostics.Process.Start("Link.pdf");
VB.NET Code:

            'Save pdf file.
            doc.SaveToFile("Link.pdf")
            doc.Close()

            'Launching the Pdf file.
            Process.Start("Link.pdf")



Preview