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

      

  • 相关阅读:
    Entity Framework Core 2.0 新特性
    asp.net core部署时自定义监听端口,提高部署的灵活性
    asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法
    使用xshell连接服务器,数字键盘无法使用解决办法
    使用Jexus 5.8.2在Centos下部署运行Asp.net core
    【DevOps】DevOps成功的八大炫酷工具
    【Network】Calico, Flannel, Weave and Docker Overlay Network 各种网络模型之间的区别
    【Network】UDP 大包怎么发? MTU怎么设置?
    【Network】高性能 UDP 应该怎么做?
    【Network】golang 容器项目 flannel/UDP相关资料
  • 原文地址:https://www.cnblogs.com/c-x-a/p/7940292.html
Copyright © 2020-2023  润新知