Use itextsharp to create a PDF file

Source: Internet
Author: User

I thought that when I tried to create a PDF component, I found that there were very few resources on the Internet. Most of the discussions were about itextsharp. Then download and try again.

Start with several useful resources:

This is a good Chinese Blog with detailed information about itextsharp.

This is a useful article in the IBM knowledge base.ArticleBut it is implemented in Java.CodeIt should not be unfamiliar.With this article, you can make exquisite reports..

I only use itextsharp to create a report. The current project is a C/S project, but we need to modify and extend it to the B/S architecture (this is what the customer requires), so I don't want to generate a PDF file directly on the hard disk, but instead generate a stream. Of course, it must be a memorystream, in this way, you will also have the opportunity to directly output data to the browser. The code below:

Private memorystream generatew.stream () {basefont = basefont. createfont (font_path, basefont. identity_h, basefont. embedded); document PDF = new document (pagesize. a4, left, right, top, bottom); memorystream stream = new memorystream (); writable writer = writable writer. getinstance (PDF, stream); PDF. open (); PDF. add (new paragraph (string. format ("Project: {0}", cover. projectname ?? ""), New pdffont (basefont, font_size_1); PDF. Close (); Return stream ;}

It should be noted that it is difficult to generate a PDF of Chinese characters because I generate a Chinese character directly, so I don't know how to solve it.

During generation, you need to create a basefont object. The first parameter of the createfont method is fontname, but it may be different from the first one ", a font file name, the path of a traditional font file, is defined in my Code as follows:

 
Private const string font_path = @ "fonts \ simsun. TTC, 1 ";

Fonts is a sub-folder under the DEBUG directory, and simsun. TTC is the file where "" is located. ", 1" is my guess that there may be two fonts in this file? Fetch 2nd? It's just speculation. I don't want to go into it. I am too lazy now. it's good to use it most of the time.

Nothing else has any technical content, so you can see it at a Glance. It is worth mentioning that the document instance, that is, the PDF must be closed once, in this way, data can be flushed into memorystream. Otherwise, memorystream does not contain any data. Other headers, footers, paragraphs, tables, images, and other files are no longer difficult than output files. just refer to the IBM Knowledge Base Article.

The effect is as follows:

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.