简体   繁体   English

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

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

I'm actualy trying to add a markup annotation in a PDF with PDFKit, in Xamarin.Mac, so for OS X. So my goal is to highlight permanently, as an annotation, a selected text in the PDF File, and save it to retrieve it when I open the file later. 我实际上是想在Xamarin.Mac中使用PDFKit在Xamarin.Mac中为PDF添加标记注释,因此对于OSX。所以我的目标是永久性突出显示PDF文件中的选定文本作为注释,并将其保存到稍后打开文件时检索它。

The thing is, I can get the current selection and store it into a variable : 问题是,我可以获取当前选择并将其存储到变量中:

PdfSelection currentSelection = m_aPdfView.CurrentSelection;

And I can create an object PdfAnnotationMarkup : 我可以创建一个对象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;

But I can't find, even though I checked a lot of different documentations, how to link the two of them. 但是,即使我检查了很多不同的文档,也找不到如何链接这两个文档。 There is no method giving the location of the currentSelection, or any hint to go in that direction. 没有方法提供currentSelection的位置,也没有提供任何朝该方向的提示。

Would anyone know of a way to make this possible ? 有谁知道实现这一目标的方法吗?

PS: I found that the subclasses of PDFAnnotation are deprecated on the Apple Developer Website , but not on the Xamarin Website , is there any way of knowing if both of them are related of entirely different ? PS:我发现PDFAnnotation的子类在Apple Developer网站上已弃用,但在Xamarin网站上却不建议使用,是否有办法知道它们是否完全不同?

Thanks in advance for your help 在此先感谢您的帮助

EDIT : Here is the code I got and works perfectly. 编辑:这是我得到的代码,并且可以完美地工作。 Thanks to svn's answers 感谢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;

A selection can span multiple pages. 一个选择可以跨越多个页面。 To get the location of a selection use: 要获取选择的位置,请使用:

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

You should be able to instantiate PdfAnnotationMarkup with bounds but the xamarin implementation does not expose that constuctor. 您应该能够使用边界实例化PdfAnnotationMarkup,但xamarin实现不会公开该构造函数。 But is does expose a bounds property 但是确实暴露了界限属性

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

I have not tested this. 我还没有测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM