簡體   English   中英

在C#中將HTML轉換為doc並以二進制形式返回

[英]convert Html to doc in c# and return it as binary

我有這段代碼將html頁面轉換為doc,但是我有一個問題

Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document wordDoc = new Microsoft.Office.Interop.Word.Document();
            Object oMissing = System.Reflection.Missing.Value;
            wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            word.Visible = false;
            Object filepath = "c:\\pagina.html";
            Object confirmconversion = System.Reflection.Missing.Value;
            Object readOnly = false;
            Object saveto = "c:\\doc.pdf";
            Object oallowsubstitution = System.Reflection.Missing.Value;

            wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, ref readOnly, ref oMissing,
                                          ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                          ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                          ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            object fileFormat = WdSaveFormat.wdFormatPDF;
            wordDoc.SaveAs(ref saveto, ref fileFormat, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                           ref oMissing, ref oMissing, ref oMissing, ref oallowsubstitution, ref oMissing,
                           ref oMissing);

我怎樣才能保存在可變的bytes []中,而不保存為HDD,這是可能的?

我不確定SaveAs方法是否支持memorystream,為什么不嘗試在保存后嘗試將文件讀取為字節,然后將其從硬盤驅動器中刪除。 下面的行將讀取文件為字節

 byte[] pdfFile = System.IO.File.ReadAllBytes((string)saveto);

您可以將文檔寫入MemoryStream並從MemoryStream中讀取它-讀取方法類似於FileStream的讀取方法。 因此,不涉及硬盤驅動器-您已經有了byte [],我想您想將其寫入HTTP-Header中進行下載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM