Reading PDF documents using Itextsharp

Source: Internet
Author: User

There are many ways to get the PDF page method has read the flow judgment type/page but after the actual test file is too large often is not very accurate, since so we do not have to bother to write their own can be used itextsharp.

Here is: http://sourceforge.net/projects/itextsharp/files/

(1) Create a Windows console application named Readpdfdemo.
(2) Add a reference to the Itextsharp.
(3) Add the following reference in the "Program.cs" file.
Using System;
Using System.IO;
Using Itextsharp.text;
Using ITextSharp.text.pdf;
(4) The code to directly modify the "Program.cs" file is as follows.
Namespace Readpdfdemo
{
Class Program
{
static void Main (string[] args)
{
Console.WriteLine ("Read PDF document");
Try
{
Create a Pdfreader object
Pdfreader reader = new Pdfreader (@ "C:\origin.pdf");
Get document Pages
int n = reader. Numberofpages;
Get the size of the first page
Rectangle psize = reader. GetPageSize (1);
Float width = psize. Width;
float height = psize. Height;
Create a document variable
Document document = new Document (Psize, 50, 50, 50, 50);
Create the document
PDFWriter writer = pdfwriter.getinstance (document, New FileStream (@ "C:\Read.pdf", FileMode.Create));
Open Document
Document. Open ();
Add content
Pdfcontentbyte cb = writer. Directcontent;
int i = 0;
int p = 0;
Console.WriteLine ("A total of" + N + "pages.");
while (I < n)
{
Document. NewPage ();
p++;
i++;
Pdfimportedpage Page1 = writer. Getimportedpage (reader, i);
Cb. Addtemplate (Page1,. 5f, 0, 0,. 5f, 0, HEIGHT/2);
Console.WriteLine ("Process" + i + "page");
if (I < n)
{
i++;
Pdfimportedpage Page2 = writer. Getimportedpage (reader, i);
Cb. Addtemplate (Page2,. 5f, 0, 0,. 5f, WIDTH/2, HEIGHT/2);
Console.WriteLine ("Process" + i + "page");
}
if (I < n)
{
i++;
Pdfimportedpage Page3 = writer. Getimportedpage (reader, i);
Cb. Addtemplate (Page3,. 5f, 0, 0,. 5f, 0, 0);
Console.WriteLine ("Process" + i + "page");
}
if (I < n)
{
i++;
Pdfimportedpage Page4 = writer. Getimportedpage (reader, i);
Cb. Addtemplate (Page4,. 5f, 0, 0,. 5f, WIDTH/2, 0);
Console.WriteLine ("Process" + i + "page");
}
Cb. Setrgbcolorstroke (255, 0, 0);
Cb. MoveTo (0, HEIGHT/2);
Cb. LineTo (width, HEIGHT/2);
Cb. Stroke ();
Cb. MoveTo (WIDTH/2, height);
Cb. LineTo (WIDTH/2, 0);
Cb. Stroke ();
Basefont bf = Basefont.createfont (Basefont.helvetica, basefont.cp1252, basefont.not_embedded);
Cb. Begintext ();
Cb. Setfontandsize (BF, 14);
Cb. Showtextaligned (Pdfcontentbyte.align_center, "page" + P + "of" + ((N/4) + (n% 4 > 0?) 1:0)), WIDTH/2, 40, 0);
Cb. Endtext ();
}
Close Document
Document. Close ();
}
catch (Exception de)
{
Console.Error.WriteLine (DE. Message);
Console.Error.WriteLine (DE. StackTrace);
}
}
}
}

Reading PDF documents using Itextsharp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.