将PDF从内存加载到telerik:RadPdfViewer

时间:2022-10-12 21:54:26

I have a PDF file stored in a database as a byte array. I'm reading the PDF byte array from my database back into my application.

我有一个PDF文件存储在数据库中作为字节数组。我正在从我的数据库中读取PDF字节数组回到我的应用程序中。

Now, I'm trying to display the PDF with the RadPdfViewer but it is not working.

现在,我正在尝试使用RadPdfViewer显示PDF但它无法正常工作。

Here is my code:

这是我的代码:

    byte[] pdfAsByteArray= File.ReadAllBytes(@"C:\Users\Username\Desktop\Testfile.pdf");

    //Save "pdfAsByteArray" into database
    //...
    //Load pdf from database into byte[] variable "pdfAsByteArray"

    using (var memoryStream = new MemoryStream(pdfAsByteArray))
    {
        this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
    }

when I execute the application I just get an empty PdfViewer.

当我执行应用程序时,我得到一个空的PdfViewer。

Question: How do I set the DocumentSource the right way?

问题:如何以正确的方式设置DocumentSource?

Question: How do I dispose the stream? (note that using doesn't works)

问题:如何处理流? (注意使用不起作用)

Note: I wan't to avoid things like writing a temp file to disk

注意:我不想避免将临时文件写入磁盘


Edit:

编辑:

I figured it out but I am not completely satisfied with this solution:

我想通了,但我对这个解决方案并不完全满意:

Not working:

不工作:

using (var memoryStream = new MemoryStream(pdfAsByteArray))
{
    this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);
}

Working:

加工:

var memoryStream = new MemoryStream(pdfAsByteArray);
this.PdfViewer.DocumentSource = new PdfDocumentSource(memoryStream);

I don't know how teleriks RadPdfViewer component works but I wan't to dispose the Stream.

我不知道teleriks RadPdfViewer组件是如何工作的,但我不想处理Stream。

2 个解决方案

#1


7  

From the Telerik documentation (particularly with regards to the "Caution" stating that this loading is done asynchronously), I believe this should work while still providing you a way to close the stream (not as cleanly as if you were able to use a using block, but still better than leaving it open):

从Telerik文档(特别是关于“注意”表示此加载是异步完成的),我相信这应该可以工作,同时仍然为您提供关闭流的方法(不像您可以使用使用一样干净利落阻止,但仍然比打开它更好):

//class variable
private MemoryStream _stream;

_stream = new MemoryStream(pdfAsByteArray);
var docSource = new PdfDocumentSource(memoryStream);
docSource.Loaded += (sender, args) => { if (_stream != null) _stream.Dispose();};
this.PdfViewer.DocumentSource = docSource;

I did this free-hand and don't have access to the Telerik API so the exact details of the Loaded event are not available to me.

我是徒手做到的,并且无法访问Telerik API,因此我无法获得Loaded事件的确切详细信息。

EDIT
Here's the relevant details from documentation I found (emphasis mine):

编辑这是我发现的文件中的相关细节(强调我的):

The PdfDocumentSource loads the document asynchronously. If you want to obtain a reference to the DocumentSource after you have imported a document, you should use the Loaded event of the PdfDocumentSource object to obtain the loaded document. This is also a convenient method that can be used to close the stream if you are loading a PDF from a stream.

PdfDocumentSource异步加载文档。如果要在导入文档后获取对DocumentSource的引用,则应使用PdfDocumentSource对象的Loaded事件来获取已加载的文档。如果从流中加载PDF,这也是一种方便的方法,可用于关闭流。

#2


2  

You need to implement PdfDocumentSource Loaded event. This is when the stream gets loaded and used up, and can be closed / disposed at that time.

您需要实现PdfDocumentSource Loaded事件。这是当流加载并用完时,可以在那时关闭/处理。

#1


7  

From the Telerik documentation (particularly with regards to the "Caution" stating that this loading is done asynchronously), I believe this should work while still providing you a way to close the stream (not as cleanly as if you were able to use a using block, but still better than leaving it open):

从Telerik文档(特别是关于“注意”表示此加载是异步完成的),我相信这应该可以工作,同时仍然为您提供关闭流的方法(不像您可以使用使用一样干净利落阻止,但仍然比打开它更好):

//class variable
private MemoryStream _stream;

_stream = new MemoryStream(pdfAsByteArray);
var docSource = new PdfDocumentSource(memoryStream);
docSource.Loaded += (sender, args) => { if (_stream != null) _stream.Dispose();};
this.PdfViewer.DocumentSource = docSource;

I did this free-hand and don't have access to the Telerik API so the exact details of the Loaded event are not available to me.

我是徒手做到的,并且无法访问Telerik API,因此我无法获得Loaded事件的确切详细信息。

EDIT
Here's the relevant details from documentation I found (emphasis mine):

编辑这是我发现的文件中的相关细节(强调我的):

The PdfDocumentSource loads the document asynchronously. If you want to obtain a reference to the DocumentSource after you have imported a document, you should use the Loaded event of the PdfDocumentSource object to obtain the loaded document. This is also a convenient method that can be used to close the stream if you are loading a PDF from a stream.

PdfDocumentSource异步加载文档。如果要在导入文档后获取对DocumentSource的引用,则应使用PdfDocumentSource对象的Loaded事件来获取已加载的文档。如果从流中加载PDF,这也是一种方便的方法,可用于关闭流。

#2


2  

You need to implement PdfDocumentSource Loaded event. This is when the stream gets loaded and used up, and can be closed / disposed at that time.

您需要实现PdfDocumentSource Loaded事件。这是当流加载并用完时,可以在那时关闭/处理。