使用 Telerik RadPdfProcessing 将 PDF 中的文本替换为图像
Replace text with image in PDF using Telerik RadPdfProcessing
我正在使用 Telerik RadPdfProcessing for Xamarin 在移动应用程序中自定义 PDF 文档。
该代码必须在文档中搜索“#placeholder#”并将其替换为用户手写的签名。
可以使用以下代码将签名图片插入给定位置:
RadFixedPage last = document.Pages.Last();
FixedContentEditor editor = new FixedContentEditor(last);
editor.Position.Translate(400, 900);
editor.DrawImage(imageSource);
而且我发现 forum 说图书馆基本上不支持在页面中查找,因为它一次只允许搜索一个字符:
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。这样您就可以在文档页面上预先分配 space。
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);
我正在使用 Telerik RadPdfProcessing for Xamarin 在移动应用程序中自定义 PDF 文档。 该代码必须在文档中搜索“#placeholder#”并将其替换为用户手写的签名。
可以使用以下代码将签名图片插入给定位置:
RadFixedPage last = document.Pages.Last();
FixedContentEditor editor = new FixedContentEditor(last);
editor.Position.Translate(400, 900);
editor.DrawImage(imageSource);
而且我发现 forum 说图书馆基本上不支持在页面中查找,因为它一次只允许搜索一个字符:
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。这样您就可以在文档页面上预先分配 space。
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);