合并PDF时,第二个Byte []覆盖第一个Byte [] [英] Second Byte[] overwrites first Byte[] while merging the PDF

查看:95
本文介绍了合并PDF时,第二个Byte []覆盖第一个Byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经生成了两个PDF字节数组,并将这两个数组合并为一个字节数组.现在,当我通过Controller中的ActionMethod渲染PDF时,它仅为在Combine()方法中传递的第二个byte[]生成PDF. 例如: 1)

I've generated two PDFs byte array and combined both of those arrays into one byte array. Now when I render the PDF through ActionMethod in Controller, it generates the PDF only for the second byte[] passed in the Combine() method. Eg: 1)

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数组数据生成PDF,而pdfBytes1数组数据将被覆盖.

If I write the above code, it generates the PDF only with the pdfBytes2 array data and pdfBytes1 array data is overwritten.

2)现在,如果更改顺序并输入:

2) Now if change the order and write:

 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.

This method generates the PDF only with the pdfBytes1 array data.

我的Combine()方法代码为:

My Combine() method code is:

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[],但在打印时仅打印一个阵列的数据.请让我知道我在哪里做错了.

While debugging I can see that combinedPdfData array contains total bytes ie. pdfBytes1[] + pdfBytes2[] but while printing it prints the data only for the one array. Please let me know where I'm doing wrong.

推荐答案

您不能仅连接2个PDF字节数组,并期望结果将是有效的PDF文档.您需要一个库来创建新的PDF(output),并将两个原始PDF都合并为一个新的有效PDF.

You can't just concatenate 2 PDF byte arrays and expect that the result will be a valid PDF document. You need a library to create a new PDF (output) and that joins both original PDFs into a new valid PDF.

在iText 5(iText的旧版本)中,代码如下所示:

In iText 5 (the old version of iText), the code would look like this:

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

在iText 7(iText的新版本;推荐)中,代码如下所示:

In iText 7 (the new version of iText; recommended), the code would look like this:

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

这篇关于合并PDF时,第二个Byte []覆盖第一个Byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆