Hello,
Thanks for your feedback. Actually I have checked the export/import annotations but it doesn’t fulfill our requirement, as we need to zoom out the page and then add additional contents to it.
The issue come from the fact that during the conversion/resizing of the page, the annotations cannot be kept. So that we have to process all the annotations and then convert/resize them and add them to the newly created page. We have managed to make it work concerning the inkannotation, but for other types of annotations like the stampannotation or the squareAnnotation, I cannot find the contents which I should copy.
Please check the codes as below:
'pdfPath : file path
Dim pdfDoc As Aspose.Pdf.Document = New Aspose.Pdf.Document(pdfPath)
Dim mNewPtoDoc As New Aspose.Pdf.Document()
Dim newPage As Aspose.Pdf.Page = mNewPtoDoc.Pages.Add()
newPage.PageInfo.Margin = New MarginInfo(0, 0, 0, 0)
newPage.SetPageSize(595, 842)
Dim page As Aspose.Pdf.Page = pdfDoc.Pages(2)
'Normally we need to zoom out the page here, but it seem that we cannot keep the annotations when creating a PdfPageStamp with a page.
Dim pageStamp As New Aspose.Pdf.PdfPageStamp(page)
pageStamp.Width = 595
pageStamp.Height = 842
pageStamp.XIndent = 0
pageStamp.YIndent = 0
newPage.AddStamp(pageStamp)
'Copy original annotation to new page
If page.Annotations.Count > 0 Then
Dim annProcessedList As New List(Of String)
For Each ann As Annotations.Annotation In page.Annotations
Select Case ann.AnnotationType
Case Annotations.AnnotationType.Ink
Dim inkAnn As Annotations.InkAnnotation = CType(ann, Aspose.Pdf.Annotations.InkAnnotation)
Dim rect As Aspose.Pdf.Rectangle = ann.Rect
Dim inkList As IList(Of Aspose.Pdf.Point()) = inkAnn.InkList
Dim newAnn As New Aspose.Pdf.Annotations.InkAnnotation(newPage, rect, inkList)
newAnn.Name = inkAnn.FullName
newAnn.Title = inkAnn.Title
newAnn.Contents = inkAnn.Contents
newAnn.Subject = inkAnn.Subject
newPage.Annotations.Add(newAnn)
Case Annotations.AnnotationType.Square
Dim stAnn As Annotations.SquareAnnotation = CType(ann, Aspose.Pdf.Annotations.SquareAnnotation)
Dim rect As Aspose.Pdf.Rectangle = ann.Rect
Dim newAnn As New Aspose.Pdf.Annotations.SquareAnnotation(newPage, rect)
newAnn.Name = stAnn.FullName
newAnn.Title = stAnn.Title
newAnn.Contents = stAnn.Contents
'We have for example an annotation with a signature, unfortunately I cannot find anything in the SquareAnnotation/StampAnnotation, the only thing I noticed is that the Appearance, but still it's not the annotation content.
'Dim form As XForm = stAnn.Appearance.Item("N")
'Dim newForm As XForm = XForm.CreateNewForm(newPage, newDoc)
'newForm.Contents.Add(New Aspose.Pdf.Operator.GSave())
'newForm.Contents.Add(New Aspose.Pdf.Operator.ConcatenateMatrix(100, 0, 0, 100, 1, 1))
'newForm.Resources.Images.Add(form.Resources.Images.Item(1))
'newForm.Contents.Add(New Aspose.Pdf.Operator.Do(newForm.Resources.Images.Item(1).Name))
'newForm.Contents.Add(New Aspose.Pdf.Operator.GRestore())
'newAnn.Appearance.Add("N", newForm)
newPage.Annotations.Add(newAnn)
Case Annotations.AnnotationType.Stamp
Dim stAnn As Annotations.StampAnnotation = CType(ann, Aspose.Pdf.Annotations.StampAnnotation)
Dim rect As Aspose.Pdf.Rectangle = AsposeRenditionHelper.CalculateRectangleCoordinate(ann.Rect, pageWidth, pageHeight, pageStamp.Width, pageStamp.Height, pageStamp.XIndent, pageStamp.YIndent)
Dim newAnn As New Aspose.Pdf.Annotations.StampAnnotation(newPage, rect)
newAnn.Name = stAnn.FullName
newAnn.Title = stAnn.Title
newAnn.Contents = stAnn.Contents
newAnn.Image = stAnn.Image
newPage.Annotations.Add(newAnn)
Case Else
'Other type of annotations
End Select
Next
End If
mNewPtoDoc.Save(outputPath)
I have aready simplified the codes which we use to copy an annotation to a new page in same size, else there are too many codes to copy.
I attahced this example file with different type of annotations, inkAnnotation, SquareAnnotation and StampAnnotation.
Test Record of Settlement Cute1 comment.pdf (110.4 KB)
Can you please provide some details how I can copy the SquareAnnotation and the StampAnnotation to the newly generated document?
Thanks,
Rui