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

使用itextsharp创建PDF文档——图片集合

2013年02月11日 ⁄ 综合 ⁄ 共 2237字 ⁄ 字号 评论关闭

文档管理系统中 ,扫描模块将文档或证件扫描后。为了便于保存多个图片,拟将多个图片生成一个PDF文档进行保存。

这里我们就需要PDF生成工具了。你可以在这里下载。PDFCreator

主要使用了开源工具ITextSharp生成PDF文档。

测试界面如下:

选择图片,可多选

生成PDF

生成的PDF文件:

目前只是生成图片的pdf文件,至于更高级的应用的探索,以后写文章再说吧。

其中关键代码PDFCreator如下

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace PDFCreator
{
    public class PDFCreator
    {
        iTextSharp.text.Document pdfdoc;
        iTextSharp.text.Image pdfImg;
        iTextSharp.text.pdf.PdfWriter pdfwriter;

        string tmpFilePath;
        public PDFCreator()
        {
            pdfdoc = new iTextSharp.text.Document();

            try
            {
                tmpFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\~tmpPdfCreatorFile.pdf";
                if (File.Exists(tmpFilePath))
                    File.Delete(tmpFilePath);
                pdfwriter = iTextSharp.text.pdf.PdfWriter.GetInstance(pdfdoc, new FileStream(tmpFilePath, FileMode.CreateNew));
                pdfdoc.Open();
            }
            catch
            {
                throw new Exception("此文件已存在!");
            }
        }
        public bool AddURIImage(string imageUrl)
        {
            try
            {
                //pdfdoc.Open();
                pdfdoc.NewPage();
                //String imageUrl = "http://jenkov.com/images/" +
                //        "20081123-20081123-3E1W7902-small-portrait.jpg";
                pdfImg = iTextSharp.text.Image.GetInstance(new Uri(imageUrl));
                pdfImg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
                //pdfImg.SetAbsolutePosition(500f, 650f);
                pdfdoc.Add(pdfImg);

                //pdfdoc.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public bool AddImageFromFile(string imageFilePath)
        {
            try
            {
                

                pdfdoc.NewPage();
                pdfImg = iTextSharp.text.Image.GetInstance(imageFilePath);
                pdfImg.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
              //  pdfImg.SetAbsolutePosition((iTextSharp.text.PageSize.POSTCARD.Width - pdfImg.ScaledWidth) / 2,
              //(iTextSharp.text.PageSize.POSTCARD.Height - pdfImg.ScaledHeight) / 2);
              //  pdfwriter.DirectContent.AddImage(pdfImg);
                pdfdoc.Add(pdfImg);
                //pdfdoc.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        public void SaveToFile(string fileName)
        {

            //pdfwriter.Close();
            //pdfwriter.Dispose();
            pdfdoc.Dispose();
            File.Copy(tmpFilePath, fileName, true);
            File.Delete(tmpFilePath);
        }
    }
}

调用代码如下:

 private void btnCreatePDF_Click(object sender, EventArgs e)
        {
            SaveFileDialog sdiag = new SaveFileDialog();
            sdiag.Filter = "PDF文档|*.pdf";
            if (sdiag.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                PDFCreator creator = new PDFCreator();
                foreach (object obj in listBox1.Items)
                {
                    if (obj == null)
                        continue;
                    string imgFilePath = obj.ToString();
                    creator.AddImageFromFile(imgFilePath);
                   
                }
                //
                creator.SaveToFile(sdiag.FileName);
            }
        }

你可以在这里下载。PDFCreator

抱歉!评论已关闭.