问题描述
谁能给我一个如何从MemoryStream
获取PdfReader
的示例吗?我可以看到PdfReader
类有几个看起来像候选方法的方法(GetStreamBytes
& GetStreamBytesRaw
),但是这些方法似乎想要特定于iText
的流,而我只是常规的Byte[]
或MemoryStream
.
Can anyone give me an example of how to get a PdfReader
from a MemoryStream
? I can see that the PdfReader
class has a couple of methods which look like likely candidates (GetStreamBytes
& GetStreamBytesRaw
), however these seem to want iText
-specific streams, mine is just a regular Byte[]
or MemoryStream
.
这使用的是C#和.NET 4.
This is using C# and .NET 4.
iTextSharp.text.pdf.PdfReader rdr = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw
推荐答案
只要MemoryStream是有效的PDF对象,就可以从MemoryStream创建PdfReader.如果MemoryStream是有效的PDF对象,则启动PdfReader的一种方法是:
You can create a PdfReader from a MemoryStream, so long as the MemoryStream is a valid PDF object. If the MemoryStream is a valid PDF object, then one way to initiate the PdfReader is this way:
PdfReader _reader = new PdfReader((byte[])_memoryStream.ToArray());
在下面的代码中,从.Net Resource初始化了PdfReader,当从Properties.Resources对象调用该资源时,它作为字节[]返回,因此Resource和MemoryStream将相同的类型返回给PdfReader,即一个字节[].然后,我从PdfReader对象创建一个PdfStamper对象,并将MemoryStream用作PdfStamper的结果容器.
In the code below, the PdfReader is initialized from .Net Resource which is returned as a byte[] when called from the Properties.Resources object, so the Resource and the MemoryStream are returning the same type to the PdfReader, a byte[]. I then create a PdfStamper object from the PdfReader object, and use a MemoryStream as the resulting container for the PdfStamper.
PdfReader _srcDoc = new PdfReader(Properties.Resources.Resource1);
MemoryStream _output = new MemoryStream();
PdfStamper _scratchDoc = new PdfStamper(_srcDoc, _output);
这篇关于来自MemoryStream()的PdfReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!