简体   繁体   中英

How to get a BaseColor from a color source in iText

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.

根据 mkl 的说法,我使用 Acrobat Reader(打印选项)或 Edge 将 PDF 文档保存到另一个文档,并且结果文档中没有空颜色。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM