0

I've generated two PDFs byte array and combined both of those arrays into one byte array. Now when I render the PDF through ActionMethod in Controller, it generates the PDF only for the second byte[] passed in the Combine() method. Eg: 1)

public ActionResult ShowPdf(string id1, string id2)
        {
            byte[] pdfBytes1 = CreatePdf1(id1);
            byte[] pdfBytes2 = CreatePdf2(id2);
            byte[] combinedPdfData = Combine(pdfBytes1, pdfBytes2);
            return File(combinedPdfData, "application/pdf");
        }

If I write the above code, it generates the PDF only with the pdfBytes2 array data and pdfBytes1 array data is overwritten.

2) Now if change the order and write:

 public ActionResult ShowPdf(string id1, string id2)
        {
            byte[] pdfBytes1 = CreatePdf1(id1);
            byte[] pdfBytes2 = CreatePdf2(id2);
            byte[] combinedPdfData = Combine(pdfBytes2, pdfBytes1);
            return File(combinedPdfData, "application/pdf");
        }

This method generates the PDF only with the pdfBytes1 array data.

My Combine() method code is:

public static byte[] Combine(byte[] first, byte[] second)
        {
            byte[] ret = new byte[first.Length + second.Length];
            Buffer.BlockCopy(first, 0, ret, 0, first.Length);
            Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
            return ret;
        }

While debugging I can see that combinedPdfData array contains total bytes ie. pdfBytes1[] + pdfBytes2[] but while printing it prints the data only for the one array. Please let me know where I'm doing wrong.

user1547554
  • 441
  • 2
  • 8
  • 16
  • As you have tagged your question [tag:itext]: You can use iText(Sharp) to *merge* pdf files. – mkl Sep 19 '17 at 04:07

2 Answers2

2

You are going wrong with thinking you can concatenate two pdf files by concatenating the bytes.

You will need to use a pdf authoring library to read in the two old documents and combine them to a new document.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
2

You can't just concatenate 2 PDF byte arrays and expect that the result will be a valid PDF document. You need a library to create a new PDF (output) and that joins both original PDFs into a new valid PDF.

In iText 5 (the old version of iText), the code would look like this:

Document doc = new Document();
PdfCopy copy = new PdfCopy(document, output);
document.Open();
PdfReader reader1 = new PdfReader(pdfBytes1);
copy.AddDocument(reader1);
PdfReader reader2 = new PdfReader(pdfBytes2);
copy.AddDocument(reader2);
reader1.Close();
reader2.Close();
document.Close(); 

In iText 7 (the new version of iText; recommended), the code would look like this:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfMerger merger = new PdfMerger(pdf);
PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1));
merger.Merge(firstSourcePdf, 1, firstSourcePdf.GetNumberOfPages());
//Add pages from the second pdf document
PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2));
merger.Merge(secondSourcePdf, 1, secondSourcePdf.GetNumberOfPages());
firstSourcePdf.Close();
secondSourcePdf.Close();
pdf.Close();
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165