2016-06-28 2 views
-2

Dears,Wie kann ich 1 PDF-Datei von 2 Byte-Arrays

Ich habe 2 Byte-Arrays erzeugen:

Byte[] bytes1; 
Byte[] bytes2; 

Jeder von ihnen stellt einen Bericht, der als PDF-Datei gespeichert werden.

Wie kann ich sie zusammenführen und eine pdf-Datei erzeugen.

Ich habe versucht, die folgendes zu tun:

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

Aber es did't Arbeit.

Irgendwelche Ideen-Kerle? Es ist wichtig!

+0

Nicht direkt, was Sie fragen, aber helfen soll: http://stackoverflow.com/questions/434248/is-it-possi ble-zu-programmatisch-ketten-mehrere-pdf-Dateien-vorzugsweise-von-co – BWA

+0

Danke für Ihre Hilfe. Aber das ist nicht was ich brauche. –

+0

Mehr Ideen? –

Antwort

0

Sie können iTextSharp wie folgt verwenden:

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

Kombinieren Klasse für 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; 
     } 

Ausgabe PDF:

enter image description here