C# 4.0: Преобразуйте PDF в байт [] и наоборот

Как я преобразовываю файл PDF в байт [] и наоборот?

55
задан jjxtra 25 June 2014 в 10:11
поделиться

2 ответа

// loading bytes from a file is very easy in C#. The built in System.IO.File.ReadAll* methods take care of making sure every byte is read properly.
// note that for Linux, you will not need the c: part
// just swap out the example folder here with your actual full file path
string pdfFilePath = "c:/pdfdocuments/myfile.pdf";
byte[] bytes = System.IO.File.ReadAllBytes(pdfFilePath);

// munge bytes with whatever pdf software you want, i.e. http://sourceforge.net/projects/itextsharp/
// bytes = MungePdfBytes(bytes); // MungePdfBytes is your custom method to change the PDF data
// ...
// make sure to cleanup after yourself

// and save back - System.IO.File.WriteAll* makes sure all bytes are written properly.
System.IO.File.WriteAllBytes(pdfFilePath, bytes);
121
ответ дан 7 November 2019 в 07:14
поделиться

Самый простой способ:

byte[] buffer;
using (Stream stream = new IO.FileStream("file.pdf"))
{
   buffer = new byte[stream.Length - 1];
   stream.Read(buffer, 0, buffer.Length);
}

using (Stream stream = new IO.FileStream("newFile.pdf"))
{
   stream.Write(buffer, 0, buffer.Length);
}

Или что-то в этом роде ...

-3
ответ дан 7 November 2019 в 07:14
поделиться
Другие вопросы по тегам:

Похожие вопросы: