AdjustCoords.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports GrapeCity.Documents.Pdf
  8. Imports GrapeCity.Documents.Pdf.Annotations
  9.  
  10. '' This sample shows how to use the Page.AdjustCoordinates() method
  11. '' to convert visual coordinates (measured from the top left corner
  12. '' of the page according to DsPdf rules) to correct coordinates in a PDF
  13. '' that was loaded into GcPdfDocument And may have arbitrary unknown
  14. '' transformations applied to its pages. The adjusted coordinates can be
  15. '' used to position annotations (redact in this case) etc.
  16. ''
  17. '' The PDF used in this sample has one page containing the scan of a sample invoice
  18. '' that was rotated 90 degrees clockwise. The PDF page Is rotated 270 degrees to
  19. '' compensate (so that visually the page has correct portrait orientation).
  20. Public Class AdjustCoords
  21. Function CreatePDF(ByVal stream As Stream) As Integer
  22. Dim doc = New GcPdfDocument()
  23. Using fs = File.OpenRead(Path.Combine("Resources", "PDFs", "invoice-scan-rot270.pdf"))
  24. doc.Load(fs)
  25.  
  26. If doc.Pages.Count <> 1 Then
  27. Throw New Exception("Unexpected: sample invoice should have exactly one page.")
  28. End If
  29.  
  30. Dim page = doc.Pages(0)
  31.  
  32. '' The bounding rectangle of the area to redact,
  33. '' measured from the top left corner of the PDF page.
  34. '' If this rectangle Is used as Is, it will miss the target content:
  35. Dim rectToRedact = New RectangleF(20, 170, 200, 40)
  36.  
  37. '' The adjusted bounding rectangle, taking into consideration
  38. '' any possible page transformations (rotation in this case):
  39. Dim adjusted = page.AdjustCoordinates(rectToRedact)
  40.  
  41. '' Note If 'rectToRedact' is used instead of 'adjusted',
  42. '' the redact will miss the target (customer name/address):
  43. Dim redact = New RedactAnnotation() With
  44. {
  45. .Rect = adjusted,
  46. .OverlayFillColor = Color.Orange,
  47. .OverlayText = "Redacted",
  48. .page = page
  49. }
  50. '' Apply the redact:
  51. doc.Redact(redact)
  52.  
  53. '' Done
  54. doc.Save(stream)
  55. Return doc.Pages.Count
  56. End Using
  57. End Function
  58. End Class
  59.