Csharp iTextSharp create pdf file
/* //http://sourceforge.net/projects/itextsharp/ * http://stackoverflow.com/questions/8713963/itextsharp-pdf-resize-document-to-accomodate-a-large-image * http://www.inliteresearch.com/barcode-recognition/ * http://footheory.com/blogs/donnfelker/archive/2008/05/11/using-itextsharp-to-watermark-write-text-to-existing-pdf-s.aspx * http://redaction.codeplex.com/ * http://www.koders.com/csharp/fidB1D68577734B615DBC366542FF8BA8A977F7F54D.aspx?s=button */ 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. //http://wrb.home.xs4all.nl/Articles_2010/Article_WPFDataMatrix_P2_01.htm // --------------------------------------------------------------- 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(); } } } }
//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)); //毫米轉英寸
流显示出来
try { String OUT_FOLDER =Server.MapPath("/pdf/"); string RESOURCES = Server.MapPath("/Font/"); String filename = OUT_FOLDER + "geovindu.pdf"; //1 流读取 MemoryStream m = new MemoryStream(); //2 保存PDF文件 // FileStream fos = new FileStream(filename, FileMode.Create); Document document = new Document(); //必须加上,否则中文显示不上 Response.Charset = "utf-8"; Response.ContentType = "application/pdf"; //1 流写入 PdfWriter writer = PdfWriter.GetInstance(document, m); //2 写入文件 // PdfWriter writer = PdfWriter.GetInstance(document, fos); writer.CreateXmpMetadata(); document.Open(); Font font = FontFactory.GetFont(RESOURCES + "钟齐翰墨毛笔.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED, 18);//BaseFont.WINANSI, BaseFont.EMBEDDED document.Add(new Paragraph("Hello World,中国人民站起来了--涂聚文(Geovin Du)曰", font)); iTextSharp.text.pdf.PdfPTable table = new PdfPTable(2); table.SpacingBefore = 18f; //与上一个元素的距离 table.TotalWidth = 616f; int[] cellwidht = { 180, 180 }; table.SetWidths(cellwidht); PdfPCell cell ; cell = new PdfPCell(new Phrase("天下为公",font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("涂聚文", font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("公正,公平,公开", font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("Geovin Du", font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("深圳市", font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); cell = new PdfPCell(new Phrase("罗湖区", font)); cell.Top = 5f; cell.HorizontalAlignment = Element.ALIGN_CENTER; table.AddCell(cell); document.Add(table); document.Close(); //PdfReader reader = new PdfReader(filename); //bool exceptionThrown = false; //try //{ // FileStream stamperFileStream = new FileStream(OUT_FOLDER + "geovindu1.pdf", FileMode.Create); // PdfAStamper stamper = new PdfAStamper(reader, stamperFileStream, PdfAConformanceLevel.PDF_A_1A); // stamper.Close(); // stamperFileStream.Close(); //} //catch (PdfAConformanceException) //{ // exceptionThrown = true; // fos.Close(); //} //reader.Close(); // fos.Close(); writer.Close(); document = null; writer = null; // Response.Write("Ok!"); //BinaryWriter fwriter = new BinaryWriter(fos); //byte[] bytes = new byte[fos.Length]; //fos.Read(bytes, 0, bytes.Length); //fos.Close(); //MemoryStream ms = new MemoryStream(bytes); //ms.Position = 0; //Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); //1在线查看 Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length); Response.OutputStream.Flush(); Response.OutputStream.Close(); } catch (Exception ex) { Response.Write(ex.Message.ToString()); }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)