Set PDF Document Properties – C#

If we want to learn something about information of one PDF document, we can get properties of this document.

Generally speaking, there are four items included in PDF properties, Description, Security, Fonts and Advanced. Description is has two parts, Description and advanced information. Description shows document title, author, created time and some other basic information, while advance shows document location, file size and other information. Security shows some protection information. Fonts show how many kinds of font style used in document. The last Advanced is about other settings for PDF, for example, reading and printing settings.

In this post, I will introduce a method about how to set PDF properties by using C#.

In my example, I will set document and file information for PDF, which will be shown as description. And I use a component Spire.PDF. So, I add its dll file as reference in project.

Steps:

  1. Create a PDF document and add page for it.
  2. Draw this page. I just add an image which is a table about employee information on this page.
  3. Set document information, including author, creator, keywords, producers, subject and title.
  4. Set file information, including cross reference type, incremental update and PDF version.
  5. Save and launch file.

Main Coding:

usingSystem;

usingSystem.Drawing;

usingSpire.Pdf;

usingSpire.Pdf.Graphics;

 

namespacePDFProperties

{

    class property

    {

        static void Main(string[] args)

        {

            //Create a pdf document.

            PdfDocument doc = new PdfDocument();

 

            // Create one page

            PdfPageBase page = doc.Pages.Add();

 

            //Draw Page

            DrawPage(page);

 

            //Document Info

            doc.DocumentInformation.Author = "Sunny";

            doc.DocumentInformation.Creator = "Sunny";

            doc.DocumentInformation.Keywords = "Employee Information";

            doc.DocumentInformation.Producer = "Spire.Pdf";

            doc.DocumentInformation.Subject = "Data about employees personal information.";

            doc.DocumentInformation.Title = "EmployeeInfo";

 

            //File Info

            doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream;

            doc.FileInfo.IncrementalUpdate = false;

            doc.FileInfo.Version = PdfVersion.Version1_5;

 

            //Save

            doc.SaveToFile("Properties.pdf");

            doc.Close();

 

            //Launch

            System.Diagnostics.Process.Start("Properties.pdf");

        }

        private static void DrawPage(PdfPageBase page)

        {

            //Draw Image

            PdfImage image = PdfImage.FromFile(@"D:\work\My Documents\ei.png");

            float width = image.Width * 0.65f;

            float height = image.Height * 0.65f;

            float x = (page.Canvas.ClientSize.Width – width) / 2;

 

            page.Canvas.DrawImage(image, x, 40, width, height);

        }

    }

}

Result Shown by Following:

________________________________________________________________________________________________

Click Here to Learn more about Spire.PDF

Click Here to Download Spire.PDF

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

2 thoughts on “Set PDF Document Properties – C#”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s