从varbinary读取word文档,编辑,创建受写保护的pdf文件

问题描述 投票:0回答:1

我有一种使用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个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.