C # generate a PDF file stream,

Source: Internet
Author: User

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 ;}

 

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.