问题描述
我按照ItextSharp示例获取图形结构,获取矩形坐标,使用代码:
I follow ItextSharp example for getting a graphical structure, to get the rectangle coordinates, using code:
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();
但是没有这样的方法。
I tried the renderInfo.GetFillColor();but there is no such a method.
我想获得额外的矩形属性:
像矩形填充颜色,(有些模拟到DIV style =background:blue; border:black;html中的标签)
我是怎么得到的?
I want to get additional rectangle properties :
like rectangle fill color,( some analog to DIV style="background: blue; border:black;" tag in html)how I get it ?
推荐答案
是。
因此,我只是将该答案(使用iText / Java)的代码移植到iTextSharp / C#:
Thus, I simply ported the code from that answer (which was using iText / Java) to iTextSharp / C#:
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
。
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,此代码返回
Output
For the PDF that former question was about, this code returns
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 ]
(正如您在坐标输出中观察到的,我当前的语言环境使用逗号作为小数分隔符...)
(As you can observe in the coordinate outputs, my current locale uses a comma as decimal separator...)
这篇关于itextsharp:如何找到矩形的填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!