0

I am trying to change background color of all images of pdf using Itextshap. How can i loop through all images and change background color of the images

I used below code to extract pdf images

 public static void ExtractImagesFromPDF(string sourcePdf, string outputPath)
{
    // NOTE:  This will only get the first image it finds per page.
    PdfReader pdf = new PdfReader(sourcePdf);
    RandomAccessFileOrArray raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf);

    try
    {
        for (int pageNumber = 1; pageNumber <= pdf.NumberOfPages; pageNumber++)
        {
            PdfDictionary pg = pdf.GetPageN(pageNumber);

            // recursively search pages, forms and groups for images.
            PdfObject obj = FindImageInPDFDictionary(pg);
            if (obj != null)
            {

                int XrefIndex = Convert.ToInt32(((PRIndirectReference)obj).Number.ToString(System.Globalization.CultureInfo.InvariantCulture));
                PdfObject pdfObj = pdf.GetPdfObject(XrefIndex);
                PdfStream pdfStrem = (PdfStream)pdfObj;
                byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)pdfStrem);
                if ((bytes != null))
                {
                    using (System.IO.MemoryStream memStream = new System.IO.MemoryStream(bytes))
                    {
                        memStream.Position = 0;
                        System.Drawing.Image img = System.Drawing.Image.FromStream(memStream);
                        // must save the file while stream is open.
                        if (!Directory.Exists(outputPath))
                            Directory.CreateDirectory(outputPath);

                        string path = Path.Combine(outputPath, String.Format(@"{0}.jpg", pageNumber));
                        System.Drawing.Imaging.EncoderParameters parms = new System.Drawing.Imaging.EncoderParameters(1);
                        parms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, 0);
                        System.Drawing.Imaging.ImageCodecInfo jpegEncoder = Utilities.GetImageEncoder("JPEG");
                        img.Save(path, jpegEncoder, parms);
                    }
                }
            }
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        pdf.Close();
        raf.Close();
    }


}

 private static PdfObject FindImageInPDFDictionary(PdfDictionary pg)
{
    PdfDictionary res =
        (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));


    PdfDictionary xobj =
      (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
    if (xobj != null)
    {
        foreach (PdfName name in xobj.Keys)
        {

            PdfObject obj = xobj.Get(name);
            if (obj.IsIndirect())
            {
                PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);

                PdfName type =
                  (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));

                //image at the root of the pdf
                if (PdfName.IMAGE.Equals(type))
                {
                    return obj;
                }// image inside a form
                else if (PdfName.FORM.Equals(type))
                {
                    return FindImageInPDFDictionary(tg);
                } //image inside a group
                else if (PdfName.GROUP.Equals(type))
                {
                    return FindImageInPDFDictionary(tg);
                }

            }
        }
    }

    return null;

}

Many thanks in advance

Narasappa
  • 546
  • 1
  • 9
  • 31
  • Please share your code – Faraz Mar 22 '16 at 13:17
  • @ FAЯAƸ i used http://stackoverflow.com/questions/5945244/extract-image-from-pdf-using-itextsharp to extract images and unable to change background color – Narasappa Mar 22 '16 at 13:33
  • Input file : https://www.dropbox.com/s/u7mdz1q6su4hxmc/input%20file%20uv.pdf?dl=0 Expected Output :https://www.dropbox.com/s/etcwlzejuv607t9/UV_output.pdf?dl=0 – Narasappa Mar 22 '16 at 13:34
  • There's a three step process that you need to do. First, extract all the images, second recolor them, third replace the originals. [The first and third steps are done in this question](http://stackoverflow.com/a/8751517/231316), the third step is whatever your own logic is. Please note, this won't every image but often gets most. – Chris Haas Mar 22 '16 at 13:37
  • *change background color of the images* - How do you intend to recognize the *background color* of an image? – mkl Mar 23 '16 at 08:10
  • @mkl just i want change background color as black. – Narasappa Mar 23 '16 at 10:17
  • But what *is* the background of the image? How do you recognize the pixels currently making up the background? – mkl Mar 23 '16 at 10:23

0 Answers0