我按照ItextSharp示例获取图形结构,使用代码获取矩形坐标:
class VectorGraphicsListener : IExtRenderListener
{
public void ModifyPath(PathConstructionRenderInfo renderInfo)
{
if (renderInfo.Operation == PathConstructionRenderInfo.RECT)
{
float x = renderInfo.SegmentData[0];
float y = renderInfo.SegmentData[1];
float w = renderInfo.SegmentData[2];
float h = renderInfo.SegmentData[3];
...
我尝试了renderInfo.GetFillColor(); 但是没有这样的方法。
我想获得额外的矩形属性:
像矩形填充颜色,(有些类似于DIV样式="背景:蓝色;边框:黑色;"标签在html中)
我是怎么得到的?
答案 0 :(得分:1)
TextRenderInfo
您正在寻找的方法PathConstructionRenderInfo
没有。
你从哪里得到这个例子?您可能使用的版本与使用的版本文档版本不同。
本文解释了您需要做什么,但它提供的Java源代码应该很容易移植到C#:http://techqa.info/programming/question/41728853/how-to-extract-the-color-of-a-rectangle-in-a-pdf,-with-itext
为了总结相关部分,它使用GraphicsState
来获取填充颜色。要实现这一点,您需要使用PdfReaderContentParser
将ExtRenderListener
传递给processContent
方法解析所有PDF内容,如下所示(取自上面的Java代码):
PdfReader pdfReader = new PdfReader(resource);
ExtRenderListener extRenderListener = new ExtRenderListener();
for (int page = 1; page <= pdfReader.getNumberOfPages(); page++)
{
PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader);
parser.processContent(page, extRenderListener);
}
ExtRenderListener将需要覆盖renderPath:
public override Path renderPath(PathPaintingRenderInfo renderInfo)
{
Field gsField = PathPaintingRenderInfo.class.getDeclaredField("gs");
gsField.setAccessible(true);
GraphicsState graphicsState = (GraphicsState) gsField.get(renderInfo);
if ((renderInfo.getOperation() & PathPaintingRenderInfo.FILL) != 0)
{
var fillColor = graphicsState.getFillColor();
bool filledRect= false;
for (PathConstructionRenderInfo pathConstructionRenderInfo in pathInfos)
{
if(pathConstructionRenderInfo.getOperation()==PathConstructionRenderInfo.RECT)
{
filledRect=true;
break;
}
if (filledRect && fillColor!=null)
Console.WriteLine("{0},{1},{2}",
fillColor.getRed(), fillColor.getGreen(), fillColor.getBlue());
}
}
}
答案 1 :(得分:1)
page Alexander in his answer posted a link to是earlier answer by me here on stack overflow的副本。
因此,我只是将该答案(使用iText / Java)的代码移植到iTextSharp / C#:
IExtRenderListener
实施class ExtRenderListener : IExtRenderListener
{
public void BeginTextBlock() { }
public void RenderText(TextRenderInfo renderInfo) { }
public void EndTextBlock() { }
public void RenderImage(ImageRenderInfo renderInfo) { }
public void ModifyPath(PathConstructionRenderInfo renderInfo)
{
pathInfos.Add(renderInfo);
}
public iTextSharp.text.pdf.parser.Path RenderPath(PathPaintingRenderInfo renderInfo)
{
GraphicsState graphicsState = getGraphicsState(renderInfo);
Matrix ctm = graphicsState.GetCtm();
if ((renderInfo.Operation & PathPaintingRenderInfo.FILL) != 0)
{
Console.Write("FILL ({0}) ", toString(graphicsState.FillColor));
if ((renderInfo.Operation & PathPaintingRenderInfo.STROKE) != 0)
Console.Write("and ");
}
if ((renderInfo.Operation & PathPaintingRenderInfo.STROKE) != 0)
{
Console.Write("STROKE ({0}) ", toString(graphicsState.StrokeColor));
}
Console.Write("the path ");
foreach (PathConstructionRenderInfo pathConstructionRenderInfo in pathInfos)
{
switch (pathConstructionRenderInfo.Operation)
{
case PathConstructionRenderInfo.MOVETO:
Console.Write("move to {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.CLOSE:
Console.Write("close {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.CURVE_123:
Console.Write("curve123 {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.CURVE_13:
Console.Write("curve13 {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.CURVE_23:
Console.Write("curve23 {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.LINETO:
Console.Write("line to {0} ", toString(transform(ctm, pathConstructionRenderInfo.SegmentData)));
break;
case PathConstructionRenderInfo.RECT:
Console.Write("rectangle {0} ", toString(transform(ctm, expandRectangleCoordinates(pathConstructionRenderInfo.SegmentData))));
break;
}
}
Console.WriteLine();
pathInfos.Clear();
return null;
}
String toString(IList<float> coordinates)
{
StringBuilder result = new StringBuilder();
result.Append("[ ");
for (int i = 0; i < coordinates.Count; i++)
{
result.Append(coordinates[i]);
result.Append(' ');
}
result.Append(']');
return result.ToString();
}
List<float> transform(Matrix ctm, IList<float> coordinates)
{
List<float> result = new List<float>();
for (int i = 0; i + 1 < coordinates.Count; i += 2)
{
Vector vector = new Vector(coordinates[i], coordinates[i + 1], 1);
vector = vector.Cross(ctm);
result.Add(vector[Vector.I1]);
result.Add(vector[Vector.I2]);
}
return result;
}
List<float> expandRectangleCoordinates(IList<float> rectangle)
{
if (rectangle.Count < 4)
return new List<float>();
return new List<float>
{
rectangle[0], rectangle[1],
rectangle[0] + rectangle[2], rectangle[1],
rectangle[0] + rectangle[2], rectangle[1] + rectangle[3],
rectangle[0], rectangle[1] + rectangle[3]
};
}
String toString(BaseColor baseColor)
{
if (baseColor == null)
return "DEFAULT";
return String.Format("{0},{1},{2}", baseColor.R, baseColor.G, baseColor.B);
}
GraphicsState getGraphicsState(PathPaintingRenderInfo renderInfo)
{
System.Reflection.FieldInfo gsField = typeof(PathPaintingRenderInfo).GetField("gs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
return (GraphicsState) gsField.GetValue(renderInfo);
}
public void ClipPath(int rule)
{
}
List<PathConstructionRenderInfo> pathInfos = new List<PathConstructionRenderInfo>();
}
检索GraphicsState
所需的反映发生在方法getGraphicsState
中。
ExtRenderListener
类using (var pdfReader = new PdfReader([FILE TO PARSE]))
{
IExtRenderListener extRenderListener = new ExtRenderListener();
// Loop through each page of the document
for (var page = 1; page <= pdfReader.NumberOfPages; page++)
{
Console.Write("\nPage {0}\n====\n", page);
PdfReaderContentParser parser = new PdfReaderContentParser(pdfReader);
parser.ProcessContent(page, extRenderListener);
}
}
对于前一个问题的PDF,此代码返回
Page 1
====
STROKE (0,0,0) the path rectangle [ 88,3 693,69 227,77 693,69 227,77 788 88,3 788 ]
STROKE (0,0,0) the path rectangle [ 227,77 693,69 367,24 693,69 367,24 788 227,77 788 ]
STROKE (0,0,0) the path rectangle [ 367,23 693,69 506,7 693,69 506,7 788 367,23 788 ]
FILL (255,0,0) the path rectangle [ 229,77 695,69 365,37 695,69 365,37 786,09 229,77 786,09 ]
STROKE (DEFAULT) the path move to [ 228 810 ] line to [ 338 810 ]
(正如您在坐标输出中所观察到的,我当前的语言环境使用逗号作为小数分隔符...)