C# tutorial: get information of PDF document


Retrieving information of PDF document

When you are working with an existing PDF file, you might want to get information about the PDF document and its pages. The information can be the version of PDF Editor used to generate the PDF document, number of pages, page size, file lenght, encryption, and so on. With iTextSharp, you can accomplish this task by using the PdfReader class.

Before you can access such information as mentioned above by using the PdfReader class, you need to create a PdfReader object. A constructor of the PdfReader class accepts one argument that is the full path of the PDF document that its information will be retrieved.

String path = "d:/myprogram/pdf/imageeditor.pdf";
PdfReader reader = new PdfReader(path);

To get the version number of an application used to generate the PDF document, you will use the PdfVersion property.

Console.WriteLine("Pdf version: {0}", reader.PdfVersion);

The number of pages of the PDF document can be accessed by using the NumberOfPages property.

Console.WriteLine("Number of pages: {0}",reader.NumberOfPages);

The information related to the page size of the PDF document such as left, right, top, and bottom of a page is accessible through the use of the GetPageSize method.

Rectangle pageSize = reader.GetPageSize(1);
Console.WriteLine("Page Size: {0},{1},{2},{3}",pageSize.Left, pageSize.Right, pageSize.Top,pageSize.Bottom);

You can check whether the PDF document is protected by a password by using the IsEncrypted method.

Console.WriteLine("Is encrypted? {0}", reader.IsEncrypted());

 

PDF files can be protected by two passwords: a user password and an owner password. If the PDF is protected with a user password, you'll have to enter this password before you can open the document in Adobe Reader. If a document has an owner password, you must provide the password along with the constructor of the PdfReader class.

String passw="passw123";

String path = "d:/pdfdoc.pdf";
PdfReader reader = new PdfReader(path,Encoding.ASCII.GetBytes(passw));

pdf information


Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.