2017-09-18 5 views
0

私は2つのPDFバイト配列を生成し、それらの配列の両方を1バイト配列に結合しました。今度はActionMethodでPDFをControllerにレンダリングすると、Combine()メソッドに渡された2番目のbyte[]のPDFのみが生成されます。 例:私は上記のコードを記述する場合 1)2番目のバイト[]は最初のバイト[]を上書きしてPDFをマージします

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

は、それだけpdfBytes2配列データとpdfBytes1配列データが上書きされるとPDFを生成します。

2)次に順序を変更して書く場合:

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

この方法のみpdfBytes1アレイデータとPDFを生成します。

コンバインマイ()メソッドのコードは次のとおりです。

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

私はcombinedPdfData配列、すなわちバイトの合計が含まれていることを見ることができますデバッグ中。 pdfBytes1[] + pdfBytes2[]ですが、印刷中には1つの配列のデータのみが出力されます。私が間違っていることを教えてください。

+0

あなたの質問にタグを付けたように[tag:itext]:iText(シャープ)を使用して* pdfファイルをマージすることができます。 – mkl

答えて

2

2つのPDFバイト配列を連結して、結果が有効なPDFドキュメントになるとは限りません。新しいPDF(output)を作成するにはライブラリが必要です。オリジナルのPDFを新しい有効なPDFに結合します。 iTextの5(iTextのの古いバージョン)で

、コードは次のようになります(iTextのの新しいバージョン;推奨)iTextは7で

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(); 

、コードは次のようになります。

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(); 
+0

ありがとう。それはうまくいった:-) – user1547554

2

バイトを連結して2つのpdfファイルを連結することができないと考えると間違っています。

2つの古いドキュメントを読み込んで新しいドキュメントに結合するには、pdfオーサリングライブラリを使用する必要があります。

関連する問題