【发布时间】: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