The method for generating PDF in asp.net, asp. netpdf

Source: Internet
Author: User

The method for generating PDF in asp.net, asp. netpdf

Recently to use asp.net 2.0 generate PDF, read the next book, check the information, found that there can be components to help, you can download itextsharp (https://sourceforge.net/projects/itextsharp)
Download and reference the control in the project. For example

1 able content to PDF
First, create a datatable to convert to pdf as follows:
Using iTextSharp;
Using iTextSharp. text;
Using iTextSharp.text.pdf;
Using System. IO;

/// <Summary>
/// Method of converting DataTable into a PDF file
/// </Summary>
Public class TableToPDF
{
Public TableToPDF ()
{
}
/// <Summary>
/// Convert the data table to a PDF document
/// </Summary>
/// <Param name = "Data"> Data table </param>
/// <Param name = "PDFFile"> full path of the target PDF file </param>
/// <Param name = "FontPath"> font path </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 Document ();
// 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.
Using writer = Using writer. GetInstance (document, new FileStream (PDFFilePath, FileMode. Create ));
// Open the target Document Object
Document. Open ();
// Create a font in the PDF document
BaseFont baseFont = BaseFont. CreateFont (
FontPath,
BaseFont. IDENTITY_H,
BaseFont. NOT_EMBEDDED );
// Create a font Based on the font path and font size attributes
Font font = new Font (baseFont, FontSize );
// Create a table in PDF format based on the data table content
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 converted table data to the target document
Document. Add (table );
// Close the target file
Document. Close ();
// Close the write stream
Writer. Close ();
Return true;
}
}


Then, you can call it in the Event code of the button to call the conversion.
/Save the target file under this project
// Simsun font
// Select 14 for the font size
// Mytb is the name of the Data able.
TableToPDF. convertDataTableToPDF (mytb, Server. mapPath (". ") + @" \ tabletables "," c: \ winnt \ FONTS \ simsun. ttc, 1 ", 14 );

2. Provide text content and generate a PDF file
For example, if you enter the text content and save path of the PDF file to be output, you can also output the PDF file.
/// <Param = "txt">: content of the text to be output </param>


Private void CreateTxt (string txt, string filepath)
{
// Create a Document Object
Document document = new Document ();
// Instantiate the generated document
Using writer. GetInstance (document, new FileStream (filepath, FileMode. Create ));
// Open the document
Document. Open ();
// Add text content to the document
Document. Add (new Paragraph (txt ));
// Close the Document Object
Document. Close ();
}

3. Add the header and footer
Private void createauthorization header (string filepath, string headertxt, string footertxt)
{
// Create a Document Object
Document document = new Document ();
// Create a document writing instance
Using writer. GetInstance (document, new FileStream (filepath, FileMode. Create ));

// Add a 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 a header
HeaderFooter header = new HeaderFooter (new Phrase (headertxt), false );
Document. Header = header;
// Design the content of each page
Document. Add (new Paragraph ("This is First Page "));
// Add a new page
Document. NewPage ();
// Add text to page 2nd
Document. Add (new Paragraph ("This is Second Page "));
// Reset the page quantity
Document. ResetPageCount ();
// Close the Document Object
Document. Close ();
}


ASPNET generates a PDF file on the page and found a lot on the Internet, but there is no good solution

Why do we need to generate a pdf? The web format and pdf format are completely different to each other.

In aspnet, data in the gridview cannot be exported to pdf files.

Export to excel. The code can be used.
Then use Acrobat to convert!
It is difficult to use only the code to generate a PDF file. I wrote two methods for you to call.
/// <Summary>
/// Import the data of the GridView to the PDF file
/// </Summary>
/// <Param name = "gdv"> the page's GridView </param>
/// <Param name = "fileName"> default pdf file name </param>
Public void GridViewDataToPdf (System. Web. UI. WebControls. GridView gdv, string fileName)
{
If (fileName. ToLower (). IndexOf (". pdf") =-1)
{
FileName = fileName + ". pdf ";
}

Export (gdv, fileName, "application/pdf-pdf"); // The format is changed to "application/ms-word ".
}

/// <Summary>
/// Import data
/// </Summary>
/// <Param name = "gdv"> </param>
/// <Param name = "fileName"> </param>
/// <Param name = "typeName"> </param>
Private void Export (System. Web. UI. WebControls. GridView gdv, string fileName, string typeName)
{
System. Web. HttpResponse httpResponse = _ InvokePage. Response;
HttpResponse. AppendHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. UrlEncode (fileName, System. Text. Encoding. UTF8 ));
Http ...... remaining full text>

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.