2016-07-11 63 views
1

我真的試圖在PDF中添加PDFKit,Xamarin.Mac,因此對於OS X. 所以我的目標是突出顯示將PDF文件中的選定文本作爲註釋永久保存,並在稍後打開文件時將其保存以便檢索它。Xamarin.Mac - 如何突出顯示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; 

回答

2

選擇可以跨越多個頁面。爲了得到一個選擇使用的位置:

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

你應該能夠實例PdfAnnotationMarkup與界限,但xamarin實現不公開該constuctor。但是是否暴露了界限屬性

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

我沒有測試過這個。

+0

感謝您的回覆。 使用您的解決方案,我確實可以獲得currentSelection的界限,但註釋不會顯示在PDF上。即使我添加了locationRect。 但是,非常感謝這個解決方案,我不知道如何,但我錯過了......這正是我所期待的! – Hikaiix

+0

可能是因爲你需要使用setQuadrilateralPoints來代替。您需要將矩形轉換爲點陣列 – svn

+0

在應用後還要更新顯示:頁面addAnnotation:和PDFView setNeedsDisplay:YES – svn

相關問題