Create PDF-iTextSharp in ASP. NET

Source: Internet
Author: User

 

 

. Net framework does not contain methods that can be used with pdf files. So when you need your ASP. when a. Net Web application contains a part that is created or interacts with a PDF file, you have to find available third-party components. you can use Google to search for charged components within your budget. Of course, there are also some open-source components. One of them is iTextSharp, which is the. Net version of the famous JAVA tool iText.

 

However, the biggest problem with iTextSharp is the lack of documentation. although there are some tutorials on the official website, most programmers still choose to read the documentation of the JAVA version-that is, the iText documentation. or you can buy the only book on the market, iText in Action. however, this book is for the Java version of iText. in iText in Action, most of the code can be stored in.. Net, but if your C # level is relative to the dish, Java and.. Net version. the lack of documentation in the. Net version will often drive you crazy. In the end, you can only use Reflector to check what some methods are used. Therefore, as a "How to" series of articles, this article will show you How to start using C # iTextSharp.

 

The first thing is to download iTextSharp here. After the download is complete, decompress the zip file to get itextsharp. dll file, create a new website in Visual Studio or Web Developer, add the bin directory by adding the Asp.net folder option, right-click the bin directory and choose add reference option. On the Browse tab, select itextsharp. dll:

Click OK and the dll will be added to the bin directory. Now you can use iTextSharp on your website or project.

I also added a folder named PDFs for storing generated PDF files. To avoid using full paths every time I use the iTextSharp class, I also added several using statements:

 

 

Using iTextSharp. text;

 

Using iTextSharp.text.pdf;

 

 

 

 

In addition, you also need to reference the System. IO naming control. Because you need to create, open, and close files, some classes in this namespace are also essential.

 

 

 

The core object of iTextSharp is the Document object. You need to use the Document object instance to operate the PDF file in memory. Therefore, you must first instantiate a Document Object:

 

Var doc1 = new Document ();

In the memory, the read-only Code sets a documentobject. The large and small file size is a4(that is, 210mm x 297mm, or 8.26 feet x 11.69 feet bytes. writable writer class to implement this function:

 

// Use a variable to let my code fit into ss the page...

 

String path = Server. MapPath ("PDFs ");

 

Using writer. GetInstance (doc1, new FileStream (path + "/Doc1.pdf", FileMode. Create ));

Now you can perform operations on the document. First open the document, write a content section in it, and then close the document:

 

Doc1.Open ();

 

Doc1.Add (new Paragraph ("My first PDF "));

 

Doc1.Close ();

After refreshing the slogs folder, you will find a new file, doc1.pdf, and open the file. The added section has been successfully displayed.

 

Most of the time, you do not want to create a default size, default margin PDF file through the default settings, so iTextSharp allows you to customize these settings, so the Document Object also provides two other constructors:

 

Public Document (iTextSharp. text. Rectangle pageSize );

 

Public Document (iTextSharp. text. Rectangle pageSize, float );

The first constructor can be used as follows:

 

Var doc = new Document (PageSize. A5 );

The PageSize class contains a series of Rectangle objects that represent the size of most paper, from A0 to A10, B0 to B10, legal, ledger, envelope, postcard, and newspaper clipping, if the paper size in the PageSize class cannot meet your needs, you can customize a Rectangle object and set the value as a parameter to the Document constructor:

 

Var doc = new Document (new Rectangle (100f, 300f ));

 

Using writer. GetInstance (doc, new FileStream (path + "/Doc2.pdf", FileMode. Create ));

 

Doc. Open ();

 

Doc. Add (new Paragraph ("This is a custom size "));

 

Doc. Close ();

 

 

In the above Code, the created PDF document is 100 pixels wide and 300 pixels long. Because it is 72 pixels/foot, this document is not large, actually 1.39 feet x 4.17 feet ().

 

The second constructor uses Rectangle and four float-type numbers as parameters to allow you to customize the page margin through float-type variables. Similarly, the unit is pixel, and the default half-foot pixel is 36 pixels.

 

If you use the constructor of the PageSize class or custom Rectangle, you can also set the background color for the document. This setting can be based on the RGB color value or CMYK value. If your generated PDF document will be printed on a professional Flat Press, you must set it through CMYK. but for most digital printers, RGB is more acceptable. Of course, if your PDF is used for WEB, RGB is preferred and the background color of the document is set, set through the BackgroundColorproperty of the Rectangle object:

 

R. BackgroundColor = new CMYKColor (25, 90, 25, 0 );

 

R. BackgroundColor = new Color (191, 64,124 );

 

 

 

The above two lines of code will set the background color of the document to Charming pink...

This article briefly introduces iTextSharp, which is also the entry point for you to learn iTextSharp. Subsequent articles will detail a series of functions of this agile component.

Address: http://www.mikesdotnetting.com/Article/80/Create-PDFs-in-ASP.NET-getting-started-with-iTextSharp

 

Translated by CareySon

 

This article is translated because PDF operations are recently used in the project. However. net, this component documentation is relatively small, even if there is some information, it is not systematic, so I found this series of articles, in the process of translation, it is also for my own learning and improvement. translation in subsequent articles ....

 

<Script> </script>

Related Article

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.