Hello,
I am building a tool that will insert an annotation (three lines of text) at the top center of the first page of 100s of existing PDFs. In building my test method, I’ve discovered that it works fine most of the time, but not with certain PDFs. Some PDFs were created by scanning documents from a printer, and the pages were scanned in upside down. The user fixed the issue by using Adobe Acrobat to rotate the pages 180 degrees and then resaving the PDF. I find that when I try to use your API to add an annotation to these PDFs, the annotation is inserted at the bottom of the page and upside down. I’ve tried using the annotation rectangle’s rotate method to resolve the issue, but it did not work. Below is the simplified test code I am using:
Public Shared Sub AddTextAnnotation(pdfFullFileName As String)
Dim lineOne As String = “TEST LINE ONE”
Dim lineTwo As String = “TEST LINE TWO”
Dim lineThree As String = “TEST LINE THREE”
'Chekc PDF exists
If Not My.Computer.FileSystem.FileExists(pdfFullFileName) Then
Exit Sub
End If
'Copy file
Dim newFullFile As String = Left(pdfFullFileName, Len(pdfFullFileName) - 4) & "_COPY_ADD_TEXT.PDF"
If My.Computer.FileSystem.FileExists(newFullFile) Then
Exit Sub
End If
My.Computer.FileSystem.CopyFile(pdfFullFileName, newFullFile)
'Get document object
Dim pdfDoc As Aspose.Pdf.Document = New Aspose.Pdf.Document(newFullFile)
'Get pdf page object
Dim pdfPage As Aspose.Pdf.Page = pdfDoc.Pages(1)
'Set the height and width of the rectangle
Dim rWidth As Int32 = 200
Dim rHeight As Int32 = 60
Dim rHalfWidth As Int32 = Convert.ToInt32(rWidth / 2)
'MsgBox("Rect Height: " & rHalfWidth & vbTab & "Rect Width: " & rWidth)
'Determine the size of the page
Dim pHeight As Int32 = Convert.ToInt32(pdfPage.MediaBox.Height)
Dim pWidth As Int32 = Convert.ToInt32(pdfPage.MediaBox.Width)
Dim pXMidPnt As Int32 = Convert.ToInt32(pWidth / 2)
' MsgBox("Page Height: " & pHeight & vbTab & "Page Width: " & pWidth)
'Calculate position of LL corner and UR corner of the annotation rectangle
Dim rURY As Int32 = pHeight - 30
Dim rLLY As Int32 = rURY - rHeight
Dim rLLX As Int32 = pXMidPnt - rHalfWidth
Dim rURX As Int32 = pXMidPnt + rHalfWidth
'Create the annotation rectangle
Dim rec As Aspose.Pdf.Rectangle = New Aspose.Pdf.Rectangle(rLLX, rLLY, rURX, rURY)
'Set the default appearance for the annotation
Dim appear As Aspose.Pdf.Annotations.DefaultAppearance = New Aspose.Pdf.Annotations.DefaultAppearance("Arial", 12, System.Drawing.Color.White)
appear.TextColor = System.Drawing.Color.Red
'Create the annotation
Dim ann As Aspose.Pdf.Annotations.FreeTextAnnotation = New Aspose.Pdf.Annotations.FreeTextAnnotation(pdfPage, rec, appear)
'Add the text to the annotation
ann.Contents = lineOne
If lineTwo <> "" Then
ann.Contents = ann.Contents & vbCr & lineTwo
End If
If lineThree <> "" Then
ann.Contents = ann.Contents & vbCr & lineThree
End If
ann.Color = Aspose.Pdf.Color.White
'If this is anything other than NONE, the annotation needs to be rotated too
Dim origPageRotation As Aspose.Pdf.Rotation = pdfPage.Rotate
'------------------------------------------------------------------'
'!!!!!!Possibly Rotate the Annotation - Nothing below works!!!!
''Set the annotation rotate property
'Select Case pdfPage.Rotate
' Case Aspose.Pdf.Rotation.None
' Case Aspose.Pdf.Rotation.on90
' ann.Rotate = Aspose.Pdf.Rotation.on90
' Case Aspose.Pdf.Rotation.on180
' ann.Rotate = Aspose.Pdf.Rotation.on180
' Case Aspose.Pdf.Rotation.on270
' ann.Rotate = Aspose.Pdf.Rotation.on270
'End Select
''Use the annotation rectangle's rotate method
'Select Case pdfPage.Rotate
' Case Aspose.Pdf.Rotation.None
' Case Aspose.Pdf.Rotation.on90
' ann.Rect.Rotate(90)
' Case Aspose.Pdf.Rotation.on180
' ann.Rect.Rotate(180)
' Case Aspose.Pdf.Rotation.on270
' ann.Rect.Rotate(270)
'End Select
''Set the annotation characteristics rotate property
'Select Case pdfPage.Rotate
' Case Aspose.Pdf.Rotation.None
' Case Aspose.Pdf.Rotation.on90
' ann.Characteristics.Rotate = Aspose.Pdf.Rotation.on90
' Case Aspose.Pdf.Rotation.on180
' ann.Characteristics.Rotate = Aspose.Pdf.Rotation.on180
' Case Aspose.Pdf.Rotation.on270
' ann.Characteristics.Rotate = Aspose.Pdf.Rotation.on270
'End Select
'----------------------------------------------------------------'
'Add the annotation to the page
pdfPage.Annotations.Add(ann)
'Save the document over the original
pdfDoc.Save(pdfFullFileName)
'Delete copy
My.Computer.FileSystem.DeleteFile(newFullFile)
pdfDoc = Nothing
End Sub