zoukankan      html  css  js  c++  java
  • iTextSharp操作pdf之pdfCreater

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System.IO;
    using System.Diagnostics;
    namespace ConsoleApplication1
    {
        class pdfCreater
        {
            ///  create pdf
            /// </summary>
            public static void CreatePdf()
            {
                iTextSharp.text.Document doc1 = new Document(PageSize.A4);
                iTextSharp.text.Document doc2 = new Document(PageSize.A4);
    
                try
                {
                    PdfWriter.GetInstance(doc1, new FileStream(@"D:	est1.pdf", FileMode.Create));
                    PdfWriter.GetInstance(doc2, new FileStream(@"D:	est2.pdf", FileMode.Create));
    
                    #region 设置PDF的头信息,一些属性设置,在Document.Open 之前完成
                    doc1.AddAuthor("cxa");
                    doc1.AddCreationDate();
                    doc1.AddCreator("cxa");
                    doc1.AddSubject("使用Do Net 创建了一个pdf1");
                    doc1.AddTitle("这是个标题");
                    doc1.AddKeywords("什么是keyword?不清楚反正这是个keyword");
    
                    doc2.AddAuthor("cxa");
                    doc2.AddCreationDate();
                    doc2.AddCreator("cxa");
                    doc2.AddSubject("使用Do Net 创建了一个pdf2");
                    doc2.AddTitle("这是个标题");
                    doc2.AddKeywords("什么是keyword?不清楚反正这是个keyword");
                    //自定义头
                    doc1.AddHeader("Expires", "0"); //0表示立即过期。我猜是立即生效
                    doc2.AddHeader("Expires", "0"); //0表示立即过期。我猜是立即生效
                    #endregion //打开document,此时打开啥呀没有
                    doc1.Open();
                    doc2.Open();
                    //载入字体
                    BaseFont bf = BaseFont.CreateFont("C:/Windows/Fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                    iTextSharp.text.Font font = new iTextSharp.text.Font(bf);
                    doc1.Add(new Paragraph("这是一个由C#创建的pdf文件,6661", font));
                    doc2.Add(new Paragraph("这是一个由C#创建的pdf文件,6662", font));
                    doc1.Close();
                    doc2.Close();
                    Process.Start(@"D:	est1.pdf");
                    Process.Start(@"D:	est2.pdf");
                }
                catch (DocumentException de)
                {
                    Console.WriteLine(de.Message); Console.ReadKey();
                }
                catch (IOException io)
                {
                    Console.WriteLine(io.Message); Console.ReadKey();
                }
            }
            public static void CreatePdf2()
            {
                
                iTextSharp.text.Document doc = new Document(PageSize.A4, 36, 72, 108, 180);
                try
                {
                    PdfWriter.GetInstance(doc, new FileStream(@"d:	est3.pdf", FileMode.Create));
                    doc.Open();
                    //创建表格
                    PdfPTable table = new PdfPTable(3);
    
                    PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
    
                    cell.Colspan = 3;
    
                    cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right  
                    cell.BorderColor = BaseColor.RED;
                    cell.BorderWidth = 2F; 
    
                    table.AddCell(cell);
                    table.AddCell("Col 1 Row 1");
    
                    table.AddCell("Col 2 Row 1");
    
                    table.AddCell("Col 3 Row 1");
    
                    table.AddCell("Col 1 Row 2");
    
                    table.AddCell("Col 2 Row 2");
    
                    table.AddCell("Col 3 Row 2");
    
                    doc.Add(table);
                    Image jpeg = Image.GetInstance(@"D:
    e.jpg");
                    doc.Add(jpeg);
                    doc.Add(new Paragraph("this is rege"));
                    doc.Close();
                    Process.Start(@"d:	est3.pdf");
                }
                catch (DocumentException de)
                {
                    Console.WriteLine(de.Message);
                }
                catch (IOException io)
                {
                    Console.WriteLine(io.Message);
                }
    
            }
        }
    }
    

      

  • 相关阅读:
    Xshell6提示“要继续使用此程序,您必须应用最新的更新或使用新版本”的解决办法
    设置UIImage的边框和圆角大小以及颜色
    CATransition使用
    [转]C/C++中extern关键字详解
    New ipad与ipad2有何不同
    使用Xcode收藏自己常用的代码模板
    objectivec 关于 self 的用法总结
    查找 EXC_BAD_ACCESS 问题根源的方法
    结构体的对齐方式
    添加three20模板的方法
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7941698.html
Copyright © 2011-2022 走看看