我需要打开一个Microsoft文档,替换一些文本,然后转换成一个pdf字节数组。我已经创建了这样做的代码,但它涉及到将pdf保存到磁盘,并将字节读入内存。我想避免将任何东西写入磁盘,因为我不需要保存文件。
下面是我迄今所做的代码..。
using System.IO;
using Microsoft.Office.Interop.Word;
public byte[] ConvertWordToPdfArray(string fileName, string newText)
{
// Temporary path to save pdf
string pdfName = fileName.Substring(0, fileName.Length - 4) + ".pdf";
// Create a new Microsoft Word application object and open the document
Application app = new Application();
Document doc = app.Documents.Open(docName);
// Make any necessary changes to the document
Selection selection = doc.ActiveWindow.Selection;
selection.Find.Text = "{{newText}}";
selection.Find.Forward = true;
selection.Find.MatchWholeWord = false;
selection.Find.Replacement.Text = newText;
selection.Find.Execute(Replace: WdReplace.wdReplaceAll);
// Save the pdf to disk
doc.ExportAsFixedFormat(pdfName, WdExportFormat.wdExportFormatPDF);
// Close the document and exit Word
doc.Close(false);
app.Quit();
app = null;
// Read the pdf into an array of bytes
byte[] bytes = File.ReadAllBytes(pdfName);
// Delete the pdf from the disk
File.Delete(pdfName);
// Return the array of bytes
return bytes;
}
在不写入磁盘的情况下,如何实现相同的结果?整个操作需要在内存中运行。
为了解释为什么我需要这样做,我希望ASP.NET MVC应用程序的用户能够将报表模板上传为word文档,当返回到浏览器时,该文档将被呈现为pdf。
发布于 2016-01-21 10:35:14
有两个问题:
所以这是不可以的。
https://stackoverflow.com/questions/34920847
复制相似问题