Skip to main content
Skip table of contents

PNG Image Handling

iText Core 7.1.11 introduced more robust PNG image handling to address an issue which could occur when adding certain PNG images to a PDF.

Details

  • When we use iText to add a PNG image to a document, in a few use cases we end up with a PDF file which Acrobat or other PDF viewers are unable to open.
  • Certain PNG images with Indexed colorspace to a PDF document resulted in a corrupted PDF. The reason for this issue is that if during deserialization the attributes of the image's colorspace start with "/" (slash) character, then we should translate it to a PdfName object. However that is not true for some attributes. 
  • Prior to iText 7.1.11, iText parsed it incorrectly into a PdfName object, resulting in a corrupted PDF.
  • After fixing this problem , iText generates the PdfStream object with the correct image's info inside.

Below is an example demonstrating the new behavior:

JAVA

JAVA
package com.itext.ImageIssue;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;

public class FixPNGIssue {

	public static final String DEST = "src/main/resources/imageissue/output.pdf";

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		new FixPNGIssue().generatePDF(DEST);

	}

	public void generatePDF(String dest) throws IOException {
		PdfDocument pdf = new PdfDocument(new PdfWriter("Destination PDF path"));
		Document document = new Document(pdf);

		// Set the problematic base64 encoded image
		String dataSrc = "encoded png image string";
		byte[] data = Base64.decodeBase64(dataSrc);
		Image image = new Image(ImageDataFactory.create(data));
		document.add(image);
		document.close();
	}

}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
 
namespace PNGImageHandling
{
        public class FixPNGIssue
        {
     
        public static readonly string DEST = "results/test/output/pngimage/";
 
        public static void Main(String[] args)
          {
           FileInfo file = new FileInfo(DEST);
           file.Directory.Create();
            new FixPNGIssue().generatePDF(DEST);
        }
 
 
        public  void generatePDF(string dest)
        {
 
            string outPdf = dest + "pngoutput.pdf";
 
            PdfDocument pdf = new PdfDocument(new PdfWriter(outPdf));
            var document = new Document(pdf);
            //Update the encoded png image
            string dataSrc = "Update the encoded png image"
            byte[] data = Convert.FromBase64String(dataSrc);
            var image = new Image(ImageDataFactory.Create(data));
            document.Add(image);
            document.Close();
         
    }
 
}
    }





JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.