I am using the code below to parse my HTML into a PDF Document.

However I have found that it works initially at overwriting documents as required unless I try to overwrite one with less content than the original, in these cases i suspect that redundant pages are not being deleted and that also ithe actual PDF will not open (as if it is corrupt)
Code:
    public void GeneratePDFDoc(strText)
    {
          Document doc = new Document(PageSize.A4, 50, 50, 50, 50);
               PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath(ConfigurationManager.AppSettings["PDFPath"].Trim()) +"/" + CourseCode + ".pdf", FileMode.OpenOrCreate));
      
        doc.AddDocListener(writer);
        doc.Open();




            String htmlText = strText;
              List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(htmlText), null);

          for (int k = 0; k < htmlarraylist.Count; k++)
            {
                doc.Add((IElement)htmlarraylist[k]);
            } 
                      
            doc.Close();
     
    }
In addition to this I actually want to create the PDF from a template so rather than opening and modifying the PDF I actually just want to overwrite it completely.

I understand I need to use the stamper methods for this but dont see how I can fit in my HTML parser text.

In addition to this I also want to insert the images refered to in the HTML image tags.

Any help on what methods to use appreciated.