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


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

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

        }
    }

  • 相关阅读:
    php 将富文本编辑后的内容取出
    阿里云Windows远程连接出现身份验证错误,要求的函数不正确”的报错。
    composer切换中国镜像
    php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法
    golang ioutil 包源码阅读
    ssh 远程登录 REMOTE HOST IDENTIFICATION HAS CHANGED 问题
    Golang -- fallthrough
    Golang 执行 go run main.go 显示 undefined
    Golang Playground 进度条示例
    关系型数据库和非关系型数据库(NOSQL)
  • 原文地址:https://www.cnblogs.com/swtool/p/3832411.html
Copyright © 2020-2023  润新知