到目前为止,我一直在接受PDF的生活:
我只想拥有一个像Excel那样显示它们的PDF文件。例:
如何正确显示iTextSharp值?我只想让我的话直接排成一行,而不是将它们分成两半。我的按钮代码如下所示:A4。按照@JoshPart
的建议旋转Dim pdfTable As New PdfPTable(DataGridView1.ColumnCount)
'Add Title of PDF
Dim cell1 As New PdfPCell(New Phrase(StudentToolStripMenuItem.Text))
cell1.Colspan = 28
cell1.Border = 0
cell1.HorizontalAlignment = 1
pdfTable.AddCell(cell1)
'Creating iTextSharp Table from the DataTable data
pdfTable.DefaultCell.Padding = 3
pdfTable.WidthPercentage = 100
pdfTable.HorizontalAlignment = Element.ALIGN_LEFT
pdfTable.DefaultCell.BorderWidth = 1
'Adding Header row
For Each column As DataGridViewColumn In DataGridView1.Columns
Dim cell As New PdfPCell(New Phrase(column.HeaderText))
cell.BackgroundColor = New iTextSharp.text.BaseColor(240, 240, 240)
pdfTable.AddCell(cell)
Next
'Adding DataRow
For Each row As DataGridViewRow In DataGridView1.Rows
If row.IsNewRow = False Then
For Each cell As DataGridViewCell In row.Cells
pdfTable.AddCell(cell.Value.ToString())
Next
End If
Next
'Exporting to PDF
Dim folderPath As String = "C:\Mickosis\Class Manager\PDFs\"
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
Using stream As New FileStream(folderPath & StudentToolStripMenuItem.Text & ".pdf", FileMode.Create)
Dim pdfDoc As New Document(PageSize.A4.Rotate, 5.0F, 5.0F, 5.0F, 5.0F)
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
pdfDoc.Add(pdfTable)
pdfDoc.Close()
stream.Close()
End Using
MsgBox("PDF Created", , msgboxtitle)
答案 0 :(得分:2)
A4纸旋转宽度为280毫米或11英寸。您正试图在其上放置28列,这意味着每列可以有10毫米或3/8英寸的空间,并且不包括任何单元格填充或列边框。正如您所见,在正常的可读字体大小上,您只能在一行中容纳3到5个字符。
一种选择是删除字体大小,可能下降到不可读的内容,如3或4。
Dim HeaderFont = FontFactory.GetFont(FontFactory.HELVETICA, 4)
Dim cell As New PdfPCell(New Phrase("Your Text", HeaderFont))
但是,更好的选择是使用更大的页面大小。如果你也放弃了字体大小,你可能可以使用A2,但你可能需要去A1或者甚至可能是A0。
Dim Doc As New Document(PageSize.A1.Rotate, 5.0F, 5.0F, 5.0F, 5.0F)
当然,当你在屏幕上看这个时,你将不得不放大,当你打印它时,你需要缩小到较小的纸张,你可能无法读取它,但那是28个真正长文本的列看起来像。
我谈到的第三个选项是垂直拆分你的桌子。虽然可能,但它不是自动的,如果您的值的长度经常变化,则可能非常脆弱。 Bruno has a great post on doing it here.
Josh Part谈到的第四个选项是旋转文本或垂直绘制。您可以看到writing vertically here的示例,尽管这非常复杂。但是,旋转文本实际上非常简单,只需将单元格上的Rotation
属性设置为90的倍数即可。
cell.Rotation = 90