[Solved] Duplicate Image inserted in PDF file using iText 7 in ASP.Net

Last Reply 13 days ago By dharmendr

Posted 13 days ago

This is my code for convert to html data stored in a database table using iText 7 and .

the problem is that when I add an image in correspondence of the database row, this image is duplicated or tripled in the pdf file.

Instead in the database table the image is present only once.

    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            contents = new Paragraph(dt.Rows[i]["contents"].ToString())
                .SetTextAlignment(TextAlignment.JUSTIFIED)
                .SetFontSize(12);

            List<IElement> lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).ToList();

            for (int j = 0; j < lst.Count; j++)
            {
                IBlockElement element = (IBlockElement)lst[j];

                if (!String.IsNullOrEmpty(dt.Rows[i]["sID_content_image"].ToString()))
                {
                    Image image_path = new Image(ImageDataFactory
                          .Create(dt.Rows[i]["sID_content_image"].ToString()))
                          .SetHorizontalAlignment(HorizontalAlignment.CENTER)
                          .SetTextAlignment(TextAlignment.CENTER);

                    image_path.ScaleToFit(500, 500);
                    image_path.SetAutoScale(true);

                    document.Add(image_path);
                }
            }
        }

        dest = filename.ToString();
    }

 

You are viewing reply posted by: dharmendr 13 days ago.
Posted 13 days ago

Hi comunidadmexi...,

Check with below code.

if (dt.Rows.Count > 0)
{
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        contents = new Paragraph(dt.Rows[i]["contents"].ToString())
            .SetTextAlignment(TextAlignment.JUSTIFIED)
            .SetFontSize(12);

        List<IElement> lst = HtmlConverter.ConvertToElements(dt.Rows[i]["contents"].ToString()).ToList();
        for (int j = 0; j < lst.Count; j++)
        {
            IBlockElement element = (IBlockElement)lst[j];
            document.Add(element);
        }

        if (!String.IsNullOrEmpty(dt.Rows[i]["sID_content_image"].ToString()))
        {
            Image image_path = new Image(ImageDataFactory
                    .Create(dt.Rows[i]["sID_content_image"].ToString()))
                    .SetHorizontalAlignment(HorizontalAlignment.CENTER)
                    .SetTextAlignment(TextAlignment.CENTER);

            image_path.ScaleToFit(500, 500);
            image_path.SetAutoScale(true);
            document.Add(image_path);
        }
    }

    dest = filename.ToString();
}