class MyEventHandler : IEventListener
{
public virtual void EventOccurred(IEventData data, EventType type)
{
if (!type.Equals(EventType.RENDER_IMAGE))
{
return;
}
ImageRenderInfo imageRenderInfo = (ImageRenderInfo)data;
var matrix = imageRenderInfo.GetImageCtm();
var image = imageRenderInfo.GetImage();
Console.WriteLine(image.GetWidth().ToString() + ':' + image.GetHeight().ToString());
Console.WriteLine(matrix.ToString());
Console.WriteLine();
}
public virtual ICollection<EventType> GetSupportedEvents()
{
return JavaCollectionsUtil.UnmodifiableSet(new LinkedHashSet<EventType>(JavaCollectionsUtil.SingletonList(EventType.RENDER_IMAGE)));
}
}
internal class ExtractImg
{
static void Main(string[] args)
{
var pdfDocument = new PdfDocument(new PdfReader("123.pdf"));
for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
{
var page = pdfDocument.GetPage(i);
var strategy = new MyEventHandler();
new PdfCanvasProcessor(strategy).ProcessPageContent(page);
}
}
}
GetImageCtm返回是一个3*3的矩阵,决定了图片的位置和大小
a,b,0
[c,d,0]
e,f,1
在pdf中,一张图片的坐标范围是[0,1]。矩阵中a,d两个参数,决定了图片的宽和高,代表了72dpi下多少像素的长度。而e,f两个参数决定了图片在页面中的位置。在有旋转的情况下,abcd都会有值。
网友评论