且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在vb.net中计算pdf页面的行数?

更新时间:2022-12-09 20:06:59

.Net本身不提供与PDF文档进行交互的任何API.
您正在使用哪个库来创建PDF(实用程序).如果它们提供了返回页数的任何方法,则可能需要查看其文档.
.Net on its own does not provide any API to interact with PDF documents.
Which library are you using to create PDF (util?). You might want to look into its documentation if they provide any method whic returns the number of pages.


是cls util和dll itextsharp
这是我的代码
公共类ClsUtils
继承PdfPageEventHelper

公共函数createPDF(ByVal标题为[String],ByVal标头为[String](),ByVal列表为ArrayList)作为Byte()
FontFactory.RegisterDirectory("C:\ WINDOWS \ Fonts")
昏暗的文档作为新文档
昏暗的流作为新的MemoryStream
Dim writer As PdfWriter = PdfWriter.GetInstance(doc,stream)
doc.Open()
writer.PageEvent = Me
暗淡的titleFont作为iTextSharp.text.Font = FontFactory.GetFont("Tahoma",BaseFont.IDENTITY_H,8,iTextSharp.text.Font.BOLD,iTextSharp.text.Color.BLACK)
作为新的ColumnText(writer.DirectContent)变暗
ct.R​​unDirection = PdfWriter.RUN_DIRECTION_RTL
ct.SetSimpleColumn(20、5、400、830)
ct.AddText(新词组(标题,标题字体))
ct.Go()

昏暗的headerFont作为iTextSharp.text.Font = FontFactory.GetFont("Tahoma",BaseFont.IDENTITY_H,8,iTextSharp.text.Font.BOLD,iTextSharp.text.Color.BLACK)
Dim detailsFont as iTextSharp.text.Font = FontFactory.GetFont("Tahoma",BaseFont.IDENTITY_H,7,iTextSharp.text.Font.NORMAL,iTextSharp.text.Color.BLACK)
将Dim表作为新的PdfPTable(DirectCast(list(0),[String]()).Length)
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL
addRowToPdfTable(标题,表格,headerFont)
对于i As Integer = 0列出-计数-1
Dim r As [String]()= DirectCast(list(i),[String]())
addRowToPdfTable(r,table,detailsFont)
下一个
doc.Add(表格)
doc.Close()
昏暗的缓冲区为Byte()= stream.GetBuffer()
stream.Close()
返回缓冲区
最终功能
受保护的子addRowToPdfTable(ByVal行为[String](),ByVal pdfTable为PdfPTable,ByVal字体为iTextSharp.text.Font)
昏暗的pdfCell作为新的PdfPCell
pdfCell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
''以相反的顺序添加单元格作为解决带有RTL表的iTextSharp错误的解决方法
对于i As Integer = row.Length-1到0步骤-1
pdfCell.Phrase =新短语(row(i),字体)
pdfTable.AddCell(pdfCell)
下一个
结束子
公共重载重写Sub OnEndPage(ByVal writer作为PdfWriter,ByVal document作为Document)
如果document.PageNumber = 1,则
返回
如果结束
昏暗的headerFont作为iTextSharp.text.Font = FontFactory.GetFont("Tahoma",BaseFont.IDENTITY_H,8,iTextSharp.text.Font.BOLD,iTextSharp.text.Color.BLACK)
暗淡的titleFont作为iTextSharp.text.Font = FontFactory.GetFont("Tahoma",BaseFont.IDENTITY_H,8,iTextSharp.text.Font.BOLD,iTextSharp.text.Color.BLACK)
作为新的ColumnText(writer.DirectContent)变暗
ct.R​​unDirection = PdfWriter.RUN_DIRECTION_RTL
ct.SetSimpleColumn(20、5、400、830)
ct.AddText(New Phrase("header",titleFont))
ct.Go()

结束子

结束类
yes cls util and the dll itextsharp
this is my code
Public Class ClsUtils
Inherits PdfPageEventHelper

Public Function createPDF(ByVal Title As [String], ByVal header As [String](), ByVal list As ArrayList) As Byte()
FontFactory.RegisterDirectory("C:\WINDOWS\Fonts")
Dim doc As New Document
Dim stream As New MemoryStream
Dim writer As PdfWriter = PdfWriter.GetInstance(doc, stream)
doc.Open()
writer.PageEvent = Me
Dim titleFont As iTextSharp.text.Font = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)
Dim ct As New ColumnText(writer.DirectContent)
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL
ct.SetSimpleColumn(20, 5, 400, 830)
ct.AddText(New Phrase(Title, titleFont))
ct.Go()

Dim headerFont As iTextSharp.text.Font = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)
Dim detailsFont As iTextSharp.text.Font = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 7, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLACK)
Dim table As New PdfPTable(DirectCast(list(0), [String]()).Length)
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL
addRowToPdfTable(header, table, headerFont)
For i As Integer = 0 To list.Count - 1
Dim r As [String]() = DirectCast(list(i), [String]())
addRowToPdfTable(r, table, detailsFont)
Next
doc.Add(table)
doc.Close()
Dim buffer As Byte() = stream.GetBuffer()
stream.Close()
Return buffer
End Function
Protected Sub addRowToPdfTable(ByVal row As [String](), ByVal pdfTable As PdfPTable, ByVal font As iTextSharp.text.Font)
Dim pdfCell As New PdfPCell
pdfCell.RunDirection = PdfWriter.RUN_DIRECTION_RTL
'' add cells in reverse order as a workaround for iTextSharp bug with RTL tables
For i As Integer = row.Length - 1 To 0 Step -1
pdfCell.Phrase = New Phrase(row(i), font)
pdfTable.AddCell(pdfCell)
Next
End Sub
Public Overloads Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)
If document.PageNumber = 1 Then
Return
End If
Dim headerFont As iTextSharp.text.Font = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)
Dim titleFont As iTextSharp.text.Font = FontFactory.GetFont("Tahoma", BaseFont.IDENTITY_H, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.Color.BLACK)
Dim ct As New ColumnText(writer.DirectContent)
ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL
ct.SetSimpleColumn(20, 5, 400, 830)
ct.AddText(New Phrase("header", titleFont))
ct.Go()

End Sub

End Class