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 ();}
}