从内存中读取数据流存储PDF [英] Read a stored PDF from memory stream

查看:1280
本文介绍了从内存中读取数据流存储PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个数据库项目使用C#和SQLServer 2012年的工作在我的形式之一,我有一个PDF文件存储在一个表中的一些其他信息。这是工作顺利,但是当我想要检索存储的信息,我有显示的PDF文件有问题,因为我不能显示它,我不知道如何来显示它。

我看了一些文章,说这不能用从一个内存流的Adobe PDF查看器中显示,有没有什么办法吗?

这是我的code从数据库中检索数据:

  sql_com.CommandText =SELECT * FROM incoming_boks_tbl其中[incoming_bok_id] = @ incoming_id和[incoming_date] = @ incoming_date;
            sql_com.Parameters.AddWithValue(incoming_id,up_inco_num_txt.Text);
            sql_com.Parameters.AddWithValue(incoming_date,up_inco_date_txt.Text);
            sql_dr = sql_com.ExecuteReader();
            如果(sql_dr.HasRows)
            {
                而(sql_dr.Read())
                {
                    up_incoming_id_txt.Text = sql_dr [0]的ToString();
                    up_inco_num_txt.Text = sql_dr [1]的ToString();
                    up_inco_date_txt.Text = sql_dr [2]的ToString();
                    up_inco_reg_txt.Text = sql_dr [3]的ToString();
                    up_inco_place_txt.Text = sql_dr [4]的ToString();
                    up_in_out_txt.Text = sql_dr [5]的ToString();
                    up_subj_txt.Text = sql_dr [6]的ToString();
                    up_note_txt.Text = sql_dr [7]的ToString();
                    串file_ext = sql_dr [8]的ToString(); // pdf文件扩展
                    byte []的inco_file =(字节[])(sql_dr [9]); // PDF文件
                    MemoryStream的毫秒=新的MemoryStream(inco_file);
                    //这里我不知道该怎么办内存流文件数据,并在那里储存。我怎样才能显示它?
                }
            }
 

解决方案

这个答案应该给你一些选项:如何使用渲染PDF文件C#


在过去,我已经使用谷歌的开源PDF渲染项目 - PDFium

有一个名为 PdfiumViewer 一个C#的NuGet包,它提供了围绕PDFium一个C#包装,让PDF文件被显示和打印

有直接与流所以不需要任何数据被写入到磁盘

这是我举的例子从一个WinForms应用程序

 公共无效LoadPdf(byte []的pdfBytes)
    {
        VAR流=新的MemoryStream(pdfBytes);
        LoadPdf(流)
    }

    公共无效LoadPdf(流流)
    {
        //创建PDF文档
        VAR pdfDocument = PdfDocument.Load(流);

        //加载PDF文件到的WinForms控制
        pdfRenderer.Load(_pdfDocument);
    }
 

I'm working on a database project using C# and SQLServer 2012. In one of my forms I have a PDF file with some other information that is stored in a table. This is working successfully, but when I want to retrieve the stored information I have a problem with displaying the PDF file, because I can't display it and I don't know how to display it.

I read some articles that said it can not be displayed with Adobe PDF viewer from a memory stream, is there any way to that?

This is my code for retrieving the data from the database:

            sql_com.CommandText = "select * from incoming_boks_tbl where [incoming_bok_id]=@incoming_id and [incoming_date]=@incoming_date";
            sql_com.Parameters.AddWithValue("incoming_id",up_inco_num_txt.Text);
            sql_com.Parameters.AddWithValue("incoming_date", up_inco_date_txt.Text);
            sql_dr = sql_com.ExecuteReader();
            if(sql_dr.HasRows)
            {
                while(sql_dr.Read())
                {
                    up_incoming_id_txt.Text = sql_dr[0].ToString();
                    up_inco_num_txt.Text = sql_dr[1].ToString();
                    up_inco_date_txt.Text = sql_dr[2].ToString();
                    up_inco_reg_txt.Text = sql_dr[3].ToString();
                    up_inco_place_txt.Text = sql_dr[4].ToString();
                    up_in_out_txt.Text = sql_dr[5].ToString();
                    up_subj_txt.Text = sql_dr[6].ToString();
                    up_note_txt.Text = sql_dr[7].ToString();
                    string file_ext = sql_dr[8].ToString();//pdf file extension
                    byte[] inco_file = (byte[])(sql_dr[9]);//the pdf file
                    MemoryStream ms = new MemoryStream(inco_file);
                    //here I don't know what to do with memory stream file data and where to store it. How can i display it?
                }
            }

解决方案

This answer should give you some options: How to render pdfs using C#


In the past I have used Googles open source PDF rendering project - PDFium

There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.

It works directly with Streams so doesn't require any data to be written to disk

This is my example from a WinForms app

    public void LoadPdf(byte[] pdfBytes)
    {
        var stream = new MemoryStream(pdfBytes);
        LoadPdf(stream)
    }

    public void LoadPdf(Stream stream)
    {
        // Create PDF Document
        var pdfDocument = PdfDocument.Load(stream);

        // Load PDF Document into WinForms Control
        pdfRenderer.Load(_pdfDocument);
    }

这篇关于从内存中读取数据流存储PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆