We are extracting and storing PDF comments made with adobe pdf in database. We are doing some manipulations needed by system and we are re importing PDF markups back to pdf document.
Everything was fine until users started to use “Text Comment” in adobe acrobat. After importing PDF markups “Text Comment” type changes to “Text Box” type. Would be OK, but TextBox has border and not all text is visible after opening pdf document you have to resize manually for text to be visible. I have attached file test_withAnottations.pdf (552.4 KB), and output.pdf (288.7 KB), where you can see converted markups.
Is there anything we could do about this?
Extracted some piece of code and also tried with 17.9.0.0 Aspose.PDF version, same results.
'---------------------------------------------------------
Dim pFilePath = “c:/temp/test_withAnottations.pdf”
Dim vOutputStream As New MemoryStream
Dim vResultStream As New MemoryStream
Dim gAnnoType As Enum = {AnnotationType.Caret, AnnotationType.Circle, AnnotationType.FileAttachment, AnnotationType.FreeText, AnnotationType.Highlight,
AnnotationType.Ink, AnnotationType.Line, AnnotationType.Link, AnnotationType.Movie, AnnotationType.Polygon, AnnotationType.PolyLine,
AnnotationType.PrinterMark, AnnotationType.Screen, AnnotationType.Popup, AnnotationType.Square,
AnnotationType.Squiggly, AnnotationType.Stamp, AnnotationType.StrikeOut, AnnotationType.Text, AnnotationType.TrapNet,
AnnotationType.Underline, AnnotationType.Watermark, AnnotationType.Widget}
Dim annotationEditor As New PdfAnnotationEditor()
annotationEditor.BindPdf(pFilePath)
Dim vBuffer As Byte() = {}
Using vMemStream As New MemoryStream
annotationEditor.ExportAnnotationsXfdf(vMemStream, 0, 1, gAnnoType)
vBuffer = vMemStream.GetBuffer
vOutputStream.Position = 0
vOutputStream.CopyTo(vMemStream)
End Using
Dim vInitialString As String = Encoding.UTF8.GetString(vBuffer)
Dim vBegin As Integer = vInitialString.IndexOf("<", 0)
vInitialString = vInitialString.Substring(vBegin)
Dim vXMLString As New String(vInitialString.ToCharArray)
Dim vXMLStringReader As New StringReader(vXMLString)
Dim vXMLDoc As New XmlDocument
InitializeAnnotationsTable()
vXMLDoc.Load(vXMLStringReader)
Dim vXMLAnnots As XmlNodeList = vXMLDoc.GetElementsByTagName("annots")
Dim vAnnotationsTypes As New Dictionary(Of String, String)
Dim vRow As DataRow
For Each vXMLAnnot As XmlNode In vXMLAnnots
For Each vXMLNode As XmlNode In vXMLAnnot.ChildNodes
If vXMLNode.OuterXml.Trim.Length > 0 Then
Dim vAnnotationXMLString As String = vXMLNode.OuterXml
vRow = AnnotationsTable.NewRow()
vRow("XmlAnnotation") = vAnnotationXMLString
AnnotationsTable.Rows.Add(vRow)
End If
Next
Next
Dim vXMLSb As New StringBuilder("<?xml version=""1.0"" encoding=""utf-8""?>")
vXMLSb.Append("<xfdf xmlns=""http://ns.adobe.com/xfdf/"" xml:space=""preserve"">")
vXMLSb.Append("<annots>")
For Each vDRow As DataRow In AnnotationsTable.Rows
vXMLSb.Append(vDRow("XmlAnnotation"))
Next
vXMLSb.Append("</annots></xfdf>")
Dim vXMLBytes As Byte() = Encoding.UTF8.GetBytes(vXMLSb.ToString)
Dim vXMLStream = New MemoryStream(vXMLBytes)
Dim AnnotationEditor2 = New PdfAnnotationEditor()
AnnotationEditor2.BindPdf("c:/temp/test.pdf")
AnnotationEditor2.ImportAnnotationFromXfdf(vXMLStream, gAnnoType)
vXMLStream.Flush()
AnnotationEditor2.Save("c:/temp/output.pdf")
annotationEditor.Close()
Console.WriteLine("Done")
Private Sub InitializeAnnotationsTable()
AnnotationsTable = New DataTable
AnnotationsTable.Columns.Add(New DataColumn(“XmlAnnotation”, System.Type.GetType(“System.String”)))
Dim vColumn As New DataColumn(“AnnotationText”, System.Type.GetType(“System.String”))
vColumn.AllowDBNull = True
AnnotationsTable.Columns.Add(vColumn)
vColumn = New DataColumn(“IsTextMarkup”, System.Type.GetType(“System.Boolean”))
vColumn.DefaultValue = 0
AnnotationsTable.Columns.Add(vColumn)
AnnotationsTable.Columns.Add(New DataColumn(“AnnotationKey”, System.Type.GetType(“System.String”)))
End Sub
'---------------------------------------------------
test.pdf (260.0 KB)