zoukankan      html  css  js  c++  java
  • c#操作pdf文件系列之创建文件

    1.我使用的工具是vs2013,引用的第三方程序集itextpdf 具体安装方法,可以通过nuget搜索iTextSharp然后进行安装。

    2具体代码如下 创建两个不同pdf文件,每个地方什么意思代码中已经详细加了注释。

    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 pdfWriter
    {
        class Program
        {
            static void Main(string[] args)
            {
                CreatePdf();
            }
            /// <summary>
            ///  create pdf
            /// </summary>
            private 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();
                }
            }
        }
    }
    

      

  • 相关阅读:
    ie和firefox浏览器对透明flash的兼容性问题
    CSS样式命名规则
    js中用script 嵌套script块
    visual stdio2005常用快捷键
    SQL数据库设计的命名规范
    js实现excel数据导入
    程序员每天该做的事
    用户体验公式
    sql编程命名规范
    五种常见的ASP.NET安全缺陷
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7940292.html
Copyright © 2011-2022 走看看