Imports System.IO
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim PdfReader As New iTextSharp.text.pdf.PdfReader("C:\Old.PDF")
Dim NumberOfPages As Integer = PdfReader.NumberOfPages
Dim PageSize As Rectangle = PdfReader.GetPageSize(1)
Dim PageSizeW As Single = PageSize.Width
Dim PageSizeH As Single = PageSize.Height
Dim OutputStream As New FileStream("C:\New.PDF", FileMode.Create)
Dim PdfStamper As New iTextSharp.text.pdf.PdfStamper(PdfReader, OutputStream)
Dim PC As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim BF As BaseFont = BaseFont.CreateFont("c:\\windows\\fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)
Dim BC As BaseColor = New BaseColor(0, 0, 0)
Dim i As Integer = 1
While i <= NumberOfPages
PC = PdfStamper.GetUnderContent(i)
PC.BeginText()
PC.SetFontAndSize(BF, 9)
PC.SetColorFill(BC)
PC.SetTextMatrix(PC.PdfDocument.Left, PC.PdfDocument.Top)
'PC.ShowText(Me.TextBox1.Text)
PC.ShowTextAligned(Element.ALIGN_CENTER, Me.TextBox1.Text, (PC.PdfDocument.Right + PC.PdfDocument.Left) / 2, PC.PdfDocument.Top, 0)
PC.SetTextMatrix(PC.PdfDocument.Right, PC.PdfDocument.Top)
PC.ShowTextAligned(Element.ALIGN_RIGHT, "第" & i & "页/共" & NumberOfPages & "页", PC.PdfDocument.Right, PC.PdfDocument.Top, 0)
PC.SetTextMatrix(PC.PdfDocument.Right, PC.PdfDocument.Bottom)
PC.ShowTextAligned(Element.ALIGN_RIGHT, Me.TextBox2.Text & " 第" & i & "页/共" & NumberOfPages & "页", PC.PdfDocument.Right, PC.PdfDocument.Bottom, 0)
PC.EndText()
'画线
PC.SetLineWidth(0)
PC.SetColorStroke(BaseColor.BLACK)
PC.MoveTo(PC.PdfDocument.Left, PC.PdfDocument.Top - 2)
PC.LineTo(PC.PdfDocument.Right, PC.PdfDocument.Top - 2)
PC.Stroke()
'画线
PC.SetColorStroke(BaseColor.BLACK)
PC.MoveTo(PC.PdfDocument.Left, PC.PdfDocument.Bottom + 10)
PC.LineTo(PC.PdfDocument.Right, PC.PdfDocument.Bottom + 10)
PC.Stroke()
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
PdfStamper.Close()
PdfReader.Close()
Catch generatedExceptionName As Exception
End Try
End Sub
End Class
Imports System.Text
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim PdfReader As New iTextSharp.text.pdf.PdfReader("C:\Old.PDF")
Dim NumberOfPages As Integer = PdfReader.NumberOfPages
Dim PageSize As Rectangle = PdfReader.GetPageSize(1)
Dim PageSizeW As Single = PageSize.Width
Dim PageSizeH As Single = PageSize.Height
Dim OutputStream As New FileStream("C:\New.PDF", FileMode.Create)
Dim PdfStamper As New iTextSharp.text.pdf.PdfStamper(PdfReader, OutputStream)
Dim PC As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim BF As BaseFont = BaseFont.CreateFont("c:\\windows\\fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED)
Dim BC As BaseColor = New BaseColor(0, 0, 0)
Dim i As Integer = 1
While i <= NumberOfPages
PC = PdfStamper.GetUnderContent(i)
PC.BeginText()
PC.SetFontAndSize(BF, 9)
PC.SetColorFill(BC)
PC.SetTextMatrix(PC.PdfDocument.Left, PC.PdfDocument.Top)
'PC.ShowText(Me.TextBox1.Text)
PC.ShowTextAligned(Element.ALIGN_CENTER, Me.TextBox1.Text, (PC.PdfDocument.Right + PC.PdfDocument.Left) / 2, PC.PdfDocument.Top, 0)
PC.SetTextMatrix(PC.PdfDocument.Right, PC.PdfDocument.Top)
PC.ShowTextAligned(Element.ALIGN_RIGHT, "第" & i & "页/共" & NumberOfPages & "页", PC.PdfDocument.Right, PC.PdfDocument.Top, 0)
PC.SetTextMatrix(PC.PdfDocument.Right, PC.PdfDocument.Bottom)
PC.ShowTextAligned(Element.ALIGN_RIGHT, Me.TextBox2.Text & " 第" & i & "页/共" & NumberOfPages & "页", PC.PdfDocument.Right, PC.PdfDocument.Bottom, 0)
PC.EndText()
'画线
PC.SetLineWidth(0)
PC.SetColorStroke(BaseColor.BLACK)
PC.MoveTo(PC.PdfDocument.Left, PC.PdfDocument.Top - 2)
PC.LineTo(PC.PdfDocument.Right, PC.PdfDocument.Top - 2)
PC.Stroke()
'画线
PC.SetColorStroke(BaseColor.BLACK)
PC.MoveTo(PC.PdfDocument.Left, PC.PdfDocument.Bottom + 10)
PC.LineTo(PC.PdfDocument.Right, PC.PdfDocument.Bottom + 10)
PC.Stroke()
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
PdfStamper.Close()
PdfReader.Close()
Catch generatedExceptionName As Exception
End Try
End Sub
End Class
【iTextSharp_PDF加文字.zip】点击下载此文件
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |