|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
Hello,
Is it possible to merge multiple file inputstreams in an ASP.NET MVC application with EO.Pdf? I have 5 file upload fields and want to merge this uploaded pdf documents single PDF stream/byte array for saving into a SQLServer Database...
regards robert
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
Yes. You can create five PdfDocument objects in an array and then pass that array to PdfDocument.Merge.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
Hi,
I do not understand - I have 5 Byte[] arrays:
System.IO.Stream Upload_1fileStream = Upload_1.InputStream; byte[] Upload_1 = new byte[Upload_1.Length];
System.IO.Stream Upload_2fileStream = Upload_2.InputStream; byte[] Upload_2 = new byte[Upload_2.Length];
System.IO.Stream Upload_3fileStream = Upload_3.InputStream; byte[] Upload_3 = new byte[Upload_3.Length];
System.IO.Stream Upload_4fileStream = Upload_4.InputStream; byte[] Upload_4 = new byte[Upload_4.Length];
System.IO.Stream Upload_5fileStream = Upload_5.InputStream; byte[] Upload_5 = new byte[Upload_5.Length];
in each byte array is an PDF - how to use EO.PDF to merge this 5 byte arrays into a singe byte array to have one PDF?
robert
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, Please follow these steps: 1. Create one PdfDocument object from each of your byte array:
Code: C#
PdfDocument doc1 = new PdfDocument(Upload_1);
....
//do the same for the other four
.....
You will have 5 PdfDocument objects after this step; 2. Put all 5 PdfDocument objects in a single array of PdfDocument:
Code: C#
PdfDocument[] docs = new PdfDocument[5];
docs[0] = doc1;
....
//do the same for the other four
....
3. Call PdfDocument.Merge:
Code: C#
PdfDocument merged = PdfDocument.Merge(docs);
Hope this is clear enough for you. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, Yes. You are right. Sorry about the mistake. Try to use byte_array and a new MemoryStream. For example:
Code: C#
MemoryStream ms = new MemoryStream(Upload_1);
PdfDocument doc = new PdfDocument(ms);
The key difference here is the current position of the stream. PdfDocument(stream) reads from the current position of the stream. So if the current position of your InputStream is at the end of the stream, then there would be nothing left for the PdfDocument to read and you will receive an error. The above code always starts from the beginning of the Stream since it is a freshly created new MemoryStream. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
after using PdfDocument DocMerged = PdfDocument.Merge(docs); to merge I want to save that docs into the database but for that I need a byte array - how to get the byte array from "DocMerged"?
robert
|
|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
Hi, I now use the following Code to save the MergeDoc to a file and also to a byte array fpr saving to the database:
Code: C#
PdfDocument DocMerged = PdfDocument.Merge(doc1, doc2);
System.IO.Stream Upload_GesamtfileStream = new System.IO.MemoryStream();
DocMerged.Save(Path.Combine(Server.MapPath("~/App_Data"), "Testen.pdf"));
DocMerged.Save(Upload_GesamtfileStream);
byte[] Upload_GesamtBytes = new byte[Upload_GesamtfileStream.Length];
Upload_GesamtfileStream.Read(Upload_GesamtBytes, 0, Convert.ToInt32(Upload_GesamtfileStream.Length));
....
//save GesamtBytes to database
.....
the problem is that the Save to file works but the file from the database is not a valid pdf - Acrobat Reader gives me an error opening the file... what's wrong with my code? robert
|
|
Rank: Newbie Groups: Member
Joined: 1/4/2016 Posts: 6
|
found the problem - have to set the Position = 0 because the stream is at the end...
Upload_GesamtfileStream.Position = 0;
robert
|
|