2016-06-28 49 views
-2

逗人,怎樣才能從2個字節數組

1個PDF文件我有2個字節數組:

Byte[] bytes1; 
Byte[] bytes2; 

它們中的每一個代表,可以保存爲PDF文件的報告。

如何將它們合併在一起並生成一個pdf文件。

我試着做到以下幾點:

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

但我以前不工作。

Any Ideas Guys? 重要!

+0

不能直接你問什麼,但應該有所幫助: http://stackoverflow.com/questions/434248/is-it-possi ble-to-programmatically -chain-several-pdf-files-preferred-from-co – BWA

+0

感謝您的幫助。 但這不是我所需要的。 –

+0

更多意見? –

回答

0

您可以使用iTextSharp這樣的:

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; 
      } 
     } 

結合類合併:

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; 
     } 

輸出PDF:

enter image description here