Xamarin.Mac - 如何突出显示PDF文件中的选定文本

时间:2016-07-11 15:30:16

标签: macos pdf xamarin.mac

我实际上尝试在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;

但即使我检查了很多不同的文档,我仍然无法找到如何链接其中两个文档。 没有方法给出currentSelection的位置,或任何提示朝这个方向发展。

有人知道如何实现这一目标吗?

PS:我发现PDFAnnotation的子类已在Apple Developer Website上弃用,但在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;

1 个答案:

答案 0 :(得分:2)

选择可以跨越多个页面。要获得选择的位置,请使用:

var locationRect = currentSelection.GetBoundsForPage(currentSelection.Pages[0]);

您应该能够使用边界实例化PdfAnnotationMarkup,但xamarin实现不会公开该构造函数。但是确实暴露了一个边界属性

var annot = new PdfAnnotationMarkup();
annot.bounds = locationRect;

我没有测试过这个。