C#生成PDF文件流
本文實(shí)例為大家分享了C#生成PDF文件流的具體代碼,供大家參考,具體內(nèi)容如下
1、設(shè)置字體
static BaseFont FontBase = BaseFont.CreateFont("C:\\WINDOWS\\FONTS\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    static iTextSharp.text.Font bodyFont = new iTextSharp.text.Font(FontBase, 12);
    static iTextSharp.text.Font titleFont = new iTextSharp.text.Font(FontBase, 18);
    static iTextSharp.text.Font paragraphFont = new iTextSharp.text.Font(FontBase, 15);
    static iTextSharp.text.Font linkFont = new iTextSharp.text.Font(FontBase, 12, Font.UNDERLINE, BaseColor.BLUE);
2.生成PDF文件流返回byte數(shù)組
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;
      PdfWriter writer = PdfWriter.GetInstance(document, file);
      writer.CloseStream = false;
      writer.Open();
      PdfContentByte cb = writer.DirectContent;
      document.Open();
      //標(biāo)題
      Paragraph title = new Paragraph(new Chunk("標(biāo)題", titleFont));
      title.Alignment = Element.ALIGN_CENTER;
      document.Add(title);
      //圖片
      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);
      //文獻(xiàn)出處
      Paragraph p2 = new Paragraph(new Chunk("出處", paragraphFont));
      p2.IndentationLeft = indentationLeft;
      document.Add(p2);
      InitData(list);//初始化業(yè)務(wù)數(shù)據(jù)
      CreateSteps(list, document, list.FirstOrDefault(it => it.PID == 0));//添加業(yè)務(wù)數(shù)據(jù)
      ////添加印章
      //iTextSharp.text.Image whyz = iTextSharp.text.Image.GetInstance(whyzPath);
      //whyz.ScalePercent(50);
      //whyz.PaddingTop = 100;
      //whyz.Alignment = Element.ALIGN_RIGHT;
      //document.Add(whyz);
      //添加日期
      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, "水印內(nèi)容", docWidth, docHeight);//添加水印,見另外一篇博客
      newfile.Position = 0;//重置流指針位置
      byte[] bytes = new byte[newfile.Length];
      newfile.Read(bytes, 0, bytes.Length);
      return bytes;
    }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 - 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
 - 01-10C#實(shí)現(xiàn)多線程寫入同一個(gè)文件的方法
 - 01-10C#編程獲取資源文件中圖片的方法
 - 01-10C#實(shí)現(xiàn)讀取被進(jìn)程占用的文件實(shí)現(xiàn)方法
 - 01-10C#刪除只讀文件或文件夾(解決File.Delete無法刪除文件)
 - 01-10C# readnodefile()不能讀取帶有文件名為漢字的osg文件解決方法
 - 01-10C#路徑,文件,目錄及IO常見操作匯總
 - 01-10C#獲取動(dòng)態(tài)生成的CheckBox值
 - 01-10winform實(shí)現(xiàn)拖動(dòng)文件到窗體上的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 - 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 


