1

I am converting my Html to PDF using ITextSharp, the problem arises when i try to insert image to HTML page but when the PDF is downloaded i cannot see the Image in it. I don't see any errors, code works fine but I cannot see the Image in the downloaded PDF. The path of PDF is right as it works in HTML PAGE, I even tried to change the dimensions of the Image & canvas using HTML but that didn't solve the problem.

Coding to convert HTML to PDF is:

public void DownloadAsPDF()
    {
        try
        {
            string case_id = Request.Form["case_id"];
            string strHtml = string.Empty;
            string pdfFileName = Request.PhysicalApplicationPath + "\\files\\" + case_id + ".pdf";


            string template = System.IO.File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));

Code to Insert Image Starts

            Base64ToImage().Save(Server.MapPath("\\files\\" + case_id + "stu.jpg"));

            Base64ToInsImage().Save(Server.MapPath("\\files\\" + case_id + "ins.jpg"));
            string facultysign = "/files/CS00022904stu.jpg";
            string stusign = "/files/CS00022904stu.jpg";
            template = template.Replace("[stusign]", facultysign);
            template = template.Replace("[facultysign]", stusign);

Code to Insert Image Ends

            CreatePDFFromHTMLFile(template, pdfFileName);
 }
catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }



    public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
    {
        try
        {
            object TargetFile = FileName;

            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;

            GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
            GeneratePDF.HtmlPdfPage first = builder.AddPage();
            first.AppendHtml(HtmlStream);
            byte[] file = builder.RenderPdf();
            System.IO.File.WriteAllBytes(TargetFile.ToString(), file);

            iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
            ModifiedFileName = TargetFile.ToString();
            ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");

            iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
            reader.Close();
            if (System.IO.File.Exists(TargetFile.ToString()))
                System.IO.File.Delete(TargetFile.ToString());
            FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
            System.IO.File.Copy(ModifiedFileName, FinalFileName);
            if (System.IO.File.Exists(ModifiedFileName))
                System.IO.File.Delete(ModifiedFileName);

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

*HMTL Coding to Insert Image *

 <p><strong>Faculty Signature: </strong></p>
   <img src='[stusign]' />

 <p><strong>Faculty Signature: </strong></p>
   <img src='[facultysign]' />
Naive
  • 345
  • 2
  • 6
  • 18
  • 1
    Which version of iTextSharp are you using? I was also expecting to see `XMLWorker` in your code. It looks like you are using code from this page: http://www.codescratcher.com/asp-net/html-pdf-using-itextsharp-library-asp-net/ but the `GeneratePDF.cs` file from that website is not a part of iTextSharp. – Amedee Van Gasse May 02 '17 at 14:13
  • If you're having issues with figuring out why certain content is (or is not) in your pdf document, consider using RUPS (downloadable from the iText repositories). It allows you to look at the internal structure of the pdf document. – Joris Schellekens May 02 '17 at 14:14
  • @AmedeeVanGasse yes i am using the code from the website which you mentioned, so what shall i do now ? – Naive May 02 '17 at 14:46
  • @JorisSchellekens i checked the document in RUPS. It shows me something like. **7 0 obj<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]>>/Parent 5 0 R/MediaBox[0 0 595 842]>>** – Naive May 02 '17 at 15:02
  • @JorisSchellekens (Student Signature: ) Tj 0 -18 Td /F1 12 Tf ( ) Tj 0 -18 Td /F3 10 Tf 0 -18 Td /F1 12 Tf ( ) Tj 0 -18 Td () Tj 0 -18 Td ( ) Tj ( ) Tj 0 -18 Td ( ) Tj ( ) Tj 0 -18 Td ( ) Tj ET Q – Naive May 02 '17 at 15:11
  • @naive please answer my other question. – Amedee Van Gasse May 02 '17 at 15:12
  • @AmedeeVanGasse 5.5.11 version – Naive May 02 '17 at 15:17
  • Your code (in `GeneratePDF.cs`) uses `HTMLWorker`, which is obsolete. Use `XMLWorker` instead. – Amedee Van Gasse May 02 '17 at 15:57
  • @AmedeeVanGasse is there a link where i can find it. Really Appreciate your help. Do i replace the whole content ? – Naive May 02 '17 at 16:16
  • You can find it on NuGet. – Amedee Van Gasse May 02 '17 at 16:21
  • 1
    [http://stackoverflow.com/questions/35594030/how-can-i-use-itext-to-convert-html-with-images-and-hyperlinks-to-pdf](http://stackoverflow.com/questions/35594030/how-can-i-use-itext-to-convert-html-with-images-and-hyperlinks-to-pdf) – kuujinbo May 02 '17 at 17:16
  • @AmedeeVanGasse i went through NuGet and and downloaded itextsharp.xmlworker.dll file, what changes i shall do ? I tried other method as well but cannot see image in that as well – Naive May 02 '17 at 21:05

2 Answers2

0

Resizing the image solved the problem for me (Even resizing the canvas will do the trick).

Naive
  • 345
  • 2
  • 6
  • 18
-1

pdfptable and pdfpcell from code behind can be used to locate the images in PDF.