C # using the iTextSharp component to create a PDF

Source: Internet
Author: User
Tags dot net

Copy the iTextSharp. dll file to the bin directory of the project and add references to the project:

Then add reference in the background code:

Copy codeThe Code is as follows: using iTextSharp. text;
Using iTextSharp.text.pdf;
Using System. IO;
Using System. Diagnostics;

// Create a PDF file
Private void CreatePdf ()
{
// Define a Document and set the page size to A4 and vertical
ITextSharp. text. Document doc = new Document (PageSize. A4 );
Try
{
// Write instance
Using writer. GetInstance (doc, new FileStream ("D :\\ Hello.pdf", FileMode. Create ));
# Region: Set the header information of the PDF file. Some attribute settings are completed before Document. Open.
Doc. AddAuthor ("author fantasies Zerow ");
Doc. AddCreationDate ();
Doc. AddCreator ("creator fantasies Zerow ");
Doc. AddSubject ("example of using the itextsharp class library to create a PDF file in Dot Net ");
Doc. AddTitle ("This PDF was created by fantasy Zerow, ");
Doc. AddKeywords ("ASP. NET, PDF, iTextSharp, fantasy Zerow ");
// Custom Header
Doc. AddHeader ("Expires", "0 ");
# Endregion // Open document
Doc. Open ();
// Load the font
BaseFont. AddToResourceSearch ("iTextAsian. dll ");
BaseFont. AddToResourceSearch ("iTextAsianCmaps. dll ");
// "UniGB-UCS2-H" "UniGB-UCS2-V" is simplified Chinese, indicating horizontal and // vertical words // "STSong-Light" is the font name
BaseFont baseFT = BaseFont. CreateFont (@ "c: \ windows \ fonts \ SIMHEI. TTF", BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED );
ITextSharp. text. Font font = new iTextSharp. text. Font (baseFT); // write a Paragraph, Paragraph
Doc. Add (new Paragraph ("Hello, PDF! ", Font ));
// Close document
Doc. Close ();
// Open the PDF file to check the effect.
Process. Start ("D :\\ Hello.pdf ");
}
Catch (incluentexception de) {Console. WriteLine (de. Message); Console. ReadKey ();}
Catch (IOException io) {Console. WriteLine (io. Message); Console. ReadKey ();}
}

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.