【问题标题】:Xamarin.Mac - How to highlight the selected text in a PDF fileXamarin.Mac - 如何突出显示 PDF 文件中的选定文本
【发布时间】:2016-07-13 16:28:09
【问题描述】:

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

【问题讨论】:

    标签: macos pdf xamarin.mac


    【解决方案1】:

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

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

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

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

    我没有测试过。

    【讨论】:

    • 感谢您的回复。使用您的解决方案,我确实可以获得 currentSelection 的范围,但注释不会显示在 PDF 上。即使我添加了 locationRect。但是非常感谢这个解决方案,我不知道怎么做,但我错过了......这正是我想要的!
    • 可能是您需要使用 setQuadrilateralPoints 代替。您需要将矩形转换为点数组
    • 应用后也更新显示:page addAnnotation: and PDFView setNeedsDisplay: YES
    • 即使我尝试了所有这些,它仍然没有显示在我的 PdfView 上。我尝试使用预览添加注释,我可以在我的程序中看到它,所以不是 PdfView 有问题。我检查了一下,注释似乎创建和存储得很好,只是显示关闭了。我不明白为什么。我更新了我的初始帖子以添加我的代码
    • weakquadrilarray 不正确。它需要是一个点对象数组。所以你需要将边界翻译成 4 个实际的 NSPoint 对象
    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 2017-08-08
    • 2012-12-24
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多