[英]Xamarin.Mac - How to highlight the selected text in a PDF file
我实际上是想在Xamarin.Mac中使用PDFKit在Xamarin.Mac中为PDF添加标记注释,因此对于OSX。所以我的目标是永久性突出显示PDF文件中的选定文本作为注释,并将其保存到稍后打开文件时检索它。
问题是,我可以获取当前选择并将其存储到变量中:
PdfSelection currentSelection = m_aPdfView.CurrentSelection;
我可以创建一个对象PdfAnnotationMarkup:
//Create the markup annotation
var annot = new PdfAnnotationMarkup();
//add characteristics to the annotation
annot.Contents = currentSelectionText;
annot.MarkupType = PdfMarkupType.Highlight;
annot.Color = NSColor.Yellow;
annot.ShouldDisplay = true;
但是,即使我检查了很多不同的文档,也找不到如何链接这两个文档。 没有方法提供currentSelection的位置,也没有提供任何朝该方向的提示。
有谁知道实现这一目标的方法吗?
PS:我发现PDFAnnotation的子类在Apple Developer网站上已弃用,但在Xamarin网站上却不建议使用,是否有办法知道它们是否完全不同?
在此先感谢您的帮助
编辑:这是我得到的代码,并且可以完美地工作。 感谢svn的回答
//Get the current selection on the PDF file opened in the PdfView
PdfSelection currentSelection = m_aPdfView.CurrentSelection;
//Check if there is an actual selection right now
if (currentSelection != null)
{
currentSelection.GetBoundsForPage(currentSelection.Pages[0]);
//Create the markup annotation
var annot = new PdfAnnotationMarkup();
//add characteristics to the annotation
annot.Contents = "Test";
annot.MarkupType = PdfMarkupType.Highlight;
annot.Color = NSColor.Yellow;
annot.ShouldDisplay = true;
annot.ShouldPrint = true;
annot.UserName = "MyName";
//getting the current page
PdfPage currentPage = currentSelection.Pages[0];
//getting the bounds from the current selection and adding it to the annotation
var locationRect = currentSelection.GetBoundsForPage(currentPage);
getValuLabel.StringValue = locationRect.ToString();
//converting the CGRect object into CGPoints
CoreGraphics.CGPoint upperLeft = locationRect.Location;
CoreGraphics.CGPoint lowerLeft = new CoreGraphics.CGPoint(locationRect.X, (locationRect.Y + locationRect.Height));
CoreGraphics.CGPoint upperRight = new CoreGraphics.CGPoint((locationRect.X + locationRect.Width), locationRect.Y);
CoreGraphics.CGPoint lowerRight = new CoreGraphics.CGPoint((locationRect.X + locationRect.Width), (locationRect.Y + locationRect.Height));
//adding the CGPoints to a NSMutableArray
NSMutableArray pointsArray = new NSMutableArray();
pointsArray.Add(NSValue.FromCGPoint(lowerLeft));
pointsArray.Add(NSValue.FromCGPoint(lowerRight));
pointsArray.Add(NSValue.FromCGPoint(upperLeft));
pointsArray.Add(NSValue.FromCGPoint(upperRight));
//setting the quadrilateralPoints
annot.WeakQuadrilateralPoints = pointsArray;
//add the annotation to the PDF file current page
currentPage.AddAnnotation(annot);
//Tell the PdfView to update the display
m_aPdfView.NeedsDisplay = true;
一个选择可以跨越多个页面。 要获取选择的位置,请使用:
var locationRect = currentSelection.GetBoundsForPage(currentSelection.Pages[0]);
您应该能够使用边界实例化PdfAnnotationMarkup,但xamarin实现不会公开该构造函数。 但是确实暴露了界限属性
var annot = new PdfAnnotationMarkup();
annot.bounds = locationRect;
我还没有测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.