1.
Using Itextsharp.text;
Using ITextSharp.text.pdf;
2. Set the page size
ITextSharp.text.Rectangle pageSize = new ITextSharp.text.Rectangle (610f, 760f);
Set Background color
Pagesize.backgroundcolor = new Basecolor (100,100,100);
3. Create a document:
Document document = new document (pageSize);
For some common dimensions such as: A4, A5\A6 can use the PageSize class, if it is a landscape page can call its rotate method. eg
PageSize.A4.Rotate ().
Set Margion:
Document doc = new document (PageSize.A4.Rotate (), 5f, 5f, 5f, 5f);
4. Create writer
PDFWriter pw = pdfwriter.getinstance (doc, New FileStream (@ "... \my.pdf", filemode.openorcreate,fileaccess.readwrite)) ;
5. Set up the relevant information:
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
#region set the header information of the PDF, some property settings, completed before Document.open
Doc. Addauthor ("Still Floor decoration");
Doc. Addcreationdate ();
Doc. Addcreator ("Addcreator");
Doc. Addsubject ("Addsubject");
Doc. AddTitle ("title");
Doc. Addkeywords ("111");
Custom headers
Doc. AddHeader ("Expires", "0");
#endregion
Doc. Open ();
Doc. NewPage ();
6. Insert text:
Paragraph ph = new Paragraph ("PPPPPPPZZZZPPPPPPP" + "", font);
Ph. Alignment = ITextSharp.text.Rectangle.ALIGN_CENTER;
Doc. ADD (ph);
7. Insert Picture
ITextSharp.text.Image Image =
ITextSharp.text.Image.GetInstance (@ "... \20140527030942838.png");
Image. Alignment = ITextSharp.text.Image.MIDDLE_ALIGN;
Image. SetAbsolutePosition (0f, 40f);
Image. Scaletofit (610f, 760f);
Doc. ADD (image);
8. Insert the table:
iTextSharp.text.pdf.PdfPTable table = new iTextSharp.text.pdf.PdfPTable (2); There are 2 columns in the table
ITextSharp.text.pdf.PdfPCell cell = new ITextSharp.text.pdf.PdfPCell (); Create cells
Cell. AddElement (New Phrase ("SFDSDF"));
Cell. Colspan = 2;
Table. Addcell (cell);
Cell. Horizontalalignment=1; Add data to a cell
Table. Setwidths (New int[]{200, 100});
Table. Addcell ("1"); Add cells to a table
Table. Addcell ("2"); Add cells to a table
Table. Addcell ("1"); Add cells to a table
Table. Addcell ("2"); Add cells to a table
Table. Addcell ("1"); Add cells to a table
Table. Addcell ("2"); Add cells to a table
Table. HorizontalAlignment = 1;
Doc. ADD (table);
Doc. Close ();
============asp.net Direct Download PDF file case:
PDF Page Size
ITextSharp.text.Rectangle pageSize = new ITextSharp.text.Rectangle (610f, 360f);
Document doc = new document (PageSize, 0f, 0f, 0f, 0f);
Set boundaries
MemoryStream Memory = new MemoryStream ();
Try
{
Pdfwriter.getinstance (Doc, Memory);
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
#region set the header information of the PDF, some property settings, completed before Document.open
Doc. Addauthor ("Still Floor decoration");
Doc. Addcreationdate ();
Doc. Addcreator ("" + model. FirstOrDefault (). Slicreatetime + "");
Doc. Addsubject ("");
Doc. AddTitle ("" + model. FirstOrDefault (). Soltitle + "");
Doc. Addkeywords ("");
Custom headers
Doc. AddHeader ("Expires", "0");
#endregion
Doc. Open ();
foreach (var item in model)
{
Doc. NewPage ();
Doc. ADD (New Paragraph ("" + Item. Sliname?? "1111" + "", font));
ITextSharp.text.Image Image =
ITextSharp.text.Image.GetInstance (SFast.MyUrl.ToAb (item. Sliimagepath));
Image. Alignment = ITextSharp.text.Image.MIDDLE_ALIGN;
Image. SetAbsolutePosition (0f, 40f);
Image. Scaletofit (610f, 360f);
Doc. ADD (image);
}
Doc. Close ();
String strFileName = "" + model. FirstOrDefault (). Soltitle + "";
HttpContext.Current.Response.Clear ();
HttpContext.Current.Response.ClearHeaders ();
HttpContext.Current.Response.Write ("<meta http-equiv=content-type content=text/html;charset=utf-8>");
strFileName = Httputility.urlencode (strFileName + ". pdf", System.Text.Encoding.GetEncoding ("UTF-8"));
HttpContext.Current.Response.AddHeader ("Content-disposition", String. Format ("attachment; Filename={0} ", strFileName));
HttpContext.Current.Response.ContentType = "Application/octet-streem";
Response.OutputStream.Write (Memory.getbuffer (), 0, Memory.getbuffer (). Length);
Response.OutputStream.Flush ();
Response.OutputStream.Close ();
Response.Flush ();
Response.End ();
}
catch (Documentexception de) {Console.WriteLine ("Picture Path not Found"); Console.readkey (); }
}
Itextsharp Generate PDF Document case