Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Dec 29, 2021 6:58 am

im adding an annotation to my existing pdf document but after im adding twice the annotation is overwritten here's my code:

Dim doc As New PdfDocument(m_sFilenameSelected)
Dim doc2 As New PdfDocument
Dim oAnn As PdfFreeTextAnnotation

doc2.InsertPage(doc, 0)
Dim page As PdfPageBase = doc2.Pages(0)
Dim pc As PointConverter = New PointConverter()
Dim myPoint As Point = CType(pc.ConvertFromString(lblLocation.Text), Point)
'Draw the text
page.Canvas.DrawString(RichTextBox1.Text, New PdfFont(PdfFontFamily.Helvetica, 30.0F), New PdfSolidBrush(Color.Black), myPoint)


oAnn = New PdfFreeTextAnnotation(New Rectangle(0, 0, 100, 100))
oAnn.Location = myPoint
With oAnn
.Border = New PdfAnnotationBorder(1)
.Color = Color.Transparent
.MarkupText = RichTextBox1.Text & RichTextBox1.Text
.Flags = PdfAnnotationFlags.Default
End With
page.AnnotationsWidget.Add(oAnn)
doc2.SaveToFile(m_sFilenameSelected, FileFormat.PDF)
PdfDocumentViewer1.LoadFromFile(m_sFilenameSelected)
PdfDocumentViewer1.Refresh()
RichTextBox1.Clear()
lblLocation.Text = ""

Twentyf4
 
Posts: 18
Joined: Thu Dec 16, 2021 2:23 am

Wed Dec 29, 2021 9:18 am

Hello,

Thank you for your inquiry.
Please save doc2 after "doc2. InsertPage(doc, 0)", then load doc2, and then perform related operations, so that the old annotations will not be overwritten. The sample code is as follows.
Code: Select all
Dim doc As New PdfDocument(m_sFilenameSelected)
Dim doc2 As New PdfDocument
Dim oAnn As PdfFreeTextAnnotation

doc2. InsertPage(doc, 0)
Dim memoryStream As MemoryStream = New MemoryStream
doc2.SaveToStream(memoryStream)
doc2.LoadFromStream(memoryStream)
Dim page As PdfPageBase = doc2.Pages(0)
...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 31, 2021 1:10 am

Thanks for the help :) I really appreciate it. but when i add a annotation on a pdf file, It saves a only the page that has an annotation and the remaining page will not be save or i think its deleted?

This is my code :
Dim doc As New PdfDocument(m_sFilenameSelected)
Dim doc2 As New PdfDocument
Dim oAnn As PdfFreeTextAnnotation
lblLocation.Text = e.X & "," & e.Y


doc2.InsertPage(doc, 0)
Dim memoryStream As MemoryStream = New MemoryStream
doc2.SaveToStream(memoryStream)
doc2.LoadFromStream(memoryStream)
Dim page As PdfPageBase = doc2.Pages(0)
Dim pc As PointConverter = New PointConverter()
Dim myPoint As Point = CType(pc.ConvertFromString(lblLocation.Text), Point)
'Draw the text
page.Canvas.DrawString(RichTextBox1.Text, New PdfFont(PdfFontFamily.Helvetica, 30.0F), New PdfSolidBrush(Color.Black), myPoint)


oAnn = New PdfFreeTextAnnotation(New Rectangle(0, 0, 100, 100))
oAnn.Location = myPoint
With oAnn
.Border = New PdfAnnotationBorder(1)
.Color = Color.Transparent
.MarkupText = RichTextBox1.Text & RichTextBox1.Text
.Flags = PdfAnnotationFlags.Default
End With
page.AnnotationsWidget.Add(oAnn)
doc2.SaveToFile(m_sFilenameSelected, FileFormat.PDF)
PdfDocumentViewer1.LoadFromFile(m_sFilenameSelected)
PdfDocumentViewer1.Refresh()
RichTextBox1.Clear()
lblLocation.Text = ""

Twentyf4
 
Posts: 18
Joined: Thu Dec 16, 2021 2:23 am

Fri Dec 31, 2021 3:46 am

Hello,

Thank you for your feedback.
Kindly note that in doc2 you only copied the first page of the doc, so the resulting file has only one page. Please use the following code to copy all the pages of doc to doc2.
Code: Select all
Dim doc As PdfDocument = New PdfDocument("2233.pdf")
Dim doc2 As PdfDocument = New PdfDocument()
'Copy all pages of doc to doc2
doc2. InsertPageRange(doc, 0, doc.Pages.Count - 1)
Dim memoryStream As MemoryStream = New MemoryStream()
doc2.SaveToStream(memoryStream)
doc2.LoadFromStream(memoryStream)
Dim page As PdfPageBase = doc2.Pages(0)
...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 31, 2021 5:38 am

Thank you very much for the solution. but when i add the text annotation on the another page it will be added on the another page.? can you help me with this problem? sorry for the inconvenience caused :(

Twentyf4
 
Posts: 18
Joined: Thu Dec 16, 2021 2:23 am

Fri Dec 31, 2021 5:40 am

by the way this is my code im so sorry annika for the inconvenience .


lblLocation.Text = e.X & "," & e.Y

Dim doc As PdfDocument = New PdfDocument(m_sFilenameSelected)
Dim doc2 As PdfDocument = New PdfDocument()
'Copy all pages of doc to doc2
doc2.InsertPageRange(doc, 0, doc.Pages.Count - 1)
Dim memoryStream As MemoryStream = New MemoryStream()
doc2.SaveToStream(memoryStream)
doc2.LoadFromStream(memoryStream)
Dim oAnn As PdfFreeTextAnnotation


Dim page As PdfPageBase = doc2.Pages(0)
Dim pc As PointConverter = New PointConverter()
Dim myPoint As Point = CType(pc.ConvertFromString(lblLocation.Text), Point)
'Draw the text
'page.Canvas.DrawString(RichTextBox1.Text, New PdfFont(PdfFontFamily.Helvetica, 30.0F), New PdfSolidBrush(Color.Black), myPoint)


oAnn = New PdfFreeTextAnnotation(New Rectangle(0, 0, 100, 100))
oAnn.Location = myPoint
With oAnn
.Border = New PdfAnnotationBorder(1)
.Color = Color.Transparent
.MarkupText = RichTextBox1.Text & RichTextBox1.Text
.Flags = PdfAnnotationFlags.Default


End With
page.AnnotationsWidget.Add(oAnn)
doc2.SaveToFile(m_sFilenameSelected, FileFormat.PDF)
PdfDocumentViewer1.LoadFromFile(m_sFilenameSelected)
PdfDocumentViewer1.Refresh()
RichTextBox1.Clear()
lblLocation.Text = ""

Twentyf4
 
Posts: 18
Joined: Thu Dec 16, 2021 2:23 am

Fri Dec 31, 2021 10:03 am

Hello,

Thank you for your feedback.
Do you mean that the added text annotation is not displayed on the correct page? But I tested your code and found that the newly added text annotation is displayed on the first page of the PDF file. This display is correct. If my understanding is wrong, please provide more details.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Fri Dec 31, 2021 10:20 am

I mean if you add annotation on page 2 the annotation will be added on page 1

Twentyf4
 
Posts: 18
Joined: Thu Dec 16, 2021 2:23 am

Tue Jan 04, 2022 1:59 am

Hello,

Thank you for your feedback.
I tested your code again and found that if you add an annotation on the second (or third) page, the annotation will be added to the second (or third) page correctly. In order to help us investigate and solve your problem quickly and effectively, please provide your input PDF file. You could attach it here or send it to us via email ([email protected]). Thanks in advance.
Code: Select all
 Dim doc As PdfDocument = New PdfDocument(m_sFilenameSelected)
 Dim doc2 As PdfDocument = New PdfDocument()
 'Copy all pages of doc to doc2
 doc2.InsertPageRange(doc, 0, doc.Pages.Count - 1)
 Dim memoryStream As MemoryStream = New MemoryStream()
 doc2.SaveToStream(memoryStream)
 doc2.LoadFromStream(memoryStream)
 Dim oAnn As PdfFreeTextAnnotation


 Dim page As PdfPageBase = doc2.Pages(1)
 'Dim page As PdfPageBase = doc2.Pages(2)
...

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF