C # IText generates PDFs with text, images, tables, text styles, alignment, header and footer, etc.,

Source: Internet
Author: User

#region Download Brochure PDF
protected void Lbtndownpdf_click (object sender, EventArgs e)
{
int pid = Converthelper.getinteger (request["pid"]);
PDS = P.getmodel (PID);
The first method of #region
DataTable datatable = new DataTable (dt. ToString ());
Try
{
String Url1 = "pdfbook\\" + getnewname () + ". pdf";
Create a Document object and set his initialization size
Rectangle pSize = new Rectangle (144, 90);
Background color of the document
Psize.backgroundcolor = Color.White;
Common page size pagesize.a4;
Document document = new document (PAGESIZE.A4);
Pdfwriter.getinstance (document, New FileStream (Server.MapPath (URL1), FileMode.Create));
Basefont Bfchinese = Basefont.createfont ("c:\\windows\\fonts\\simsun.ttc,1", Basefont.identity_h, BaseFont.NOT_ EMBEDDED);
Font Fontchinese = new Font (Bfchinese, N, Font.normal, new Color (0, 0, 0));
Font Headerchinese = new Font (Bfchinese, Font.Bold, New Color (0, 0, 0));
Font Beizhufontchinese = new Font (Bfchinese, ten, Font.Italic, new Color (255, 0, 0));
#region Headers and footers
/**
* HeaderFooter 2nd parameter is not false when the page number is printed
* The header and footer can also be added to the picture, not only the text
*/
HeaderFooter Header = new HeaderFooter ("All rights reserved for Shaanxi Holy Palace Jewellery", Beizhufontchinese), false);

Set whether there are borders, etc.
Header.setborder (Rectangle.no_border);
Header. Border = Rectangle.bottom_border;
Header. Alignment = 1;
Header. BorderColor = color.red;
Document. Header = header;

HeaderFooter footer = new HeaderFooter (New Phrase ("-", Beizhufontchinese), New Phrase ("-", Beizhufontchinese));
/**
* 0 is on the left
* 1 is centered
* 2 is Right
*/
Footer. Alignment = 1;
Footer. BorderColor = color.red;
Footer. Border = Rectangle.bottom_border;
Document. Footer = Footer;

/**
* The header and footer settings must be set before open.
*/
#endregion
Document. Open ();

Output text by a set font
String contents = Replacehtmlcode (PDS. ProductName);
String details = Replacehtmlcode (PDS. Productdetail);
Paragraph heading = new Paragraph (contents, Headerchinese);
Heading. Spacingafter = 18f;
Heading. Alignment = 1;
Heading. Indentationleft = 220f;
Document. ADD (heading);

Multicolumntext columns = new Multicolumntext ();

Columns. Addsimplecolumn (36f, 336f);

Columns. Addsimplecolumn (360f, document. pagesize.width-36f);

Paragraph detail = new Paragraph (details, Fontchinese);
Detail. Spacingafter = 9f;
Detail. Alignment = element.align_justified;
Detail. Indentationleft = 12f;//The full text is indented
//document. ADD (detail);
//Output picture to PDF file
string Url = "... /uploadpic\\ "+ PDS. Picture;//
iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance ( Server.MapPath (URL));
Jpeg01. Indentationleft = 150f;
//document. ADD (JPEG01);
Columns. AddElement (detail);
Columns. AddElement (JPEG01);
Columns. AddElement (detail);
Columns. AddElement (detail);
Columns. AddElement (detail);
//columns. AddElement (end);
Document. ADD (columns);
//paragraph end = new Paragraph ("Shaanxi Holy Palace jewellery All rights reserved", Beizhufontchinese);
//end. Indentationleft = 200f;
//document. ADD (end);
Document. Close ();

#region Automatic Download
Save the physical path to the file
String FullPath = HttpContext.Current.Server.MapPath (URL1);
Initializes an instance of the FileInfo class as a wrapper for the file path
FileInfo FI = new FileInfo (FullPath);
Determine if a file exists
if (FI. Exists)
{
Save the file to your computer
Response.Clear ();
Response.AddHeader ("Content-disposition", "attachment;filename=" + Server.URLEncode (FI. Name));
Response.AddHeader ("Content-length", FI. Length.tostring ());
Response.ContentType = "Application/octet-stream";
Response.Filter.Close ();
Response.WriteFile (FI. FullName);
Response.End ();
}
#endregion
}
catch (Documentexception de)
{
Response.Write (DE. ToString ());
}
#endregion


}
#endregion

C # IText generates PDFs with text, images, tables, text styles, alignment, header and footer, etc.,

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.