使用itextsharp从选定区域或坐标中的pdf中提取图像 [英] Extract image from a pdf in a selected area or coordinates using itextsharp

查看:127
本文介绍了使用itextsharp从选定区域或坐标中的pdf中提取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的要求,即从pdf文件中的特定区域提取图像。该区域可能是选定的或突出显示的,也可能来自一组给定的坐标。



当我经历时,所有方法都是完全从PDF中提取图像和文本而不是在指定位置。我尝试使用 iTextSharp,Syncfussion,Apose 但是无法找到更好的方法。



如果有人可以帮助我这会很棒。你能否分享一下如何在.net中实现这个想法和建议。



问候,

Kranthi。

I have a specific requirement of extracting images from a specific area in a pdf file.The area might be a selected or highlighted or from a given set of coordinates.

When i went through, all the approaches are to extract images and text entirely from the PDF on not in a specified location. I tried with iTextSharp,Syncfussion,Apose but couldn't figure out a better approach for this.

If somebody could help me out in this it would be greatful. Can you share your ideas and suggestion on how to implement this in .net.

Regards,
Kranthi.

推荐答案

使用(var pdfReader = new PdfReader(Server.MapPath(savePath1)))

{

float distanceInPixelsFromLeft = 360;

浮动distanceInPixelsFromBottom = 30;

浮动宽度= 120;

浮动高度= 80;



var rect = new System.util.RectangleJ(

distanceInPixelsFromLeft,

distanceInPixelsFromBottom,

宽度,

高度);







var filters = new RenderFilter [1];

filters [0] =新区域TextRenderFilter(rect);



ITextExtractionStrategy strategy =

new FilteredTextRenderListener(

new LocationTextExtractionStrategy(),

过滤器);

尝试

{

var currentText = PdfTextExtractor.GetTextFromPage(

pdfReader,

c,

策略);



currentText =

编码。 UTF8.GetString(Encoding.Convert(

Encoding.Default,

Encoding.UTF8,

Encoding.Default.GetBytes(currentText))) ;



text.Append(currentText);



//验证区域中存在的文本

if(text.ToString()!= )

{

ValidateErrorMessage.Text =文本存在于条形码区域;

ValidateErrorMessage.Visible = true;

}

其他

{

ValidateErrorMessage.Text =条形码区域有效;

ValidateErrorMessage .Visible = true;

}

}

catch(例外情况)

{



ValidateErrorMessage.Text =条形码区域无效;

V alidateErrorMessage.Visible = true;



}



pdfReader.Close();

}
using (var pdfReader = new PdfReader(Server.MapPath(savePath1)))
{
float distanceInPixelsFromLeft = 360;
float distanceInPixelsFromBottom = 30;
float width = 120;
float height = 80;

var rect = new System.util.RectangleJ(
distanceInPixelsFromLeft,
distanceInPixelsFromBottom,
width,
height);



var filters = new RenderFilter[1];
filters[0] = new RegionTextRenderFilter(rect);

ITextExtractionStrategy strategy =
new FilteredTextRenderListener(
new LocationTextExtractionStrategy(),
filters);
try
{
var currentText = PdfTextExtractor.GetTextFromPage(
pdfReader,
c,
strategy);

currentText =
Encoding.UTF8.GetString(Encoding.Convert(
Encoding.Default,
Encoding.UTF8,
Encoding.Default.GetBytes(currentText)));

text.Append(currentText);

//Validation for text existing in the area
if (text.ToString() != "")
{
ValidateErrorMessage.Text = "Text exists in Barcode area";
ValidateErrorMessage.Visible = true;
}
else
{
ValidateErrorMessage.Text = "Barcode area is valid";
ValidateErrorMessage.Visible = true;
}
}
catch (Exception ex)
{

ValidateErrorMessage.Text = "Barcode area not valid";
ValidateErrorMessage.Visible = true;

}

pdfReader.Close();
}


这篇关于使用itextsharp从选定区域或坐标中的pdf中提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆