使用ITEXTSHARP 合并PDF(包含签名)

需求,将不同文件夹下所有的PDF合并。

下载itextsharp.dll 并引入

递归生成pdf文件List

private List<string> FindFile(string Path)
        {
            List<string> list = new List<string>();

            director(Path, list);
            
            return list;

        }
        public void director(string Path,List<string> flist)
        {
            DirectoryInfo di = new DirectoryInfo(Path);
            FileSystemInfo[] fis = di.GetFileSystemInfos();//
            foreach (FileSystemInfo temp in fis)
            {
                if (temp is DirectoryInfo)
                {
                    //递归调用
                    director(temp.FullName,flist);
                }
                else
                {
                    if (temp.Name.ToString().Contains(".pdf"))
                    {
                        flist.Add(Path + "\\" + temp.Name);
                    }
                }
            }


        }

合并PDF 方法1:读取并写入(缺点无签名)

        public static void MergePDFFile(List<string> fileList, string outMergeFile)
        {
            PdfReader reader;
            iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle((float)609.45, (float)430.87);
            Document document = new Document(rec);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            foreach (var itemFile in fileList)
            {
                if (!File.Exists(itemFile))
                {
                    string fileName = Path.GetFileName(itemFile);
                    MessageBox.Show(string.Format("文件打印合并__{0} 文件不存在", fileName));
                    continue;
                }
                FileInfo fInfo = new FileInfo(itemFile);
                if (fInfo.Length < 1)
                {
                    string fileName = Path.GetFileName(itemFile);
                    MessageBox.Show(string.Format("文件打印合并__文件内容为空,无法打印,{0}", fileName));
                    return;
                }


                var ext = Path.GetExtension(itemFile).ToLower();
                if (".pdf".Equals(ext))
                {
                    reader = new PdfReader(itemFile);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 1; j <= iPageNum; j++)
                    {
                        document.NewPage();
                        newPage = writer.GetImportedPage(reader, j);
                        string fpath = AppDomain.CurrentDomain.BaseDirectory + "\\BQ.JPG";
                        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(fpath);
                        img.SetAbsolutePosition((float)409, (float)500);
                        writer.DirectContent.AddImage(img);
                        //float w =  reader.GetPageSize(1).Width;
                        //float h =  reader.GetPageSize(1).Height;
                        // float Scale = w > h ? h : w;
                        cb.AddTemplate(newPage, 0, 0);
                        //cb.AddTemplate(newPage, Scale, 0, 0, Scale, 0, 0);
                    }
                }

            }
            document.Close();
            MessageBox.Show("合并成功!");
        }

合并PDF 方法2:读取并COPY(包含签名)

public static void MergePDFFile(List<string> fileList, string outMergeFile)
        {
            PdfReader reader;
            iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle((float)609.45, (float)430.87);
            Document document = new Document(rec);
         
            FileStream baos = new FileStream(outMergeFile, FileMode.Create, FileAccess.Write);
            PdfSmartCopy copy = new PdfSmartCopy(document, baos);
            copy.ViewerPreferences = PdfWriter.HideToolbar | PdfWriter.HideMenubar;  
            document.Open();
      
            foreach (var itemFile in fileList)
            {
                if (!File.Exists(itemFile))
                {
                    string fileName = Path.GetFileName(itemFile);
                    MessageBox.Show(string.Format("文件打印合并__{0} 文件不存在", fileName));
                    continue;
                }
                FileInfo fInfo = new FileInfo(itemFile);
                if (fInfo.Length < 1)
                {
                    string fileName = Path.GetFileName(itemFile);
                    MessageBox.Show(string.Format("文件打印合并__文件内容为空,无法打印,{0}", fileName));
                    return;
                }


                var ext = Path.GetExtension(itemFile).ToLower();
                if (".pdf".Equals(ext))
                {
                    reader = new PdfReader(itemFile);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 1; j <= iPageNum; j++)
                    {
                        document.NewPage();
                        PdfImportedPage page = copy.GetImportedPage(reader, j);
                        copy.AddPage(page);  
            
                    }
                }

            }
            document.Close();
            MessageBox.Show("合并成功!");
        }
       
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值