Csharp iTextSharp create pdf file

/*
  */
 
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
//using System.Drawing;
using  System.Linq;
using  System.Text;
using  System.Windows.Forms;
using  System.IO;
using  iTextSharp.text;
using  iTextSharp.text.pdf;
using  iTextSharp.text.xml;
using  iTextSharp.text.exceptions;
using  System.Drawing.Imaging;
using  iTextSharp;
using  DataMatrix.net;
 
namespace  WindowsWordDemo
{
     public  partial  class  FormPdf : Form
     {
 
         private  static  string  path = Directory.GetCurrentDirectory();
         string  imagePath=path+ "\\geovindu.png" ;
         string  strFileName = path + "\\geovindu.pdf" ;
         Document doc = new  Document(PageSize.A4.Rotate()); //LETTER.Rotate()
 
         // ---------------------------------------------------------------
         // Date      180310
         // Purpose   Text to DataMatrix image.
         // Entry     sFileName - Name of the barcode file (PNG, + path).
         //           sValue - The string to save as barcode.
         //           iBitSize - The size of one data bit (pixels).
         //           iBorSize - The size of the quiet zone (pixels).
         // Return    The bitmap, null if not successful.
         // Comments  Supports .bmp, .gif, .ico, .jpg, .png, .wdp, .tiff.
         //           See source, project DataMatrixTest, Program.cs.
         // ---------------------------------------------------------------
         private  System.Drawing.Bitmap EncodeText( string  sFileName, string  sValue,
                                                   int  iBitSize, int  iBorSize)
         {
             DmtxImageEncoder encoder = new  DmtxImageEncoder();
 
             DmtxImageEncoderOptions options = new  DmtxImageEncoderOptions();
             options.ModuleSize = iBitSize;
             options.MarginSize = iBorSize;
             options.BackColor = System.Drawing.Color.White;
             options.ForeColor = System.Drawing.Color.Black;
 
             // With options (GDI+).
             System.Drawing.Bitmap oB = encoder.EncodeImage(sValue, options);
 
             // Without options.
             // System.Drawing.Bitmap encodedBitmap = encoder.EncodeImage(sValue);
 
             try
             {
                 oB.Save(sFileName, System.Drawing.Imaging.ImageFormat.Png);
             }
             catch
             {
                 oB = null ;
             }
             return  oB;
         }
         /// <summary>
         ///
         /// </summary>
         public  FormPdf()
         {
             InitializeComponent();
             
         }
         /// <summary>
         ///
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  FormPdf_Load( object  sender, EventArgs e)
         {
             //var doc1 = new Document(PageSize.A4, 50, 50, 80, 50);
             //MemoryStream Memory = new MemoryStream();
             //PdfWriter pdfWrite = PdfWriter.GetInstance(doc1, Memory);
             //20120603 Geovin Du
             //塗聚文
             
             try
             {
                 //條形碼碼圖片
                 EncodeText(imagePath, "36242520120603" , 100, 100);
 
 
                 PdfWriter.GetInstance(doc, new  FileStream(strFileName, FileMode.Create));
                 doc.Open();
                 doc.Add( new  Paragraph());
                 iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
                 if  (img.Height > img.Width)
                 {
                     //Maximum height is 800 pixels.
                     float  percentage = 0.0f;
                     percentage = 700 / img.Height;
                     img.ScalePercent(percentage * 100);
                 }
                 else
                 {
                     //Maximum width is 600 pixels.
                     float  percentage = 0.0f;
                     percentage = 540 / img.Width;
                     img.ScalePercent(percentage * 100);
                 }
                 img.SetAbsolutePosition(10, 10);
                 img.Border = iTextSharp.text.Rectangle.BOX;
                 img.BorderColor = iTextSharp.text.Color.BLACK;
                 img.BorderWidth = 3f;
                 
                 doc.Add(img);
                 doc.NewPage();
                 BaseFont btfChinese=BaseFont.CreateFont( @"D:\WINDOWS\Fonts\繁篆书.ttf" , BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
                 Font ChFont= new  Font(btfChinese,12);
                 Font ChFont_blue = new  Font(btfChinese, 40, 3, iTextSharp.text.Color.BLUE);
                 Font ChFont_message = new  Font(btfChinese, 60, 5, iTextSharp.text.Color.RED);
                 doc.Add( new  Paragraph( "First Pdf File made by Salman using iText,涂斯博" , ChFont_blue));
                 doc.NewPage();
                 Chunk c = new  Chunk( "£捡垃圾,就是我的一生渴求. £并且喜欢这句『书中自有黄金屋,书中自有颜如玉,书中自有千钟黍,书中车马多簇簇』的感慨.『读书长见识,行路广阅历』,是一个「悟」字. $自然是我最想追求的「偶像」. ¥读者是,读之者,者之读.一沙一世界! ¥to be is to do举世皆清我独浊,众人皆醒我独醉website:http://www.dusystem.com , www.geovi.com,www.dupcit.com " ,ChFont);
                 Phrase p1 = new  Phrase(c);
                 doc.Add(p1);
                 Chunk c1 = new  Chunk( "近期心愿  有饭吃,有觉睡" , ChFont);
                 Chunk c2 = new  Chunk( "哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构(分析)整合学习, 情商(EQ)运筹(学)成功,信息流,物流,人力资源流,资本流的系统解决方案的开发与设计." , ChFont_blue);
                 Chunk c3 = new  Chunk( "why we only heard about haves and have-nots,why we didn't heard about doers and doer-nots.人生是一种心境,生活是一种艺术,成功是一种心态,幸福是一种感觉,竞争是一种建构,情感是一种整合.学习是一种成长.逻辑和美都关注封闭的事实,我们的生活则是发现的经验中度过的.....$经一番挫折,长一番识见.容一番横逆,增一番器度.省一番经营,多一分道义.学一分退让,讨一分便宜.去一分奢侈,少一分罪过.加一分体贴,知一分物情." , ChFont);
                 Phrase p2 = new  Phrase();
                 p2.Add(c1);
                 p2.Add(c2);
                 p2.Add(c3);
                 Paragraph pg = new  Paragraph(p2);
                 pg.SetAlignment( "Justify" );
                 pg.FirstLineIndent = 20f;
                 pg.SetLeading(0.0f, 2.0f);
                 doc.Add(pg);
                 doc.Close();
 
             }
             catch
             {
                 // add some code here incase you have an exception
             }
             finally
             {
                 //Free the instance of the created doc as well
                 doc.Close();
             }
 
         }
}
}

 

1
2
//A4紙:页面宽度 827* 1169 (英寸),210*297(毫米)
//自定義紙張大小 PaperSize   pkCustomSize   =   new   PaperSize( "custom   size",(int)(82.55*100/25.4+1),(int)(184.0*100/25.4+1)); //毫米轉英寸

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值