【问题标题】:read word document from varbinary, edit, create a write protected pdf从 varbinary 读取 word 文档,编辑,创建一个写保护的 pdf
【发布时间】:2020-01-08 17:51:14
【问题描述】:

我有一种方法,它使用 Microsoft.Office.Interop 来读取 Word 文档、对其进行编辑并将其写入为 PDF。我有一个单独的方法,它使用 Itext7 读取此 PDF 并将其写入可以查看和打印但不能轻易更改的不同 PDF。

第一种方法是从磁盘读取word文档;但是,我被要求让它从存储为 varbinary 的变量的 sql 查询中读取文档,并将最终结果写为 varbinary - 不使用磁盘上的任何中间文件。我想我需要将这些阅读为“流”

这是我所拥有的:

class clsMakeCert
{
    string myName;
    public string myDate;

    public clsMakeCert(string name, DateTime date)
    {
        myName = name;
        myDate = date.ToString("MM/dd/yyyy");
    }
    public void createCertPdf(string certFilename)
    {
        // Get the document out of the SQL table
        System.Data.DataTable dtContents = new System.Data.DataTable();
        SqlDataReader rdr_Contents = null;

        using (SqlConnection conn = new SqlConnection("Server=KGREEN3-LT\\SQLEXPRESS;Initial Catalog=SAN;Integrated Security=SSPI"))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand("Select [file_data] From cert_files where filename=@certFilename", conn);
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@certFilename", certFilename);
                rdr_Contents = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                dtContents.Load(rdr_Contents);
            }
        }
        byte[] byteArray = (byte[])dtContents.Rows[0]["file_data"];

        // Put it into a word document
        Application wordApp = new Application { Visible = false };
        Document doc = new Document();

        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(byteArray, 0, (int)byteArray.Length);
            doc = wordApp.Documents.Open(stream, ReadOnly: false, Visible: false);
            doc.Activate();
            FindAndReplace(wordApp, "First Middle Last", myName);
            FindAndReplace(wordApp, "11/11/1111", myDate);

        }



        return ;

    }

}

不过,open 方法似乎不会接受流。

看起来我可以使用 openXML 以流的形式读取/编辑 docx。但它不会转换为pdf。 看起来我可以使用 Interop 读取/编辑 docx 并写入 PDF,但我不能将其作为流(仅作为磁盘上的文件)进行。

有没有办法让 Interop 读取流(即从 varbinary 加载的文件)? k

【问题讨论】:

    标签: pdf office-interop filestream varbinary


    【解决方案1】:

    不,“互操作”一词不支持流式传输。最接近的(也是唯一具有此功能的 Office 应用程序)是使用对象模型的InsertXML 方法以 OPC 平面文件格式传入必要的 Word Open XML。但是,不能保证结果与传入的 Word Open XML 文件完全相同,因为目标文档的设置可能会覆盖一些传入的设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多