如何将bytearray保存为PDF? [英] How to save bytearray to PDF?

查看:121
本文介绍了如何将bytearray保存为PDF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,其中要求是存储任何类型的字节数组[即doc,jpg,pdf,xps等]仅限pdf格式。





我只接收字节数组。



我不知道这个字节数组生成的文件类型。



我怎么能将此字节数组仅保存为PDF格式?



我尝试过:



I'm working on a project in which requirement is to store byte array of any type [i.e. doc, jpg, pdf, xps etc] to pdf format only.


I'll receive byte array only.

I'll not have any knowledge of from which type of file this byte array is generated.

How can i save this byte array to PDF format only?

What I have tried:

File.WriteAllBytes(path+"/file.pdf", bytes);





如果只从pdf文件创建字节数组,则代码工作正常。



above code works fine if byte array is created from pdf file only.

推荐答案

您必须知道传递的数据类型。如果没有这些信息,你就无法做任何有用的事情。



如果您知道数据代表某种文件类型,您可以尝试检测类型。在Linux中,有文件命令使用 magic(5): file命令的魔术模式文件 - Linux手册页 [ ^ ]检测文件类型。使用Windows,您必须实现类似的功能(或在Web上搜索现有实现)。



一旦确定了类型,就可以将数据保存为具有相应扩展名的文件并使用特定类型的转换器来创建PDF文件。



PDF文件例如以字符串%PDF-开头。如果您的数据以这些字符开头,您可以认为它是PDF文件并且可以这样存储。
You have to know the type of data being passed. Without that information you can't do anything useful.

If you know that the data represents some kind of file type you can try to detect the type. With Linux there is the file command that uses the magic(5): file command's magic pattern file - Linux man page[^] to detect file types. With Windows you have to implement something similar (or search the web for existing implementations).

Once you identified the type you can save the data as file with the appropriate extension and use a type specific converter to create a PDF file.

PDF files for example begin with the string "%PDF-". If your data begins with these characters you can assume that it is a PDF file and can be stored as such.


这篇关于如何将bytearray保存为PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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