Asp.net PDF generation method, asp.net pdf generation

Source: Internet
Author: User
Tags asp net

Asp.net PDF generation method, asp.net pdf generation

Today, I saw someone posting an article on PFd generation. I have prepared to keep a copy of the article for future use;

First go to The itextsharp website to download controls (https://sourceforge.net/projects/itextsharp)

Reference the downloaded control to your project. The main bll file is the itextsharp. dll file.

1. Generate a PDF file based on DataTable

Add Itextsharp reference

Using iTextSharp;
Using iTextSharp. text;
Using iTextSharp.text.pdf;
Using System. IO;

Application code

/// <Summary> // convert the Data table to a PDF file // </summary> /// <param name = "Data"> Data table </param> // /<param name = "PDFFile"> full path of the target PDF file </param> // <param name = "FontPath"> path of the font </param> // <param name = "FontSize"> font size </param> // <returns> whether the call is successful </returns> public static bool ConvertDataTableToPDF (DataTable datatable, string PDFFilePath, string FontPath, float FontSize) {// initialize a target Document class, document Document = new effece Nt (); // call the PDF Writing Method stream // note FileMode-Create indicates that if the target file does not exist, it is created. if it already exists, it is overwritten. Specified writer = Specified writer. getInstance (document, new FileStream (PDFFilePath, FileMode. create); // open the target document Object document. open (); // create the font BaseFont baseFont = BaseFont. createFont (FontPath, BaseFont. IDENTITY_H, BaseFont. NOT_EMBEDDED); // create a Font font Font = new Font (baseFont, FontSize) based on the font path and Font size attributes ); // create a PDF table PdfPTable table = new PdfPTable (datatable. columns. count); // traverse the content of the original table for (int I = 0; I <datatable. rows. count; I ++) {for (int j = 0; j <datatable. columns. count; j ++) {table. addCell (new Phrase (datatable. rows [I] [j]. toString (), font) ;}// Add the converted table data document to the target document. add (table); // close the target file document. close (); // Close the write stream writer. close (); return true ;} /// <summary> /// generate Dataset /// </summary> /// <returns> </returns> private DataSet GetSet () {DataSet ds = new DataSet (); string SQL = "Select * from T_AjaxXML"; string Config = ConfigurationManager. connectionStrings ["Config"]. connectionString; using (SqlConnection cnn = new SqlConnection (Config) {using (SqlCommand cmm = new SqlCommand (SQL, cnn) {SqlDataAdapter dapter = new SqlDataAdapter (cmm); dapter. fill (ds) ;}} return ds ;}call method: ConvertDataTableToPDF (GetSet (). tables [0], Server. mapPath ("~ /Tablevideos ")," C: \ Windows \ Fonts \ simsun. ttc, 1 ", 14 );

2. Generate a PDF file based on the text content

Private void CreateTxt (string txt, string filepath) {// create the Document Object document = new Document (); // instantiate the generated Document into writer. getInstance (document, new FileStream (filepath, FileMode. create); // open the document. open (); // Add the document content to the document. add (new Paragraph (txt); // close the document Object document. close ();}

3. Generate the header and footer (however, this method is not available in iTextSharp5.5. Please help me find one)

Private void createdocument header (string filepath, string headertxt, string footertxt) {// create a Document Object document Document = new Document (); // create a document and write it to the instance mongowriter. getInstance (document, new FileStream (filepath, FileMode. create); PdfPageEventHelper hh = new PdfPageEventHelper (); // Add the footer HeaderFooter footer = new HeaderFooter (new Phrase (footertxt), true); footer. border = Rectangle. NO_BORDER; document. footer = footer; // open the document content object document. open (); // Add the header HeaderFooter header = new HeaderFooter (new Phrase (headertxt), false); document. header = header; // design the document on each page. add (new Paragraph ("This is First Page"); // Add a new Page document. newPage (); // Add the text document to page 2nd. add (new Paragraph ("This is Second Page"); // reset the Page quantity document. resetPageCount (); // closes the document Object document. close ();}

  

 

 


Asp net pdf

You can use regular expressions to remove html code, but it may not be in html format. If you want html format, you need to parse it in detail.

How to generate a pdf file in the aspnet Environment

A third-party component is required to generate a PDF file!
For example, ABCpdf. NET X64 [dynamically generate the. NET Component of PDF] v7.0.1.2 registered version

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.