【发布时间】: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