參考: http://www.cnblogs.com/LifelongLearning/archive/2011/03/30/1999652.html

//-------JK fixed ok----------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
//using System.Drawing; //  在此 iTextSharp 有自己的image 類別
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text; //要加入這個iTextSharp.dll 的參考
using iTextSharp.text.pdf;  //官網下載dll  http://sourceforge.net/projects/itextsharp/
using System.IO;

namespace forPDF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


   
        private void button1_Click(object sender, EventArgs e)
        {


            //=========================================
            //建立另外的pdf檔案  圖片處理


            //添加文本
            //创建文档对象
            Document document = new Document();
            //实例化生成的文档
            PdfWriter.GetInstance(document, new FileStream(@"c_image.pdf", FileMode.Create));
            //打开文档
            document.Open();
            //在文档中添加文本内容
            document.Add(new Paragraph("Hello World!"));

           // string filename = "Desert.jpg";
//            Image image = Image.GetInstance(@"D:\Desert.jpg");  //讀取圖片來源
            Image image = Image.GetInstance(@"Desert.jpg");  //讀取圖片來源

                image.Alignment = Image.ALIGN_LEFT;
                image.ScalePercent(30);//'縮小到30%才放的進版面
               document.Add(image);

                document.Add(new Paragraph("居中对齐:"));
               image.Alignment = Image.ALIGN_MIDDLE;
               image.ScaleToFit(100,100);//指定大小縮放 (還是會保持原比例)
               document.Add(image);
   
               document.Add(new Paragraph("适合页面对齐:"));
               image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
               image.ScalePercent(30);
               document.Add(image);
               document.NewPage();//'加新空白頁
           
            var p = new Paragraph("旋转一:");
               p.Add(Chunk.NEWLINE);
   
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
              image.ScalePercent(30);
               image.RotationDegrees = 30;
               p.Add(new Chunk(image, 50, -30)); //x向右為正   y向下為負
               document.Add(p);
   
             p = new Paragraph("旋转二:");
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
              p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
               p.Add(Chunk.NEWLINE);
              image.Alignment = Image.ALIGN_JUSTIFIED_ALL;
               image.ScalePercent(40);
               image.RotationDegrees = -30;
               p.Add(new Chunk(image, 0, -55));//x向右為正   y向下為負
               document.Add(p);

            //关闭文档对象
            document.Close();

      

            //=========================================
            //建立另外的pdf檔案

            document = new Document();
            // 创建文档写入实例
            PdfWriter.GetInstance(document, new FileStream("D:\\d.pdf", FileMode.Create));
      
            //打开文档内容对象
            document.Open();
            //设计各页的内容
            document.Add(new Paragraph("This is First Page"));
            //新添加一个页
            document.NewPage();
            //第2页中添加文本
            document.Add(new Paragraph("This is second page"));
            //重置页面数量
            document.ResetPageCount();
            //关闭文档对象
            document.Close();


            //=========================================
            //建立另外的pdf檔案

 

            //转换数据表为PDF文档
            //初始化一个目标文档类
            document = new Document();
            //调用PDF的写入方法流
            PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream("D:\\e.pdf", FileMode.Create));
            //打开目标文档对象
            document.Open();
            //创建PDF文档中的字体
            BaseFont baseFont = BaseFont.CreateFont();
            //根据字体路径和字体大小属性创建字体
            float f = 12;
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, f);
            PdfPTable table = new PdfPTable(6);
            //遍历原table的内容
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 6; j++)
                {
                    table.AddCell(new Phrase(Convert.ToString(i + 1) + ":" + Convert.ToString(j + 1), font));
                }

            }
            //在目标文档中添加转化后的表数据
            document.Add(table);
            //关闭目标文件
            document.Close();

        }
    }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 prague12 的頭像
    prague12

    prague12

    prague12 發表在 痞客邦 留言(0) 人氣()