Ejemplo n.º 1
0
        public static async Task <byte[]> AggregatePdfsAsync(this IEnumerableAsync <byte[]> pdfs)
        {
            var composedPdf = await pdfs.AggregateAsync(
                new PdfDocument(),
                (outputPdf, pdfBytes) =>
            {
                if (pdfBytes.IsDefaultNullOrEmpty())
                {
                    return(outputPdf);
                }

                var stream = new MemoryStream(pdfBytes);
                var doc    = PdfReader.Open(stream, PdfDocumentOpenMode.Import);
                foreach (var page in doc.Pages)
                {
                    outputPdf.AddPage(page);
                }
                return(outputPdf);
            });

            var concatenatedStream = new MemoryStream();

            composedPdf.Save(concatenatedStream);
            return(await concatenatedStream.ToBytesAsync());
        }