实现pdf合并就是使用Spire.Pdf.dll类库的方法,但是注意需要同时引用Spire.Pdf.dll和Spire.License.dll两个类库,且两个类库的版本要一致
String[] files = new String[Dir.GetFiles().Length]; int i = 0; foreach (FileInfo f in Dir.GetFiles()) //循环文件 { files[i] = f.FullName; i = i + 1; } string outputFile = newPathDir + "\\" + Dir.Name+".pdf";//输出的路径 PdfDocumentBase doc =Spire.Pdf.PdfDocument.MergeFiles(files); doc.Save(outputFile, FileFormat.PDF); //System.Diagnostics.Process.Start(outputFile);//打开文件
但是使用spire的话合并成的pdf第一页会出现文字水印,这并不是我们要的,所以需要去除这个文字水印,那么就要用到iTextSharp.text.pdf.dll类库
PRStream stream; String content; PdfArray contentarray; string watermarkText = "要去除的水印文字"; PdfReader reader2 = new PdfReader(path); reader2.RemoveUnusedObjects(); PdfDictionary page = reader2.GetPageN(1);//获取第一页 contentarray = page.GetAsArray(PdfName.CONTENTS); if (contentarray != null) { //Loop through content for (int j = 0; j < contentarray.Size; j++) { //Get the raw byte stream stream = (PRStream)contentarray.GetAsStream(j); //Convert to a string. NOTE, you might need a different encoding here content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));//获取pdf页内的文字内容 //Look for the OCG token in the stream as well as our watermarked text if (content.IndexOf("/OC") >= 0 || content.IndexOf(watermarkText) >= 0)//如果pdf内容包含水印文字 { //Remove it by giving it zero length and zero data content= content.Replace(watermarkText, "");//替换水印文字为空 byte[] byteArray = System.Text.Encoding.Default.GetBytes(content);//转换为byte[] stream.Put(PdfName.LENGTH, new PdfNumber(byteArray.Length));//重新指定大小 stream.SetData(byteArray);//重新赋值 } } } FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None); PdfStamper stamper = new PdfStamper(reader2, fs); //stamper.SetFullCompression(); if (stamper != null) { stamper.Close(); } if (null != fs) { fs.Close(); } if (null != reader2) { reader2.Close(); }
对,就是这么简单
Tale、车车