C # generate a PDF file stream,
1. Set Font
1 static BaseFont FontBase = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);2 static iTextSharp.text.Font bodyFont = new iTextSharp.text.Font(FontBase, 12);3 static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, 18);4 static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, 15);5 static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase, 12, Font.UNDERLINE, BaseColor.BLUE);
2. Generate a PDF file stream and return the byte array
Public byte [] DocCreate (System. drawing. image image, List <TreeNodes> list) {MemoryStream file = new MemoryStream (); string fileName = string. empty; Rectangle page = PageSize. a4; float y = page. height; Document document = new Document (page, 15, 15, 30, 30); float docWidth = page. width-15*2; float docHeight = page. height-document. bottomMargin-document. topMargin; writable writer = writable writer. getInstance (document, file); writer. closeStream = false; writer. open (); Response contentbyte cb = writer. directContent; document. open (); // title Paragraph title = new Paragraph (new Chunk ("title", titleFont); title. alignment = Element. ALIGN_CENTER; document. add (title); // picture iTextSharp. text. image img = iTextSharp. text. image. getInstance (image, ImageFormat. png); float widthSzie = (page. width-30)/img. width; if (widthSzie <1) {img. scalePercent (widthSzie * 100);} document. add (img); // source Paragraph p2 = new Paragraph (new Chunk ("Source", paragraphFont); p2.IndentationLeft = indentationLeft; document. add (p2); InitData (list); // initialize business data CreateSteps (list, document, list. firstOrDefault (it => it. PID = 0); // Add business data // Add a seal // iTextSharp. text. image whyz = iTextSharp. text. image. getInstance (whyzPath); // whyz. scalePercent (50); // whyz. paddingTop = 100; // whyz. alignment = Element. ALIGN_RIGHT; // document. add (whyz); // Add date Paragraph createtime = new Paragraph (new Chunk (DateTime. now. toLongDateString (). toString (), bodyFont); createtime. alignment = Element. ALIGN_RIGHT; // createtime. spacingBefore =-80; createtime. paddingTop = 200; document. add (createtime); document. close (); file. position = 0; MemoryStream newfile = SetWaterMark (file, "watermark content", docWidth, docHeight); // Add a watermark. For more information, see newfile. position = 0; // reset the stream pointer Position byte [] bytes = new byte [newfile. length]; newfile. read (bytes, 0, bytes. length); return bytes ;}