Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] Modifying image contrast and saturation inside a
 PDF

Ignacio Ruiz-de-Conejo

2007-07-11

Replies:

Hi all!

Given a PDF with gray images, I have found that an easy way to adjust its colors is to modify its Image objects. The dictionary associated with this type of object contains a key, /ColorSpace whose value is usually assigned to  "/DeviceGray". What I have done (manually) has been to substitute this value with the colorspace
  "[ /CalGray << /WhitePoint [0.9505 1.0000 1.0890] /Gamma 2.9 >>]"
and varying the value of the gamma parameter I can tweak the image to the desired density. Deleting the cross-reference table(s) and "startxref" is enough for the PDF to work properly, with better looking images. Acrobat will rebuild those tables the first time it opens the document.

 Now, with iText:
I am trying to use iText low level functions to apply the change described above (even though I´ve read iText is better suited for generation rather than modification of PDF files). So far, I am able to traverse the cross-reference table and locate all the gray Image objects contained in the PDF.

reader = new PdfReader(args[0]);
for (int i = 0; i < reader.XrefSize; i++)
{
    PdfObject pdfobj = reader.GetPdfObject(i);
    if (pdfobj != null)
    {
       if (pdfobj.IsStream())   {
           PdfStream pdfdict    = (PdfStream) pdfobj;
           PdfObject pdfsubtype = pdfdict.Get(PdfName.SUBTYPE);
           if (pdfsubtype == null) {
              continue;
           }
          if (!pdfsubtype.ToString().Equals( PdfName.IMAGE.ToString())) {
              continue;
           }
           PdfObject pdfcolorspace = pdfdict.Get(PdfName.COLORSPACE);
          if (pdfcolorspace.ToString().Equals(PdfName.DEVICEGRAY.ToString ())) {

// Act here!
//    PdfArray GrayObj;
//  ...
//    pdfdict.Put(PdfName.COLORSPACE, GrayObj);

          }
      }
   }


My next step will be building the GrayObj object, that defines the new color space; it does not look difficult to do. But what I am not sure how to do is the writer part of the document. I mean, how to copy or stamp the new file so that the original file, obtained through the reader, is maintained, except for the Image objects, that I want to modify as explained above. I am assuming this is feasible with iTextSharp, isn´t it?

Any help?
 Thanks,
         Ignacio


PS:
 I have not seen in iTextSharp the tools that exist in the Java version of iText. I´m particulary interested in the PDF Analyser / Treeviewer. Hints?
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
©2008 junlu.com - Jax Systems, LLC, U.S.A.