Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么可以';我是否可以使用iTextSharp将一个PDF附加到另一个带有此代码的PDF?_C#_Visual Studio_Pdf_Itext_Append - Fatal编程技术网

C# 为什么可以';我是否可以使用iTextSharp将一个PDF附加到另一个带有此代码的PDF?

C# 为什么可以';我是否可以使用iTextSharp将一个PDF附加到另一个带有此代码的PDF?,c#,visual-studio,pdf,itext,append,C#,Visual Studio,Pdf,Itext,Append,我正在尝试使用iTextSharp将两个PDF文件的内容合并成一个新的PDF文件。我以前在类似的情况下使用过PDFStamper来实现这一点,但由于某些原因,这次它不起作用。附加根本不起作用;文件已创建,但在这段代码结束时,大小保持在0字节。任何人能提供的任何帮助都将不胜感激 File.Create(session.getLocalDir() + newPdfFile); // pasting content from original file to new file PdfReader r

我正在尝试使用iTextSharp将两个PDF文件的内容合并成一个新的PDF文件。我以前在类似的情况下使用过PDFStamper来实现这一点,但由于某些原因,这次它不起作用。附加根本不起作用;文件已创建,但在这段代码结束时,大小保持在0字节。任何人能提供的任何帮助都将不胜感激

File.Create(session.getLocalDir() + newPdfFile);

// pasting content from original file to new file
PdfReader reader = new PdfReader(originalFile);
string pageSelection = "1-" + reader.NumberOfPages;
reader.SelectPages(pageSelection);
PdfStamper stamper = new PdfStamper(reader, new FileStream(newPdfFile, FileMode.Append, FileAccess.Write));
stamper.Close();
reader.Close();

// pasting content from temp file to new file
reader = new PdfReader(temp);
pageSelection = "1-" + reader.NumberOfPages;
reader.SelectPages(pageSelection);
stamper = new PdfStamper(reader, new FileStream(newPdfFile, FileMode.Append, FileAccess.Write));
stamper.Close();
reader.Close();

这里最简单的解决方案是在添加临时pdf文件时使用MemoryStream保存该文件。经过一些研究,我发现正如mkl建议的那样,PdfStamper类不适合此操作。在iTextSharp中,您可以使用另一种方式附加2个PDF:

        MemoryStream stream = new MemoryStream();
        PdfCopyFields copy = new PdfCopyFields(stream);


        var ms1 = new MemoryStream(File.ReadAllBytes(file1Path));
        ms1.Position = 0;
        copy.AddDocument(new PdfReader(ms1));
        ms1.Dispose();

        var ms2 = new MemoryStream(File.ReadAllBytes(file2Path));
        ms2.Position = 0;
        copy.AddDocument(new PdfReader(ms2));
        ms2.Dispose();
        copy.Close();
生成的“stream”变量包含组合的PDF,可以使用PdfStamper写入文件

如果切换到.net的iText 7是一个选项,则可以省略PdfStamper,而改用PdfDocument.copyPagesTo()方法

一个简单的示例(对于.net,使用iText 7):


使用特定用例的解决方案进行了第二次编辑。

当您在调试器中使用它时,您观察到了什么?好吧,就是这样,它不会引发异常或以其他方式指示发生了任何错误。但是原始文件和临时文件都不会进行追加。不清楚为什么结果文件的长度为0字节,但尝试追加时无论如何都不会起作用。你不能简单地将两个PDF连接起来,然后希望结果是它们的合并。我不是在问例外情况。我想问的是,在执行代码时,每一行代码是否都符合您的期望。如果您的期望只是程序不会崩溃或抛出异常,那么您的代码就可以工作了!不过,我怀疑它们是否如此有限。每一行的期望值应该针对该行。不,它没有按照我的期望值工作。正如我在我的原始帖子中提到的,我以前已经使用此方法成功地将其附加到PDF中。唯一的区别是我没有使用我显式创建的PDF,就像我在上面的代码中所做的那样。也许这就是问题所在。但是有没有更好的方法可以将两个PDF合并成一个新的PDF?
    MemoryStream stream = new MemoryStream();
    PdfDocument outputDocument = new PdfDocument(new PdfWriter(stream));
    PdfDocument pdfSource = new PdfDocument(new PdfReader("c:\\firstInput.pdf"));

    pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages(), outputDocument);
    pdfSource = new PdfDocument(new PdfReader("c:\\secondInput.pdf"));
    pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages(), outputDocument);
    pdfSource.Close();
    outputDocument.Close();

    MemoryStream outputStream = new MemoryStream(stream.ToArray());
    outputDocument = new PdfDocument(new PdfReader(outputStream), new PdfWriter("c:\\result.pdf"));