如何将缓冲区转换为pdf并下载 [英] How to convert the buffer to pdf and download

查看:91
本文介绍了如何将缓冲区转换为pdf并下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#代码将文件流中的数据转换为pdf,并使用c#代码和

下载。



我尝试过:



 byte [] fileContents = Docx(); 
using(FileStream file = new FileStream(wordPath,FileMode.Create))
{
file.Write(fileContents,0,fileContents.Length);
file.Close();
}





i希望将fileContents中的数据转换为pdf

解决方案

你不能 - 或者至少不能在合理的时间范围内。

DOCX是一种基于Zipped XML的文档格式,PDF与它完全不同。

您不能将原始DOCX作为字节流并说这是PDF - 您必须将DOCX文件处理为文档并将该文档重新解释为PDF文件。 />


使用插件或API - 尝试自己动手并不是一个好主意,因为文档格式不是很简单所以涉及太多理解和处理。


这是可能的,但你需要第三方库。在这种情况下,您的代码可能如下所示:



  byte  [ ] fileContents = Docx(); 
使用(FileStream file = new FileStream(wordPath,FileMode.Create))
{
file.Write(fileContents, 0 ,fileContents.Length);
file.Close();
}

// 构造函数的虚拟对象,因为你不会在外面合并数据
对象 myDataObject = new 对象();

DocumentGenerator dg = new DocumentGenerator(myDataObject);
MemoryStream outputDocumentStream = new MemoryStream();
dg.GenerateDocument(file,outputDocumentStream,Pdf);



如果您认为这对我们有帮助,请参阅更多示例


How to convert the data in the file stream to pdf with out any plugin using c# code and
download.

What I have tried:

byte[] fileContents = Docx();
         using (FileStream file = new FileStream(wordPath, FileMode.Create))
         {
             file.Write(fileContents, 0, fileContents.Length);
             file.Close();
         }



i want to convert the data in fileContents to pdf

解决方案

You can't - or at least not in a reasonable time frame.
DOCX is a Zipped XML based document format, PDF is nothing like it.
You can't just take a "raw" DOCX as a stream of bytes and say "this is a PDF" - you have to process the DOCX file into a document and reinterpret that document as a PDF file.

Use a plug in, or an API - it really isn't a good idea to try and do it yourself, there is just too much involved as neither document format is trivial to understand and process.


It is possible, but you will need 3rd party library. In that case your code could look like this:

byte[] fileContents = Docx();
using (FileStream file = new FileStream(wordPath, FileMode.Create))
{
	file.Write(fileContents, 0, fileContents.Length);
	file.Close();
}

// dummy object for constructor, since you will not merge outside data
object myDataObject = new object();

DocumentGenerator dg = new DocumentGenerator(myDataObject); 
MemoryStream outputDocumentStream = new MemoryStream(); 
dg.GenerateDocument(file, outputDocumentStream, Pdf); 


See more examples if you think this could be helpful for you.


这篇关于如何将缓冲区转换为pdf并下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆