码迷,mamicode.com
首页 > 其他好文 > 详细

将一个目录中所有PDF文件合并到一个新的PDF文件中

时间:2014-07-09 14:38:48      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:http   文件   width   os   art   cti   

将一个目录中所有PDF文件合并到一个新的PDF文件中

 

bubuko.com,布布扣

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;//这个dll要引用
using iTextSharp.text.pdf;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            mergePDFFiles(tb_path.Text, tb_result.Text);
        }
        /// <summary>
        /// PDF合并
        /// </summary>
        /// <param name="fileList">需要合并的PDF文件路径</param>
        /// <param name="outMergeFile">合并保存输出路径</param>
        public void mergePDFFiles(string filePath, string outMergeFile)
        {
            PdfReader reader;
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            string[] fileList = System.IO.Directory.GetFiles(filePath, "*.pdf");
            for (int i = 0; i < fileList.Length; i++)
            {
                if (!File.Exists(fileList[i]))
                    continue;

                reader = new PdfReader(fileList[i]);
                int iPageNum = reader.NumberOfPages;
                for (int j = 1; j <= iPageNum; j++)
                {
                    document.NewPage();
                    newPage = writer.GetImportedPage(reader, j);
                    cb.AddTemplate(newPage, 0, 0);
                }
            }
            document.Close();
        }

    }
}

将一个目录中所有PDF文件合并到一个新的PDF文件中,布布扣,bubuko.com

将一个目录中所有PDF文件合并到一个新的PDF文件中

标签:http   文件   width   os   art   cti   

原文地址:http://www.cnblogs.com/swtool/p/3832411.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!