2016-06-28 32 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