Home > Enterprise >  How to get a BaseColor from a color source in iText
How to get a BaseColor from a color source in iText

Time:06-29

I am parsing a PDF document with iText, and I want to know the colors for lines and rectangles in the pages. I am using this code which does the parsing:

private PdfDictionary getColorDictionary(PdfDictionary resourcesDic) {
   PdfDictionary colorDict = resourcesDic.getAsDict(PdfName.COLORSPACE);
   return colorDict;
}

public void decode(File file) throws IOException {
   PdfReader reader = new PdfReader(file.toURI().toURL());
   int numberOfPages = reader.getNumberOfPages();
   ProcessorListener listener = new ProcessorListener ();
   PdfContentStreamProcessor processor = new PdfContentStreamProcessor(listener);
   for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber  ) {
      PdfDictionary pageDic = reader.getPageN(pageNumber);
      PdfDictionary resourcesDic = pageDic.getAsDict(PdfName.RESOURCES);
      PdfDictionary colorSpaceDic = getColorDictionary(resourcesDic);
      listener.setResources(colorSpaceDic);
      processor.processContent(ContentByteUtils.getContentBytesForPage(reader, pageNumber), resourcesDic);
   } 
}

My listener has the following code, I simplified it to show only the part which gets the graphics elements in each page:

public class ProcessorListener implements ExtRenderListener {
  private PdfDictionary colorSpaceDic = null;

  public void setResources(PdfDictionary colorSpaceDic) {
     this.colorSpaceDic = colorSpaceDic;
  }

   @Override
   public void beginTextBlock() {
   }

   @Override
   public void renderText(TextRenderInfo tri) {
   }

   @Override
   public void renderImage(ImageRenderInfo iri) {
   }

   @Override
   public Path renderPath(PathPaintingRenderInfo renderInfo) {
      GraphicsState graphicsState;
      try {
         graphicsState = getGraphicsState(renderInfo);
      } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
         return null;
      }

      if ((renderInfo.getOperation() & PathPaintingRenderInfo.STROKE) != 0) {
         PdfName resource = graphicsState.getColorSpaceStroke();
         if (resource != null && colorSpaceDic != null) {
            PdfObject obj = colorSpaceDic.get(resource);
            System.err.println("STROKE: "   obj);
         }
      }
      if ((renderInfo.getOperation() & PathPaintingRenderInfo.FILL) != 0) {
         PdfName resource = graphicsState.getColorSpaceStroke();
         if (resource != null && colorSpaceDic != null) {
            PdfObject obj = colorSpaceDic.get(resource);
            System.err.println("FILL: "   obj);
         }
      }
   }
   return null;
}

This code executes correctly, but each PDFObject associated with afill or stroke is a PRIndirectReference. How to I get the BaseColor associated with this reference?

Also I tried to use the following code (for example for the Fill):

BaseColor fillColor = graphicsState.getFillColor();

But the color is always null. There are not only black shapes in the document (which I assume is the default), but also green or blue lines as well.

CodePudding user response:

Per mkl remark, I saved the PDF document to another document using Acrobat Reader (print option) or Edge, and I have not null colors in the resulting document.

  • Related