Vb.net 列表框开头缺少取消选择的显示项目(iTextSharp)

Vb.net 列表框开头缺少取消选择的显示项目(iTextSharp),vb.net,listbox,itext,adobe,reader,Vb.net,Listbox,Itext,Adobe,Reader,在Acroform PDF文档中使用iTextSharp生成或重新生成多选列表框时,我在Adobe Reader DC中得到了不想要的结果 问题:在Adobe Reader DC中查看修改后的PDF时,PDF表单在列表框的开头缺少取消选择的显示项。例如:“一”、“二”、“三”、“四”、“五”是列表项;选择“二”和“四”;然后列表框顶部缺少前面的“一”等项。列表框中显示的第一项从第一个选择开始,在本例中为“二”。(请参阅Adobe Reader DC屏幕截图) 仅供参考:使用Adobe Reade

在Acroform PDF文档中使用iTextSharp生成或重新生成多选列表框时,我在Adobe Reader DC中得到了不想要的结果

问题:在Adobe Reader DC中查看修改后的PDF时,PDF表单在列表框的开头缺少取消选择的显示项。例如:“一”、“二”、“三”、“四”、“五”是列表项;选择“二”和“四”;然后列表框顶部缺少前面的“一”等项。列表框中显示的第一项从第一个选择开始,在本例中为“二”。(请参阅Adobe Reader DC屏幕截图)

仅供参考:使用Adobe Reader DC,当我在列表框中选择不同的字段选择,然后在列表框外单击时,列表框字段将恢复正常外观,显示所有项目。在Adobe Acrobat Professional 8中打开修改后的PDF时,我无法重现此行为,并且所有字段项目都可见且正确y选中。将PDF转换为BMP或PNG时,此缺少列表项行为也可以在GhostScript中复制

请回答我的问题:如果这是一个iTextSharp问题,或者如果我的语法不正确,你能为我提供一个解决方案吗?你能告诉我这个行为是否可以用你的Adobe Reader DC复制吗

谢谢你的支持

已修改的Acroform PDF文档,存在以下问题:

Adobe Reader DC屏幕截图:
(来源:)

VB.NET代码(v3.5–Windows应用程序):


可见列表从第二个条目开始的原因是iTextSharp从第一个选定条目开始绘制列表

这是一种针对列表的优化,列表中的条目比固定文本框区域中显示的条目多(可能多),因此显示的条目至少包含一个感兴趣的条目,即选中的条目

遗憾的是,这种优化并不考虑这是否意味着在底部留下一些空行,而在完全符合文本框的列表中,甚至没有滚动条或任何东西。


但iTextSharp也提供了一种禁用此优化的方法:您可以显式地手动设置第一个可见项:

txtField.ChoiceSelections = selIndex
txtField.VisibleTopChoice = 0 ' Top visible choice is start of list!
Dim listField As PdfFormField = txtField.GetListField
添加此中间行将使生成的外观从第一个列表值开始

Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Public Class listboxTest
    Private Sub RunTest()
        Dim cList As New listboxTest()
        Dim fn As String = Application.StartupPath.ToString.TrimEnd("\") & "\listbox-error.pdf"
        Dim b() As Byte = cList.addListBox(System.IO.File.ReadAllBytes(fn), New iTextSharp.text.Rectangle(231.67, 108.0, 395.67, 197.0), "ListBox1", "ListBox1", 1)
        File.WriteAllBytes(fn, b)
        Process.Start(fn)
    End Sub
    Public Function addListBox(ByVal pdfBytes() As Byte, ByVal newRect As Rectangle, ByVal newFldName As String, ByVal oldfldname As String, ByVal pg As Integer) As Byte()
        Dim pdfReaderDoc As New PdfReader(pdfBytes)
        Dim m As New System.IO.MemoryStream
        Dim b() As Byte = Nothing
        Try
            With New PdfStamper(pdfReaderDoc, m)
                Dim txtField As iTextSharp.text.pdf.TextField
                txtField = New iTextSharp.text.pdf.TextField(.Writer, newRect, newFldName)
                txtField.TextColor = BaseColor.BLACK
                txtField.BackgroundColor = BaseColor.WHITE
                txtField.BorderColor = BaseColor.BLACK
                txtField.FieldName = newFldName 'ListBox1
                txtField.Alignment = 0 'LEFT
                txtField.BorderStyle = 0 'SOLID
                txtField.BorderWidth = 1.0F 'THIN
                txtField.Visibility = TextField.VISIBLE
                txtField.Rotation = 0 'None
                txtField.Box = newRect '231.67, 108.0, 395.67, 197.0
                Dim opt As New PdfArray
                Dim ListBox_ItemDisplay As New List(Of String)
                ListBox_ItemDisplay.Add("One")
                ListBox_ItemDisplay.Add("Two")
                ListBox_ItemDisplay.Add("Three")
                ListBox_ItemDisplay.Add("Four")
                ListBox_ItemDisplay.Add("Five")
                Dim ListBox_ItemValue As New List(Of String)
                ListBox_ItemValue.Add("1X")
                ListBox_ItemValue.Add("2X")
                ListBox_ItemValue.Add("3X")
                ListBox_ItemValue.Add("4X")
                ListBox_ItemValue.Add("5X")
                txtField.Options += iTextSharp.text.pdf.TextField.MULTISELECT
                Dim selIndex As New List(Of Integer)
                Dim selValues As New List(Of String)
                selIndex.Add(CInt(1)) ' SELECT #1 (index)
                selIndex.Add(CInt(3)) ' SELECT #3 (index)
                txtField.Choices = ListBox_ItemDisplay.ToArray
                txtField.ChoiceExports = ListBox_ItemValue.ToArray
                txtField.ChoiceSelections = selIndex
                Dim listField As PdfFormField = txtField.GetListField
                If Not String.IsNullOrEmpty(oldfldname & "") Then
                    .AcroFields.RemoveField(oldfldname, pg)
                End If
                .AddAnnotation(listField, pg)
                .Writer.CloseStream = False
                .Close()
                If m.CanSeek Then
                    m.Position = 0
                End If
                b = m.ToArray
                m.Close()
                m.Dispose()
                pdfReaderDoc.Close()
            End With
            Return b.ToArray
        Catch ex As Exception
            Err.Clear()
        Finally
            b = Nothing
        End Try
        Return Nothing
    End Function
End Class
txtField.ChoiceSelections = selIndex
txtField.VisibleTopChoice = 0 ' Top visible choice is start of list!
Dim listField As PdfFormField = txtField.GetListField