如何将包含PDF文件数据的两个内存流合并为一个

如何将包含PDF文件数据的两个内存流合并为一个

本文介绍了如何将包含PDF文件数据的两个内存流合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个PDF文件读入两个内存流,然后返回将同时包含两个流数据的流.但是我似乎不明白我的代码有什么问题.

I am trying to read two PDF files into two memory streams and then return a stream that will have both stream's data. But I don't seem to understand what's wrong with my code.

示例代码:

string file1Path = "Sampl1.pdf";
string file2Path = "Sample2.pdf";
MemoryStream stream1 = new MemoryStream(File.ReadAllBytes(file1Path));
MemoryStream stream2 = new MemoryStream(File.ReadAllBytes(file2Path));
stream1.Position = 0;
stream1.Copyto(stream2);
return stream2;   /*supposed to be containing data of both stream1 and stream2 but contains data of stream1 only*/

推荐答案

在PDF文件的情况下,内存流的合并与.txt文件不同.对于PDF,您需要使用一些.dll,就像我使用iTextSharp.dll(在AGPL许可下可用)一样,然后使用此库的功能将它们组合如下:

It appears in case of PDF files, the merging of memorystreams is not the same as with .txt files. For PDF, you need to use some .dll as I used iTextSharp.dll (available under the AGPL license) and then combine them using this library's functions as follows:

MemoryStream finalStream = new MemoryStream();
PdfCopyFields copy = new PdfCopyFields(finalStream);
string file1Path = "Sample1.pdf";
string file2Path = "Sample2.pdf";

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

finalStream包含ms1和ms2的合并pdf.

finalStream contains the merged pdf of both ms1 and ms2.

这篇关于如何将包含PDF文件数据的两个内存流合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:47