0

I am creating a PDF Document consisting 6 images (1 Image on 1 Page) using iTextSharp.

I need to add a HTML Page as a last page after the 6th Image.

I have tried the below, but the HTML does not get added on a new page, instead gets attached immediately below the 5th Image.

Please advice how to make the html add to the last page.

Code for reference:

string ImagePath = HttpContext.Current.Server.MapPath("~/Images/");
string[] fileNames = System.IO.Directory.GetFiles(ImagePath);
string outputFileNames = "Test.pdf";
string outputFilePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Pdf/" + outputFileNames);
Document doc = new Document(PageSize.A4, 20, 20, 20, 20);                
System.IO.Stream st = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(doc, st);
doc.Open();
writer.PageEvent = new Footer();
for (int i = 0; i < fileNames.Length; i++)
{
    string fname = fileNames[i];
    if (System.IO.File.Exists(fname) && Path.GetExtension(fname) == ".png")
    {
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(fname);                        
        img.Border = iTextSharp.text.Rectangle.BOX;
        img.BorderColor = iTextSharp.text.BaseColor.BLACK;                        
        doc.Add(img);
    }
}

byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/Style1.css"));
var html = File.ReadAllText(MapPath("~/HtmlPage1.html"));

using ( var memoryStream = new MemoryStream())
{
    using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
    {
        using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
        {
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, htmlMemoryStream, cssMemoryStream);
        }
    }

    pdf = memoryStream.ToArray();
    //document.Add(new Paragraph(Encoding.UTF8.GetString(pdf)));
}

doc.NewPage();
doc.Add(new Paragraph(Encoding.UTF8.GetString(pdf)));

doc.Close();
writer.Close();

I need to add a HTML Page as a last page after the 6th Image.

Any help is appreciated

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mufaddal
  • 558
  • 5
  • 24

1 Answers1

1

In contrast to what you assume according to your code comments, pdf is not where the result will be. It remains empty:

byte[] pdf; // result will be here
...

using ( var memoryStream = new MemoryStream())
{
    ... code not accessing memoryStream ...

    pdf = memoryStream.ToArray();
    //document.Add(new Paragraph(Encoding.UTF8.GetString(pdf)));
}

doc.NewPage();
doc.Add(new Paragraph(Encoding.UTF8.GetString(pdf)));

Thus, you add the new page before adding an empty paragraph, after the converted html already has been added to the document.

Actually it is added during

XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, htmlMemoryStream, cssMemoryStream);

So you have to add the new page before that. Thus, the following replacing everything from your byte[] pdf; on should do the job:

var cssText = File.ReadAllText(MapPath("~/Style1.css"));
var html = File.ReadAllText(MapPath("~/HtmlPage1.html"));

using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
    using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
    {
        doc.NewPage();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, htmlMemoryStream, cssMemoryStream);
    }
}

doc.Close();

As an aside, don't close the writer! It implicitly is closed when the doc is closed. Closing it again does nothing at best or damage otherwise.


In a comment you claimed

but this also does not resolve the issue... the pdf content still get added after the image and then continued on new page.

So I tested the proposed change. Obviously I don't have your environment and also not your image, html, and css files. Thus, I used own ones, a small screen shot and "<html><body><h1>Test</h1><p>This is a test piece of html</p></body></html>".

With your code I get:

HtmlLikeMufaddal

With the code changed as described above I get

HtmlLikeMufaddalImproved

My impression here is that the proposed code change does resolve the issue. The html content is added on a new page.

Thus apparently your either incorrectly applied the proposed change, or you executed old code, or you inspected some old result.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Thank you for the response, but this also does not resolve the issue... the pdf content still get added after the image and then continued on new page. any other enhancements or improvements for this... – Mufaddal Dec 25 '19 at 19:37
  • @Mufaddal I tested the proposed code change and it *did* resolve the issue, the html content gets added *on a new page*, see the screen shots in my edited answer. Thus apparently your either incorrectly applied the proposed change, or you executed old code, or you inspected some old result. – mkl Dec 26 '19 at 08:41
  • Your suggested change did work, with one small minor change; where in added the { doc.NewPage();} twice due to which the HTML portion is now getting rendered correctly on new page. Thank you for the guidance. – Mufaddal Dec 26 '19 at 20:49
  • However the HTML created using this technique does not support all CSS properties i.e Fonts, text-shadow, border-radius, box-shadow etc. so to overcome this I created the HTML PDF using onlineTool on sejda.com (which had all the css properties ) and merged the two pdf's . Hope this is helpful for someone. Thanks . – Mufaddal Dec 26 '19 at 20:56
  • *"where in added the { doc.NewPage();} twice"* - this is weird. As mentioned in my answer, I tested it and it worked with a single `NewPage()` call. Which iTextSharp version do you use? I assumed the newest 5.5.x version. Probably you are using an older version and there once was a bug which is fixed in the current version? Concerning the supported CSS properties I don't know which ones are supported in iText 5. The CSS support is said to be improved in the iText 7 HTML-to-PDF utility. – mkl Dec 27 '19 at 08:57
  • I am using 5.5.13.1 version. I checked, this is the latest version of iText available. – Mufaddal Dec 28 '19 at 15:41
  • Indeed, that's pretty current. This different behaviour is really weird... – mkl Dec 29 '19 at 08:46