Reputation: 6776
I used iTextSharp in order to merge two documents from byte arrays like this:
using (MemoryStream ms = new MemoryStream())
using (Document doc = new Document())
using (PdfSmartCopy copy = new PdfSmartCopy(doc, ms))
{
// Open document
doc.Open();
// Create reader from bytes
using (PdfReader reader = new PdfReader(pdf1.DocumentBytes))
{
//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
// Create reader from bytes
using (PdfReader reader = new PdfReader(pdf2.DocumentBytes))
{
//Add the entire document instead of page-by-page
copy.AddDocument(reader);
}
// Close document
doc.Close();
// Return array
return ms.ToArray();
}
I am unable to convert this to iText 7 since a bunch of stuff changed. Would someone be that kind to give me the right directions? Thanks a lot in advance!
Upvotes: 4
Views: 7593
Reputation: 6776
I figured it out after some research. This is the solution (iText7) in case anyone is also facing trouble converting the code:
using (MemoryStream ms = new MemoryStream())
using (PdfDocument pdf = new PdfDocument(new PdfWriter(ms).SetSmartMode(true)))
{
// Create reader from bytes
using (MemoryStream memoryStream = new MemoryStream(pdf1.DocumentBytes))
{
// Create reader from bytes
using (PdfReader reader = new PdfReader(memoryStream))
{
PdfDocument srcDoc = new PdfDocument(reader);
srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdf);
}
}
// Create reader from bytes
using (MemoryStream memoryStream = new MemoryStream(pdf2.DocumentBytes))
{
// Create reader from bytes
using (PdfReader reader = new PdfReader(memoryStream))
{
PdfDocument srcDoc = new PdfDocument(reader);
srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdf);
}
}
// Close pdf
pdf.Close();
// Return array
return ms.ToArray();
}
Upvotes: 6