如何在Word中将word doc的字节数组转换为pdf文档的字节数组

时间:2015-01-14 15:48:08

标签: c# pdf stream crystal-reports-2010

我有一个字节序列,如下所示(Word格式)从水晶报告中导出

 Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);

我想将该流转换为PDF流,以存储在sql server数据库的varbinary列中。

文档中有条形码,当我导出为pdf时,我无法嵌入它。导出到单词虽然有效但我想尝试从Word到PDF 任何人都可以帮助我获取这个PDF字节[]?

1 个答案:

答案 0 :(得分:0)

您可以使用Microsoft.Office.Interop.Word NuGet Package执行此操作。将其添加到应用程序后,可以将字节数组刷新为临时文件,然后使用 Interop.Word 打开临时文件,使用它,保存结果并将结果读回字节阵列。

以下是一个示例代码段:

// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);

Application app = new word.Application();
Document doc = app.Documents.Open(filePath);

// Save DOCX into a PDF
var pdfPath = "path-to-pdf-file.pdf";
doc.SaveAs2(pdfPath, word.WdSaveFormat.wdFormatPDF);

doc.Close();
app.Quit(); // VERY IMPORTANT: do this to close the MS Word instance
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);

有关该主题的更多信息,您还可以在我的博客上read this post