使用Telerik RadPdfProcessing将PDF中的文本替换为图像



我正在使用Telerik RadPdfProcessing for Xamarin在移动应用程序中定制PDF文档。代码必须搜索"#占位符#"在文件内,并将其替换为用户键入的签名手。

签名图像可以通过以下代码插入到给定位置:

RadFixedPage last = document.Pages.Last();
FixedContentEditor editor = new FixedContentEditor(last);
editor.Position.Translate(400, 900);
editor.DrawImage(imageSource);

我发现一个论坛说,在页面中查找基本上不支持库,因为它只允许一次搜索一个字符:

foreach (var contentElement in last.Content) {
if (contentElement is TextFragment) {
TextFragment fragment = (TextFragment) contentElement;
string text = fragment.Text;
//** THIS DOESN'T WORK! **
//if ("#placeholder#" == text)  {
//    fragment.Text = "";
//    editor.Position = fragment.Position;
//    editor.DrawImage(imageSource);
//}
}
}
谁有关于这个主题的最新消息?

您可以通过以下方式实现:

RadFixedPage lastPage = document.Pages.Last();
IPosition position = new SimplePosition();
foreach (ContentElementBase contentElement in lastPage.Content.ToList())
{
if (contentElement is TextFragment fragment)
{
if (fragment.Text == "#placeholder#")
{
position = fragment.Position;
lastPage.Content.Remove(fragment);
}
}
}
ImageSource imageSource;
using (FileStream source = File.Open("image1.jpg", FileMode.Open))
{
imageSource = new ImageSource(source);
}
FixedContentEditor editor = new FixedContentEditor(lastPage);
editor.Position = position;
editor.DrawImage(imageSource, new Size(50, 50));

另一个选项是通过搜索其名称来替换预定义的Field。这样你就可以在文档的页面上预先分配空间。

RadFixedPage firstPage = document.Pages[0];
Annotation field = firstPage.Annotations.First(a => ((VariableContentWidget)a).Field.Name == "SampleField");
System.Windows.Rect fieldRect = field.Rect;
string imagePath = "image1.jpg";
ImageSource imageSource;
using (FileStream source = File.Open(imagePath, FileMode.Open))
{
imageSource = new ImageSource(source);
}
IPosition position = new SimplePosition();
simplePosition.Translate(fieldRect.X, fieldRect.Y);
Image image = new Image
{
ImageSource = imageSource,
Position = position,
Width = fieldRect.Width,
Height = fieldRect.Height
};
firstPage.Annotations.Remove(field);
firstPage.Content.Add(image);

最新更新