Convert byte array to pdf with c # code
I have a table with much more binary type columns. I need to convert a byte array to pdf. I am writing it with C # codes like this:
this.Cursor = Cursors.WaitCursor;
using (CMEntities myEntity = new CMEntities())
{
myEntity.ContextOptions.LazyLoadingEnabled = false;
var r = (from b in myEntity.FileDatas
orderby b.Oid
where b.Status == null
select b).Skip(Directory.GetFiles(@"D:\test\FromBinary\", "*.*", SearchOption.AllDirectories).Length).Take(500);
button5.Text = Directory.GetFiles(@"D:\test\FromBinary\", "*.*", SearchOption.AllDirectories).Length.ToString();
int i = 1;
try
{
FileSystemStoreObject fileSystem = new FileSystemStoreObject();
foreach (var item in r)
{
byte[] a = item.Content;
File.WriteAllBytes(@"D:\test\FromBinary\" + item.Oid + "-" + item.FileName, a);
fileSystem.Oid = item.Oid;
fileSystem.FileName = item.FileName;
fileSystem.Size = item.size;
item.Status = 1;
myEntity.FileSystemStoreObjects.AddObject(fileSystem);
if (i == 500)
{
break;
}
i++;
fileSystem = new FileSystemStoreObject();
}
myEntity.SaveChanges();
}
catch { }
}
but when my program starts running some PDF files won't open. I am getting the error that this file is demaged or .... How can I get a useful pdf file? Please help me.
source to share
If some people have problems with Pdf too, I suggest downloading the PDF spec .
Indeed, you could see that the PDF file must start with %PDF-
plus version, for example %PDF-1.4
and end with %%EOF
, and the previous two lines are byte offset and startxref
... If you look at the generated PDF using Notepad ++, you can determine whether whether your file or not and which part is missing.
source to share