How to combine the exported PDF with another PDF file

To combine a PDF exported by the Bold Reports® ASP.NET Core Report Writer with another PDF file, such as a license agreement template, merge the two PDF streams with the Syncfusion® PDF merge documents feature. The following sample controller exports the report to PDF, merges it with the template, and adds page numbers.

 public IActionResult Export()
        {
            // Export the RDL To PDF.
            FileStream mainReportStream = new FileStream(_hostingEnvironment.WebRootPath + @"\Reports\Sales Order Detail.rdl", FileMode.Open, FileAccess.Read);
            MemoryStream reportStream = new MemoryStream();
            mainReportStream.CopyTo(reportStream);
            reportStream.Position = 0;
            mainReportStream.Close();

            //Template File
            FileStream templateStream = new FileStream(_hostingEnvironment.WebRootPath + @"\PDF\Template.pdf", FileMode.Open, FileAccess.Read);

            BoldReports.Writer.ReportWriter writer = new BoldReports.Writer.ReportWriter();
            MemoryStream memoryStream = new MemoryStream();

            writer.LoadReport(reportStream);
            writer.Save(memoryStream, BoldReports.Writer.WriterFormat.PDF);

            // Download the generated export document to the client side.
            memoryStream.Position = 0;
            FileStreamResult fileStreamResult = new FileStreamResult(memoryStream, "application/pdf");

            var exportedPdfSream = fileStreamResult.FileStream;

            // Combine the Exported PDF and template
            PdfDocument finalDoc = new PdfDocument();

            // Creates a PDF stream for merging
            Stream[] streams = { exportedPdfSream, templateStream };

            // Merges the PDF document.
            PdfDocumentBase.Merge(finalDoc, streams);

            //Save the document into a stream
            MemoryStream combinestream = new MemoryStream();

            finalDoc.Save(combinestream);

            combinestream.Position = 0;

            FileStreamResult fileStreamResult1 = new FileStreamResult(combinestream, "application/pdf");

            var combinePDF = fileStreamResult1.FileStream;

            // Add Page number for the combined PDF
            PdfLoadedDocument loadedDoc = new PdfLoadedDocument(combinePDF);

            //Set the font.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 12f);

            //Create a page number field.
            PdfPageNumberField pageNumber = new PdfPageNumberField(font, PdfBrushes.Black);

            //Create a page count field.
            PdfPageCountField count = new PdfPageCountField(font, PdfBrushes.Black);

            //Add the fields in composite fields.
            PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.Black, "Page {0} of {1}", pageNumber, count);

            for (int i = 0; i < loadedDoc.Pages.Count; i++)
            {
                //Draw the composite field.
                compositeField.Draw(loadedDoc.Pages[i].Graphics, new PointF(loadedDoc.Pages[i].Size.Width / 2 - 20, loadedDoc.Pages[i].Size.Height - 20));
            }
            MemoryStream combineWithPagenumber = new MemoryStream();

            loadedDoc.Save(combineWithPagenumber);
            //Save the document.

            combineWithPagenumber.Position = 0;

            //Close the document.
            loadedDoc.Close(true);
            finalDoc.Close(true);

            string contentType = "application/pdf";

            //Define the file name
            string fileName = "Combinewithpagenumber.pdf";

            //Creates a FileContentResult object by using the file contents, content type, and file name
            return File(combineWithPagenumber, contentType, fileName);
        }