Multiple pages from HTML

May 9, 2015 at 4:13 PM
Edited May 9, 2015 at 4:16 PM
I have a number of seperate HTML pages that I would like to convert to one single PDF Document. How can I do that?

At the moment I'm creating a seperate PDF for every page, save it to MemoryStream, open it again with PdfSharp PdfDocumentOpenMode.Import and add the pages to the "main" document.
var pdf = new PdfSharp.Pdf.PdfDocument();
// .. snip

                        foreach (var s in sheets)
                        {
                            // ... snip - sheetContent is the page HTML

                            if (!string.IsNullOrEmpty(sheetContent))
                            {
                                var pagePDF = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(sheetContent, PdfSharp.PageSize.A4);
                                using (var pageStream = new MemoryStream())
                                {
                                    pagePDF.Save(pageStream, false);
                                    pageStream.Position = 0;

                                    var importPagePDF = PdfSharp.Pdf.IO.PdfReader.Open(pageStream, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);

                                    pdf.AddPage(importPagePDF.Pages[0]);

                                    importPagePDF.Close();
                                    pagePDF.Close();
                                }
                            }
                        }