【问题标题】:Stream PDF to Byte Array将 PDF 流式传输到字节数组
【发布时间】:2015-11-15 15:48:02
【问题描述】:

我正在尝试将 pdf 文件加载到字节数组,但在代码运行后字节数组长度为零。不知道我做错了什么。该项目是 vs 2010,在 vm 9x 中运行 win 7 32 位操作系统。希望有一个简单的解决方法来解决这个问题。提前致谢。

Demonstrates the code I'm using to stream the pdf file to byte array

【问题讨论】:

  • 可能路径不正确。文件名中有空格。
  • 路径和文件字符串都正确。问题很简单,文件是空的。我在原始帖子之后发现了这一点,并且有点尴尬地承认了过度网站。
  • 路径正确。我引用的文件原来是空的。最后要分享的是,pdf 查看器上有一个属性,需要设置文档属性才能使其工作。渲染器的工作方式如下所示,但查看器更好的是它具有使其更易于使用的附加功能。感谢您的帮助...

标签: c# winforms pdf bytearray


【解决方案1】:

以下代码对我有用:

    private void btnOpenFile_Click(object sender, EventArgs e)
    {
        string strId = gridViewMain.SelectedRows[0].Cells["id"].Value.ToString();

        if (!string.IsNullOrEmpty(strId))
        {

            SqlCommand cmd = new SqlCommand(strQuery_GetAttachmentById, objConn);
            cmd.Parameters.AddWithValue("@attachId", strId);

            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataTable dt = new DataTable();

            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            SqlCommandBuilder sqlCmdBuilder = new SqlCommandBuilder(da);

            da.Fill(dt);

            DataRow dr = dt.Rows[0];

            byte[] PDFBytes = (byte[])dr["attachment"];
            LoadPdf(PDFBytes);
        }

    }

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

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

        // Load PDF Document into WinForms Control
        pdfRenderer1.Load(pdfDocument);    
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2013-04-18
    相关资源
    最近更新 更多