现在的位置: 首页 > 综合 > 正文

使用C#创建PDF文档

2018年05月14日 ⁄ 综合 ⁄ 共 1261字 ⁄ 字号 评论关闭

using System;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;

namespace CreatePDFDemo

{

  class Progarm

  {

    static void Main(string[] args)

    {

      Document doc = new Document();

      /*设置页面

      Rectangle pageSize = new Rectangle(320,240);

      pageSize.BackgroundColor = new Color(0xFF,0xFF,0xFF);

      Document doc = new Document(pageSize);

      Document doc = new Document(PageSize.A5,36,72,108,180);

      */

      try

      {

        PdfWriter.GetInstance(doc,new FileStream(@"d:/create.pdf",FileMode.Create));

        /*添加文档信息

        doc.AddTitle("PDFInfo");

        doc.AddSubject("Demo of PDFInfo");

         doc.AddKeywords("Info,PDF,Demo");

         doc.AddCreator("SetpdfInfoDemo");

          doc.AddAuthor("Z");

        */

        doc.Opent();

        /*添加部分内容

        Pagegraph paragraph = new Paragraph();

        paragraph.Alignment = Element.ALIGN_JUSTIFIED;

        for(int i=0; i<20; i++)

        {

          paragraph.Add("pdf,pdf,pdf,pdf,pdf,pdf,pdf");

        }

        doc.Add(paragraph);

        */

        doc.Add(new Paragraph("PDF1");

        doc.Add(new Paragraph("PDF2");

        doc.Add(new Paragraph("PDF3");

        doc.Add(new Paragraph("PDF4");

      }

      catch(DocumentException de)

      {

        string e1 = de.Message;

      }

      catch(IOException ioe)

      {

        string e2 = ioe.Message;

      }

      doc.Close();

    }

  }

抱歉!评论已关闭.