Xamarin.Mac - 如何在 PDF 文件中高亮选中的文本

Xamarin.Mac - How to highlight the selected text in a PDF file

我实际上正在尝试在 Xamarin.Mac 中使用 PDFKit 在 PDF 中添加标记注释,因此对于 OS X。 所以我的目标是永久突出显示 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;

但是我找不到,即使我检查了很多不同的文档,如何 link 他们两个。 没有给出 currentSelection 位置的方法,也没有任何指向该方向的提示。

有人知道实现这一目标的方法吗?

PS:我发现 PDFAnnotation 的子类在 Apple Developer Website , but not on the Xamarin Website 上被弃用了,有没有办法知道它们是否相关或完全不同?

在此先感谢您的帮助

编辑:这是我得到的代码,运行良好。感谢 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;

我还没有测试过这个。