0

I found this example that works that parse HTML to PDF using iTextSharp enter link description here

I need to add on the document a header and footer with images from a folder of my project how can I do that using this function?

using iTextSharp.text;  
using iTextSharp.text.html.simpleparser;  
using iTextSharp.text.pdf;  

public class PdfController : Controller
{     
    [Route("/htmlpdf")]
    public FileStreamResult DownloadPDF()
    {
        string HTMLContent = "Hello <b>World</b>";// Put your html tempelate here
        
        MemoryStream ms = new MemoryStream();
        TextReader txtReader = new StringReader(HTMLContent);

        // 1: create object of a itextsharp document class  
        Document doc = new Document(PageSize.A4, 25, 25, 25, 25);

        // 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file  
        PdfWriter PdfWriter = PdfWriter.GetInstance(doc, ms);
        PdfWriter.CloseStream = false;

        // 3: we create a worker parse the document  
        HTMLWorker htmlWorker = new HTMLWorker(doc);

        // 4: we open document and start the worker on the document  
        doc.Open();
        htmlWorker.StartDocument();


        // 5: parse the html into the document  
        htmlWorker.Parse(txtReader);

        // 6: close the document and the worker  
        htmlWorker.EndDocument();
        htmlWorker.Close();
        doc.Close();

        ms.Flush(); //Always catches me out
        ms.Position = 0; //Not sure if this is required

        return File(ms, "application/pdf", "HelloWorld.pdf");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michaelweston
  • 149
  • 1
  • 11

0 Answers0