简体   繁体   English

直接将docx stream转换为pdf

[英]Convert docx stream to pdf directly

I need to convert any file coming from web response into.pdf format, I'm currently getting it word docx file format from the URL and saving it into memory stream so i can later insert it in it's designated library.我需要将来自 web 响应的任何文件转换为 .pdf 格式,我目前正在从 URL 获取 word docx 文件格式并将其保存到 memory stream 中,以便稍后将其插入指定的库中。

The problem I'm facing now is that I'm saving my docx files directly into.pdf by putting an extension at the end which obviously ends up not opening the file later, So i'm trying to convert my memory stream into pdf directly.我现在面临的问题是我将我的 docx 文件直接保存到 .pdf 中,方法是在末尾放置一个扩展名,这显然最终不会在以后打开文件,所以我试图将我的 memory stream 直接转换为 pdf .

Here is my piece of code that i tried to convert the the stream to.pdf but it looks like the file isn't getting converted correctly.这是我尝试将 stream 转换为 .pdf 的一段代码,但看起来该文件未正确转换。

private Stream DownloadFromUrl(string url)
        {
            var webRequest = WebRequest.Create(url);
            webRequest.Credentials = CredentialCache.DefaultNetworkCredentials;
            webRequest.PreAuthenticate = true;
            webRequest.UseDefaultCredentials = true;
            //EventLogUtility.LogInformationMessage(DocumentURL);
            string message = string.Empty;
            using (Stream outputStream = new MemoryStream())
            {
                using (var response = webRequest.GetResponse())
                {

                    using (var content = response.GetResponseStream())
                    {
                        var memory = new MemoryStream();
                        content.CopyTo(memory);
                        Document doc = new Document(memory);
                        doc.Save(memory, SaveFormat.Pdf);
                        return memory;
                    }
                }
            }
        }

If the content in the stream is actually in the Microsoft Word file format (and not just plain text), then you need to map the format to the file format for PDF. I know there is a 'Print to PDF' function available in Word, you could try looking into that.如果 stream 中的内容实际上是 Microsoft Word 文件格式(而不仅仅是纯文本),那么您需要将 map 格式转换为 PDF 的文件格式。我知道 Word 中有一个“打印为 PDF”function ,您可以尝试调查一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM