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

      

  • 相关阅读:
    慕课网-安卓工程师初养成-1-4 练习题
    慕课网-安卓工程师初养成-1-3 使用记事本编写Java程序
    慕课网-安卓工程师初养成-1-2 开发环境搭建
    慕课网-安卓工程师初养成-1-1 Java简介
    安卓开发菜鸟初学
    Java编程——输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
    JSP三种常用注释
    语录——(摘自应届毕业生网)
    Servlet基础知识
    建议网站购物——建立一个简易购物网站,包括登录页面、商品选择页面和结账页面。
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7941698.html
Copyright © 2011-2022 走看看