使用PdfCopy合并pdf文件这些需要添加在特定页码之后,而不是附加到末尾。我如何1:将pdf合并到特定页码2:pdfCopy.AddDocument不可用。我测试了5.4.3、5.4.5和5.5.10版本。我在这里错过了什么?所有人都说要使用5.XI...'PdfCopy'不包含'AddDocument'的定义,也找不到接受类型为'PdfCopy'的第一个参数的扩展方法'AddDocument'(您是否缺少using指令或程序集引用?)3:当pageToInsertat大于源中的总页数时,如何处理?我现在阅读了很多文档。所有报告使用PdfCopy和.AddDocument…在c#.net中使用iTextSharp并多个PDFhttp://weblogs.sqlteam.com/mladenp/archive/2014/01/10/simple-merging-of-pdf-documents-with-itextsharp-5-4-5.aspx这是我第一次接受它……使用系统;使用System.Windows.Forms;使用iTextSharp.text;使用iTextSharp.text.pdf;使用System.IO;namespacePdfMergeTest{publicpartialclassForm1:Form{privateconststringbaseFile="baseFile.tmp";privateconststringbaseTempPdfFileName="temp.pdf";publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidbtnMerge_Click(objectsender,EventArgse){if(!CheckBasePaths())return;//获取要合并到baseFile的文件varfilesToMerge=GetAllFilesToMerge();如果(filesToMerge.Length==0)返回;//获取我们需要合并上述文件的基础文件,它是.tmpextvarbaseFileWithPath=GetBaseFile();如果(string.IsNullOrWhiteSpace(baseFileWithPath))返回;//temp基础pdfvartempPdfWithPath=GetBaseTempFile();如果(string.IsNullOrWhiteSpace(tempPdfWithPath))返回;//遍历要合并的文件并合并到baseFilevarpage=2;//合并文件的页面,我们没有追加到最后。实际代码将从源中找到要合并的页面,并将向其中添加1foreach(FileInfotoMergeinfilesToMerge){//将基本文件复制为源的临时文件;此时出于调试目的File.Copy(baseFileWithPath,tempPdfWithPath,true);//开始合并,首先在#2,第二个在#4,第三个在#6,依此类推MergeFiles(baseFileWithPath,tempPdfWithPath,toMerge.FullName,page);页+=2;}}privateboolCheckBasePaths(){if(string.IsNullOrWhiteSpace(txtBaseDir.Text)){MessageBox.Show("NoBaseDirectory");返回假;}if(string.IsNullOrWhiteSpace(txtFilesToMergeToBase.Text)){MessageBox.Show("NofilestomergeDirectory");返回假;}如果(!Directory.Exists(txtBaseDir.Text)){MessageBox.Show("基本目录不存在");返回假;}if(!Directory.Exists(txtFilesToMergeToBase.Text)){MessageBox.Show("要合并目录的文件不存在");返回假;}返回真;}privateFileInfo[]GetAllFilesToMerge(){DirectoryInfod=newDirectoryInfo(txtFilesToMergeToBase.Text);FileInfo[]files=d.GetFiles("*.pdf");if(files.Length==0)MessageBox.Show("没有要合并的文件");返回文件;}privateStringGetBaseFile(){varmyBaseFile=Path.Combine(txtBaseDir.Text,baseFile);如果(!File.Exists(myBaseFile)){myBaseFile="";MessageBox.Show("基础文件丢失");}返回我的基础文件;}privateStringGetBaseTempFile(){varmyBaseTempFile=Path.Combine(txtBaseDir.Text,baseTempPdfFileName);返回我的BaseTempFile;}privatevoidMergeFiles(stringoriginalFile,stringsourceFile,stringtoMergeFile,intinsertPage){Documentdocument=null;PdfCopypdfCopy=null;PdfReaderpdfReader=null;try{//Step#1:创建一个document对象document=newDocument();//Step#2:创建一个writer来监听文档pdfCopy=newPdfSmartCopy(document,newFileStream(originalFile,FileMode.Create));如果(pdfCopy==null)返回;//Step#3:打开文档document.Open();//步骤#4:为toMergeFile创建一个阅读器并添加文档pdfReader=newPdfReader(toMergeFile);//添加整个文档而不是逐页添加pdfCopy.添加文档(pdfReader);pdfReader.Close();}catch(Exceptionex){MessageBox.Show(ex.Message);}finally{if(pdfReader!=null)pdfReader.Close();如果(pdfCopy!=null)pdfCopy.Close();如果(文档!=null)文档。关闭();}}}}我见过下面的用法.AddPage但这不是我想要的一些清洁工作,现在将在长周末之后进行。以上就是C#学习教程:使用PdfCopy合并pdf文件分享全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注—PrivateSubbtnExtractMerge_Click(senderAsObject,eAsEventArgs)HandlesbtnExtractMerge.ClicktxtCurSetupMessages.Text=""ShowMessage(txtCurSetupMessages,"Extract&MergeProcessStarted")'从DimbaseDirAsString=txtCurSetupBaseDataFolder.TextDimbaseInvDir形式获取并检查路径/文件。AsString=txtEMBaseFile.TextDimtargetFileNameAsString=txtEMTargetFile.TextIfString.IsNullOrWhiteSpace(baseDir)ThenShowMessage(txtCurSetupMessages,"Basefolderempty!")ExitSubEndIfIfIfString.IsNullOrWhiteSpace(baseInvDir)ThenShowCessage(t基本发票文件夹名称为空!”)ExitSubEndIfIfIfString.IsNullOrWhiteSpace(baseFileName)ThenShowMessage(txtCurSetupMessages,"Basefilenameempty!")ExitSubEndIfIfString.IsNullOrWhiteSpace(targetFileName)ThenShowMessage(txtCurSetupMessages,"目标文件名为空!")ExitSubEndIfIfNotDirectory.Exists(baseDir)ThenShowMessage(txtCurSetupMessages,"Basefolderdoesnotexist!")ExitSubEndIfbaseInvDir=System.IO.Path.Combine(baseDir,baseInvDir)IfNotDirectory.Exists(baseInvDir)ThenShowMessage(txtCurSetupMessages,"Baseinvoicefolderdoesnotexist!")ExitSubEndIf'获取发票文件DimdirInfoAsDirectoryInfo=NewDirectoryInfo(baseInvDir)DimfilesAsFileInfo()=dirInfo.GetFiles("*.pdf")Iffiles.LengthbgReader.NumberOfPagesThenFindPageNo=-1'错误。无效的bgReader.Close()EndIfpagen=0Fori=startpageTobgReader.NumberOfPagesstr1=pdftextextractor.GetTextFromPage(bgReader,i)pos1=str1.IndexOf("InvoiceNo:")pos2=str1.IndexOf("Phone:")Ifpos2>pos1ThenIfstr1.Substring(pos1+11,pos2-pos1-11).Trim.Equals(invno)=TrueThenpagen=i'foundthepage'bgReader.Close()'退出函数ElseIf第0页en'我们找到了页码。所以不需要更进一步。'现在退出FindPageNo=pagen'找到最后一页bgReader.Close()ExitFunctionEndIfEndIfEndIfNextibgReader.Close()EndIfIfpagen0ThenFindPageNo=pagen'找到最后一页ElseFindPageNo=0'未找到EndIfEndFunctionPrivateFunctionBuildTempBeforeAndAfter(ByValtempSourceAsString,ByValtempBeforeAsString,ByValtempAfterAsString,ByValendPageAsInteger,ByValtotalPagesAsInteger)作为布尔值DimreaderAsPdfReader=NothingDimcopyAsPdfCopy=NothingDimdocAsDocument=NothingDimimpPageAsPdfImportedPage=NothingDimisBuildAsBoolean=True尝试reader=NewPdfReader(tempSource)doc=NewDocument(reader.GetPageSizeWithRotation(1))'beforefilecopy=NewPdfCopy(doc,NewFileStream(tempBefore,FileMode.Create))doc.Open()Forindex=1ToendPageimpPage=copy.GetImportedPage(reader,index)copy.AddPage(impPage)Nextcopy.Close()doc.Close()'afterfiledoc=NewDocument(reader.GetPageSizeWithRotation(1))copy=NewPdfCopy(doc,NewFileStream(tempAfter,FileMode.Create))doc.Open()Forindex=endPage+1TototalPagesimpPage=copy.GetImportedPage(reader,index)copy.AddPage(impPage)Nextreader.Close()copy.Close()doc.Close()CatchexAsExceptionisBuild=False最后'cleantheobjectsIfNotreaderIsNothingThenreader.Close()IfNotdocIsNothingThendoc.Close()如果不是readerIsNothingThenreader.Close()EndTryReturnisBuildEndFunctionPrivateFunctionAddDocuments(ByValtargetFileAsString,ByValaddFile1AsString,ByValaddFile2AsString,ByValaddFile3AsString)AsBooleanDimreaderAsPdfReader=NothingDimcopyAsPdfCopy=NothingDimdocAsDocument=NothingDimisAddedAsBoolean=TrueTrydoc=NewDocumentcopy=NewPdfSmartCopy(doc,NewFileStream(targetFile,FileMode.Create))doc.Open()'添加文件1IfNotString.IsNullOrWhiteSpace(addFile1)Thenreader=NewPdfReader(addFile1)copy.AddDocument(reader)reader.Close()EndIf'addfile2如果不是String.IsNullOrWhiteSpace(addFile2)Thenreader=NewPdfReader(addFile2)copy.AddDocument(reader)reader.Close()EndIf'添加文件3IfNotString.IsNullOrWhiteSpace(addFile3)Thenreader=NewPdfReader(addFile3)copy.AddDocument(reader)reader.Close()EndIfcopy.Close()doc.Close()CatchexAsExceptionisAdded=FalseFinally'cleantheobjectsIfNotreaderIsNothingThenreader.Close()IfNotdocIsNothingThendoc.Close()IfNotreaderIsNothingThenreader.Close()EndTryReturnisAddedEndFunction,不代表一个职位,如涉及侵权,请点击维权联系管理员删除如转载请注明出处:
