Annotation Inserted Upside Down On Bottom

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

@mperryc2853

Thank you for contacting support.

Would you please share source and generated PDF files with us so that we may try to reproduce and investigate it in our environment.

Sure. Attached.

Thank you for looking into this.

470-5815 - WORKS - SOURCE.PDF (31.3 KB)
470-5815 - WORKS - RESULT.PDF (50.8 KB)
670-3034 - DOES NOT WORK - RESULT.pdf (338.6 KB)
670-3034 - DOES NOT WORK - SOURCE.pdf (320.1 KB)

@mperryc2853

Thank you for sharing requested data.

We have been able to reproduce the issue in our environment. A ticket with ID PDFNET-45668 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

Hello,

Is there any update to this issue? The users are waiting for their application, but we’d rather not implement it with such a problem.

Thank you.

@mperryc2853

The issue reported by you has been logged in our issue management system few days ago. Currently, it is pending owing to previously logged tickets and will be investigated on its due turn, that can take some months. We appreciate your patience and comprehension in this regard.

However, we also offer Paid Support, where issues are used to be investigated with higher priority. Our customers, who have paid support subscription, report their issue there which are meant to be investigated urgently. In case your reported issue is a blocker, you may please consider subscribing for Paid Support. For further information, please visit Paid Support FAQs.

I came across this very same problem myself. As a solution I check the page rotations on each page of the PDF first:

                    Dim nPages As Integer = oPDF.Pages.Count
                    Dim nPage As Integer = 0
                    Dim oPage As Aspose.Pdf.Page
                    Dim bRotatedPages As Boolean = False
                    While nPage < nPages
                        nPage = nPage + 1 '1-based
                        oPage = oPDF.Pages.Item(nPage)
                        If oPage.Rotate <> Aspose.Pdf.Rotation.None Then
                            bRotatedPages = True
                        End If
                    End While

After this check, if I find bRotatedPages True I use .Save to save the document as a .docx. I then use Aspose Words to open the .docx, and save it again as the original pdf filename. After that all pages have Aspose.Pdf.Rotation.None

This solution works for us as we “pre-process” documents for our purposes and we keep using a previously pre-processed document as long as the original doesn’t change. This saves us time, as the process of saving it to .docx and back to .pdf is a bit slow.

@rozeboosje

Thank you for sharing the workaround.

It will certainly help other community members. We will also notify here as soon as the ticket will be resolved.

1 Like

Hi There , I have aspose total license and I have same issue while scanned pdf is rotated in 90, 270, 180 degrees. FreeTextAnnotation with rectangle is vertical instead of horizontal in the pdf. Is there a solution available for this? Is total license include paid support? Need help urgently.

Note the workaround suggested(converting to word and back to pdf) in this doesn’t serve the purpose in my case as I need the formulas in the pdf.

@anianupra

Please try to set the considerRotation Property as true while adding the annotation to page and let us know if it does not resolve the issue:

Page.Annotations.Add(annotation, true);

Furthermore, the subscription to the paid support has to be purchased separately and does not come with a normal license. For more details, you can create an inquiry in our Purchase Forum.

@asad.ali thank you for your response. After I added Page.Annotations.Add(annotation, true);

image.png (4.8 KB)
The rectangular FreeTextAnnotation( FreeTextAnnotation(Page, Rectangle, DefaultAppearance)) is distorted as seen in image attached, and coordinates are wrong where I annotated.

I am partly successful in 90 and 180 by applying this logic.
if(pageRotation==Rotation.on90){ ← working as expected
doc.Pages[shapes[i].p].Rotate = Rotation.on270;
var transform = doc.Pages[shapes[i].p].RotationMatrix.Transform(rectangle);
freetext = new FreeTextAnnotation(doc.Pages[shapes[i].p], transform, DefaultAppearance);
freetext.Rotate = Rotation.on90;
}
else if(pageRotation==Rotation.on180){ ← working as expected

}
else if(pageRotation==Rotation.on270){ ← If I annotate on the top of the page I am getting at the bottom.
// I am having trouble transforming this part
}

doc.Pages[shapes[i].p].Annotations.Add(freetext);
doc.Pages[shapes[i].p].Rotate = rotation; ← setting back same rotation as when I started

Is there any other resolution? Any help is highly appreciated.
Thank you,
A

@asad.ali, Any update on the 270 degree logic to plot rectangular free text annotation correctly? Right now even though I do Reverse() on RotationMatrix.Reverse().Transform(rectangle)
it is getting annotated in the wrong places or going out of bound. Any help in this matter is really appreciated.
Thank you in advance.

@anianupra

Can you please share a complete sample code snippet along with sample PDF document so that we can test the scenario in our environment and address it accordingly? We are sorry for the delayed response.

@asad.ali,

Here is the attached sample project it also contains the pdf SCAN0001.pdf in the data folder.
Examples.zip (9.6 MB)
Please let me know if you need anything else from my end.

Note: 90 and 180 are working as expected. Just 270 coordinates are off the page.

Thank you,
A

@anianupra

Please check the below code snippet in which we have made some modification by commenting some lines and tested it with 21.7 version of the API. The output PDF seemed fine and has been attached here as well for your kind reference:

Document pdfDocument = new Document(dataDir + "SCAN0001.pdf");
// Instantiate DefaultAppearance object
DefaultAppearance default_appearance = new DefaultAppearance("Arial", 28, System.Drawing.Color.Blue);
for(int i=1; i<= pdfDocument.Pages.Count; i++)
{
 Rotation rotation = pdfDocument.Pages[i].Rotate;
                
 // Create annotation
 FreeTextAnnotation freetext = new FreeTextAnnotation(pdfDocument.Pages[i], new Aspose.Pdf.Rectangle(200, 400, 400, 600), default_appearance);
 // Specify the contents of annotation
 freetext.Contents = "Free Text - @ " + rotation;

 //*****Rotating the page to correct diraction and applying the annotation.
 if (rotation == Rotation.on90)
 {
  //pdfDocument.Pages[i].Rotate = Rotation.on270;
  var transform = pdfDocument.Pages[i].RotationMatrix.Transform(new Aspose.Pdf.Rectangle(200, 400, 400, 600));
  freetext = new FreeTextAnnotation(pdfDocument.Pages[i], transform, default_appearance);
  freetext.Contents = "Free Text - @ "+ rotation;
  //*******set the rotation back to where it was.
  //freetext.Rotate = Rotation.on90;
 }
 else if(rotation == Rotation.on180)
 {
  //pdfDocument.Pages[i].Rotate = Rotation.on180;
  var transform = pdfDocument.Pages[i].RotationMatrix.Transform(new Aspose.Pdf.Rectangle(200, 400, 400, 600));
  freetext = new FreeTextAnnotation(pdfDocument.Pages[i], transform, default_appearance);
  freetext.Contents = "Free Text - @ " + rotation;
  //*******set the rotation back to where it was.
  //freetext.Rotate = Rotation.on180;
 }
 else if (rotation == Rotation.on270)
 {
  //pdfDocument.Pages[i].Rotate = Rotation.on90;
  var transform = pdfDocument.Pages[i].RotationMatrix.Reverse().Transform(new Aspose.Pdf.Rectangle(200, 400, 400, 600));
  freetext = new FreeTextAnnotation(pdfDocument.Pages[i], transform, default_appearance);
  freetext.Contents = "Free Text - @ " + rotation;
  //*******set the rotation back to where it was.
  //freetext.Rotate = Rotation.on270;
 }
               
 // Add anootation to annotations collection of page
 pdfDocument.Pages[i].Annotations.Add(freetext, true);

 //pdfDocument.Pages[i].Rotate = rotation;
}
dataDir = dataDir + "SetFreeTextAnnotationFormatting_out.pdf";
// Save the updated document
pdfDocument.Save(dataDir);
// ExEnd:SetFreeTextAnnotationFormatting
Console.WriteLine("\nFree TextAnnotation with specific text formatting added successfully.\nFile saved at " + dataDir);

SetFreeTextAnnotationFormatting_out.pdf (4.6 MB)

@asad.ali,

I tried with 21.7 version commenting the line you asked. Still coordinates aren’t exactly the way they were plotted but vertical horizontal and upside down. This still remains an issue at my end :frowning:
The attached PDF I am not able to see as it says “Sorry, this file is private. Only visible to topic owner and staff members.”

@anianupra

Can you please share an expected output PDF by adding annotations in it using Adobe Reader? You can also share screenshots by marking up the anomalies in the PDF generated by the code you tried. We will try to produce a PDF similar to your expected one and share it with you with code snippet.

@asad.ali,
As always appreciate your prompt response.
Here are the coordinates before and after the transform for 270degree.
Before transform:
llx --> 172.078
lly --> -46.38635
urx --> -303.483
ury --> -27.408
After 270degree transform:
llx --> 819.408
lly --> 172.078
urx --> 838.386
ury --> -303.483

Documents in the Annotations folder contains DocScan-270.pdf is the sample and Expected_Approximation output.pdf is the marking up with anomalies.

Examples.zip (8.0 MB)

Please do the needful.
Thank you,
A

@anianupra

Thanks for sharing more details.

Could you please confirm if “Expected_Approximation output.pdf” is the document which you expect to generate using the API? If yes, please share the output document generated at your end using the API so that we can compare it to calculate the difference between annotation positions.

@asad.ali,
Thank you for your quick response.
Could you please confirm if “Expected_Approximation output.pdf” is the document which you expect to generate using the API? That is correct.
Below is the coordinates after the transform is applied for 270 degree. which in turn goes off the page and not visible as you can see negative ury highlighted.
Before transform:
llx --> 172.078
lly --> -46.38635
urx --> -303.483
ury --> -27.408
After 270degree transform:
llx --> 819.408
lly --> 172.078
urx --> 838.386
ury --> -303.483

90, 180 and none work as expected. It is just 270 is quite off the grid. Which is causing a major issue for us.
Below lines should work for 270 why the coordinates are transformed or not correctly plotting in the pdf?
var transform = pdfDocument.Pages[i].RotationMatrix.Reverse().Transform(rect);
freetext = new FreeTextAnnotation(pdfDocument.Pages[i], transform, default_appearance);

Please do the needful.

Thank you,
A