Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何连接两字节数组_C#_Arrays_Pdf_Memorystream - Fatal编程技术网

C# 如何连接两字节数组

C# 如何连接两字节数组,c#,arrays,pdf,memorystream,C#,Arrays,Pdf,Memorystream,我使用这种方法合并现有的pdf文档,并在内存中(而不是物理内存)添加新的pdf文档 问题是,我有两个内存流,一个用于合并pdf,另一个用于新文档,然后将其转换为字节数组,我想合并这两个数组 public static byte[] merge(List<String> pdf) { MemoryStream copystream; MemoryStream ms; using (ms = new MemoryStream()) { Do

我使用这种方法合并现有的pdf文档,并在内存中(而不是物理内存)添加新的pdf文档 问题是,我有两个内存流,一个用于合并pdf,另一个用于新文档,然后将其转换为字节数组,我想合并这两个数组

public static byte[] merge(List<String> pdf)
{
    MemoryStream copystream;
    MemoryStream ms;
    using (ms = new MemoryStream())
    {
        Document document;
        using (document = new Document())
        {
            using (PdfWriter wri = PdfWriter.GetInstance(document, ms))
            {
                wri.CloseStream = false;
                document.Open();
                document.SetPageSize(iTextSharp.text.PageSize.A4); // for vertical layout

                document.Add(new Paragraph("Hello"));

                document.Close();

                copystream = new MemoryStream();
                Document doc = new Document();
                using (PdfCopy copy = new PdfCopy(doc, copystream))
                {
                    copy.CloseStream = false;
                    copy.Open();
                    doc.Open();

                    // copy.AddPage(PageSize.A4, 0);
                    for (int i = 0; i < pdf.Count; ++i)
                    {
                        PdfReader reader = new PdfReader(pdf[i]);
                        // loop over the pages in that document
                        int n = reader.NumberOfPages;
                        for (int page = 0; page < n;)
                        {
                            copy.AddPage(copy.GetImportedPage(reader, ++page));
                        }
                    }

                    copy.Close();
                    copystream.CopyToAsync(ms);
                    copystream.Close();
                }
            }
        }

        byte[] mergedPdf2 = copystream.ToArray();
        byte[] mergedPdf3 = ms.ToArray();
        byte[] result = new byte[mergedPdf2.Length + mergedPdf3.Length];
        mergedPdf2.CopyTo(result, 0);
        mergedPdf3.CopyTo(result, mergedPdf2.Length);
        return result;
    }
}

在字节[]中有一个LINQ方法,它允许连接

a.Concat(b).ToArray();
您必须使用System.Linq;第一如果不想这样做,可以创建一个方法,例如:

 static byte[] Concat(byte[] a, byte[] b)
 {           
     byte[] output = new byte[a.Length + b.Length];
     for (int i = 0; i < a.Length; i++)
         output[i] = a[i];
     for (int j = 0; j < b.Length; j++)
         output[a.Length+j] = b[j];
     return output;           
 }

在字节[]中有一个LINQ方法,它允许连接

a.Concat(b).ToArray();
您必须使用System.Linq;第一如果不想这样做,可以创建一个方法,例如:

 static byte[] Concat(byte[] a, byte[] b)
 {           
     byte[] output = new byte[a.Length + b.Length];
     for (int i = 0; i < a.Length; i++)
         output[i] = a[i];
     for (int j = 0; j < b.Length; j++)
         output[a.Length+j] = b[j];
     return output;           
 }

我想看看这两个数组,你现在的代码行吗?你期望它做什么而不做什么?如果你把这两个内存流合并,结果将不是一个有效的PDF。为什么不简单地在ms中添加pdf进行复制呢?我想看看这两个数组,您当前的代码是否有效?你期望它做什么而不做什么?如果你把这两个内存流合并,结果将不是一个有效的PDF。你为什么不干脆在ms中添加pdf来复制呢?