Hi all, I managed to get what I needed, I'm sharing my solution with you, I attach the class PdfContentStreamProcessor with my changes and below I'm posting my listener, whose interesting methods are
renderMoveToDrawOperation, renderLineToDrawOperation,
renderCurveToDrawOperation
which get coordinates in page space (that is, after having applied the
current transformation matrix).
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import com.itextpdf.text.pdf.PdfContentByte;
public class Listener implements RenderListener {
private PdfStamper stamper = null;
private int pageNum = 0;
private float border = 3;
private ArrayList<Rectangle2D> imageBoundingBoxes = null;
private ArrayList<Rectangle2D> textBoundingBoxes = null;
private GeneralPath path = null;
private double x_distanceThreshold = 30.0;
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
imageBoundingBoxes = new ArrayList<Rectangle2D>();
textBoundingBoxes = new ArrayList<Rectangle2D>();
path = new GeneralPath();
}
public Listener(PdfStamper stamper) {
this.stamper = stamper;
}
/**
* Creates a RenderListener that will look for images.
*/
public Listener() {
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
*/
public void beginTextBlock() {
}
/**
* @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
*/
public void endTextBlock() {
}
/**
* @see
com.itextpdf.text.pdf.parser.RenderListener#renderImage(com.itextpdf.text.pd
f.parser.ImageRenderInfo)
*/
public void renderImage(ImageRenderInfo renderInfo) {
}
private TextRenderInfo firstChar;
private TextRenderInfo lastChar;
private TextRenderInfo curChar;
/**
* @see
com.itextpdf.text.pdf.parser.RenderListener#renderText(com.itextpdf.text.pdf
.parser.TextRenderInfo)
*/
public void renderText(TextRenderInfo renderInfo) {
boolean drawRect = false;
if (curChar == null) {
firstChar = renderInfo;
curChar = renderInfo;
}
// if the current char is empty and the new char (in
renderInfo) is not
// empty
if ("".equals(curChar.getText().trim())
&& !"".equals(renderInfo.getText().trim()))
{
firstChar = renderInfo;
}
// if the current char is not empty and the new char (in
renderInfo) is
// empty
if (!"".equals(curChar.getText().trim())
&& "".equals(renderInfo.getText().trim())) {
lastChar = curChar;
drawRect = true;
}
// update the current char with the new one
curChar = renderInfo;
if (drawRect) {
textBoundingBoxes
.add(new
Rectangle2D.Float(firstChar.getDescentLine()
.getBoundingRectange().x, firstChar
.getDescentLine().getBoundingRectange().y,
lastChar.getDescentLine().getBoundingRectange().x
-
firstChar.getDescentLine()
.getBoundingRectange().x
+
lastChar.getDescentLine()
.getBoundingRectange().width,
lastChar.getAscentLine().getBoundingRectange().y
-
lastChar.getDescentLine()
.getBoundingRectange().y));
}
}
public void calculateCurrentImageBoundingBox() {
Rectangle2D currPathBoundingBox = path.getBounds2D();
boolean notContainedBB = true;
boolean notIntersectsBB = true;
if (imageBoundingBoxes.size() > 0) {
int currBoundingBoxIndex = 0;
for (Rectangle2D currBoundingBox :
imageBoundingBoxes) {
if
(currPathBoundingBox.contains(currBoundingBox)) {
imageBoundingBoxes.set(currBoundingBoxIndex,
currPathBoundingBox);
} else if
(currBoundingBox.contains(currPathBoundingBox)) {
notContainedBB = false;
} else if
(currPathBoundingBox.intersects(currBoundingBox)) {
imageBoundingBoxes.set(currBoundingBoxIndex,
currPathBoundingBox.createUnion(currBoundingBox));
notIntersectsBB = false;
}
currBoundingBoxIndex++;
}
if (notContainedBB && notIntersectsBB) {
imageBoundingBoxes.add(currPathBoundingBox);
}
} else {
imageBoundingBoxes.add(currPathBoundingBox);
}
}
private Rectangle2D calculateItemBoundingBox(Rectangle2D textBB,
Rectangle2D imageBB){
Rectangle2D result = imageBB.createUnion(textBB);
return result;
}
public void drawBoundingBoxes(){
PdfContentByte content = stamper.getOverContent(pageNum);
int i = 0;
Rectangle2D currBB = null;
for (Rectangle2D currTextBoundingBox : textBoundingBoxes) {
currBB =
calculateItemBoundingBox(currTextBoundingBox, imageBoundingBoxes.get(i));
content.rectangle(
(float)currBB.getX() -
border,
(float)currBB.getY() -
border,
(float)currBB.getWidth() + 2
* border,
(float)currBB.getHeight() +
2 * border
);
content.stroke();
content.saveState();
i++;
}
}
public void renderMoveToDrawOperation(double x, double y) {
if (path.getCurrentPoint() != null
&& Math.abs(path.getCurrentPoint().getX() -
x) > x_distanceThreshold) {
calculateCurrentImageBoundingBox();
path = new GeneralPath();
}
path.moveTo(x, y);
}
public void renderLineToDrawOperation(double x, double y) {
path.lineTo(x, y);
}
public void renderCurveToDrawOperation(double ctrlP1x, double
ctrlP1y,
double ctrlP2x, double ctrlP2y, double endPx, double
endPy) {
if (ctrlP1x == -1.0 && ctrlP1y == -1.0) {
ctrlP1x = path.getCurrentPoint().getX();
ctrlP1y = path.getCurrentPoint().getY();
}
path.curveTo(ctrlP1x, ctrlP1y, ctrlP2x, ctrlP2y, endPx,
endPy);
}
}
-----Messaggio originale-----
Da: Kevin Day [mailto:[email protected]]
Inviato: giovedì 3 novembre 2011 22:35
A: [email protected]
Oggetto: Re: [iText-questions] R: R: R: R: R: R: R: image in Flatedecode
stream without metadata in dictionary
Leonard Rosenthol-3 wrote:
>
> You also need to implement the cm, q & Q operators in order to properly
> handle transformations and the Gstate.
>
The parser already handles those operators. The render listener receives
operands in page space coordinates.
I'll run this to ground with Giampaolo offline (I've sent him a direct email
to get this going).
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/image-in-Flatedecode-stream-witho
ut-metadata-in-dictionary-tp3962812p3988223.html
Sent from the iText - General mailing list archive at Nabble.com.
----------------------------------------------------------------------------
--
RSA(R) Conference 2012
Save $700 by Nov 18
Register now
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a
reference to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php
-----
Nessun virus nel messaggio.
Controllato da AVG - www.avg.com
Versione: 10.0.1411 / Database dei virus: 2092/3993 - Data di rilascio:
03/11/2011
PdfContentStreamProcessor.java
Description: Binary data
------------------------------------------------------------------------------ RSA(R) Conference 2012 Save $700 by Nov 18 Register now http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php
