0

I have 2 byte arrays:

Byte[] bytes1; 
Byte[] bytes2;

Each one of them represents a report that could be saved as a PDF file.

How can I merge them together and produce one PDF file.

I tried to do the following:

byte[] newByteArray2 = bytes1.Concat(bytes2).ToArray();
System.IO.File.WriteAllBytes("C://hello.pdf", newByteArray2);

But it did't work.

tomRedox
  • 28,092
  • 24
  • 117
  • 154
Amr Deif
  • 103
  • 1
  • 14
  • Not directly what you asking but should help: http://stackoverflow.com/questions/434248/is-it-possible-to-programmatically-chain-several-pdf-files-preferably-from-co – BWA Jun 28 '16 at 09:03
  • Thanks for your help. But this is not what i need. – Amr Deif Jun 28 '16 at 09:14
  • *Each one of them represents a report that could be saved as a pdf file.* - do you mean that each byte array actually contains a PDF? In that case you'll need a PDF library with merging / page copying capabilities. – mkl Jun 29 '16 at 10:37
  • 1
    I looked at the first comment and when I read your question, it looks as *exactly what you need*, so if you want a better answer, you'll have to post a better question. – Bruno Lowagie Jun 29 '16 at 13:05

1 Answers1

0

You can use iTextSharp like this:

private void printBytes()
        {

            string fileName = @"D:\Byte.pdf";

            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            Byte[] bytes1 = { 0x01, 0x20, 0x20, 0x20 };
            Byte[] bytes2 = { 0x31, 0x32, 0x33 };
            Byte[] bytes3 = Combine(bytes1, bytes2);

            string result = string.Empty;
            for (int i = 0; i < bytes3.Count(); i++)
            {
                result = result + bytes3[i].ToString() + " ";
            }

            try
            {
                // Step 1: Creating System.IO.FileStream object
                using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                // Step 2: Creating iTextSharp.text.Document object
                using (Document doc = new Document())
                // Step 3: Creating iTextSharp.text.pdf.PdfWriter object
                // It helps to write the Document to the Specified FileStream
                using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
                {
                    // Step 4: Openning the Document
                    doc.Open();

                    // Step 5: Adding a paragraph
                    // NOTE: When we want to insert text, then we've to do it through creating paragraph

                    doc.Add(new Paragraph("The sequence Bytes:"));
                    doc.Add(new Paragraph(result));

                    // Step 6: Closing the Document
                    doc.Close();
                }
            }
            // Catching iTextSharp.text.DocumentException if any
            catch (DocumentException de)
            {
                throw de;
            }
        }

Combine class for merge:

private byte[] Combine(byte[] a, byte[] b)
        {
            byte[] c = new byte[a.Length + b.Length];
            System.Buffer.BlockCopy(a, 0, c, 0, a.Length);
            System.Buffer.BlockCopy(b, 0, c, a.Length, b.Length);
            return c;
        }

Output PDF:

enter image description here

Ali Soltani
  • 9,589
  • 5
  • 30
  • 55