C#4.0:将PDF转换为字节,反之亦然
如何将PDF文件转换为字节[],反之亦然?
// 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. byte[] bytes = System.IO.File.ReadAllBytes("myfile.pdf"); // munge bytes with whatever pdf software you want, ie 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("myfile.pdf", bytes);
最简单的方法:
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); }
或者沿着这些线路