[Translation] creating PDF-iTextSharp in ASP. NET,

Source: Internet
Author: User

[Translation] creating PDF-iTextSharp in ASP. NET,

. 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 isDocumentObject, You need to operate the PDF file in the memory through the Document object instance, So first you need to instantiate a Document Object:

var doc1 = new Document();

The above Code uses the default settings in the memory to instantiate a Document object. The default Document size is A4 (that is, 210mm x 297mm, or 8.26 feet x 11.69 feet), and the margins are half feet by default. the next step is to save the Document Object in memory to the hard disk and useItextsharp.textmetadata. Partition writerClass to implement this function:

//use a variable to let my code fit across the page... string path = Server.MapPath("PDFs");PdfWriter.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, float, float, float);

The first constructor can be used as follows:

var doc = new Document(PageSize.A5);

PageSizeClass contains a seriesRectangleObjects represent the size of most paper, from A0 to A10, B0 to B10, legal, ledger, envelope, postcard, newspaper clipping, etc. IfPageSizeThe paper size in the class cannot meet your needs. You can customizeRectangleObject, which is passed as a parameter after being setDocumentConstructor:

var doc = new Document(new Rectangle(100f, 300f)); PdfWriter.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. however, 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 through the Rectangle object.BackgroundColorpropertySet:

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.



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.