Imports System.IO
Imports System.Text
Imports iTextSharp.text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddWaterMark(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text)
End Sub
Public Sub AddWaterMark(ByVal InputFilePath As String, ByVal OutputFilePath As String, ByVal WaterMarkImagePath As String)
Try
Dim PdfReader As New iTextSharp.text.pdf.PdfReader(InputFilePath)
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(OutputFilePath, FileMode.Create)
Dim PdfStamper As New iTextSharp.text.pdf.PdfStamper(PdfReader, OutputStream)
Dim WaterMarkContent As iTextSharp.text.pdf.PdfContentByte
Dim image As Image = iTextSharp.text.Image.GetInstance(WaterMarkImagePath)
'坐标原点在页面的左下角
'image.SetAbsolutePosition((PageSizeW - image.Width) / 2, (PageSizeH - image.Height) / 2) '页面居中
image.SetAbsolutePosition(PageSizeW - image.Width, PageSizeH - image.Height) '页面右上角
Dim i As Integer = 1
While i <= NumberOfPages
WaterMarkContent = PdfStamper.GetUnderContent(i)
WaterMarkContent.AddImage(image)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
pdfStamper.Close()
pdfReader.Close()
Catch generatedExceptionName As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim OpenD As OpenFileDialog = New OpenFileDialog
OpenD.Filter = "PDF文件(*.PDF)|*.PDF"
If OpenD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox1.Text = OpenD.FileName
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim SaveD As SaveFileDialog = New SaveFileDialog
SaveD.Filter = "PDF文件(*.PDF)|*.PDF"
If SaveD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox2.Text = SaveD.FileName
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim OpenD As OpenFileDialog = New OpenFileDialog
OpenD.Filter = "图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png"
If OpenD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox3.Text = OpenD.FileName
End If
End Sub
End Class
Imports System.Text
Imports iTextSharp.text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddWaterMark(Me.TextBox1.Text, Me.TextBox2.Text, Me.TextBox3.Text)
End Sub
Public Sub AddWaterMark(ByVal InputFilePath As String, ByVal OutputFilePath As String, ByVal WaterMarkImagePath As String)
Try
Dim PdfReader As New iTextSharp.text.pdf.PdfReader(InputFilePath)
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(OutputFilePath, FileMode.Create)
Dim PdfStamper As New iTextSharp.text.pdf.PdfStamper(PdfReader, OutputStream)
Dim WaterMarkContent As iTextSharp.text.pdf.PdfContentByte
Dim image As Image = iTextSharp.text.Image.GetInstance(WaterMarkImagePath)
'坐标原点在页面的左下角
'image.SetAbsolutePosition((PageSizeW - image.Width) / 2, (PageSizeH - image.Height) / 2) '页面居中
image.SetAbsolutePosition(PageSizeW - image.Width, PageSizeH - image.Height) '页面右上角
Dim i As Integer = 1
While i <= NumberOfPages
WaterMarkContent = PdfStamper.GetUnderContent(i)
WaterMarkContent.AddImage(image)
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
End While
pdfStamper.Close()
pdfReader.Close()
Catch generatedExceptionName As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim OpenD As OpenFileDialog = New OpenFileDialog
OpenD.Filter = "PDF文件(*.PDF)|*.PDF"
If OpenD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox1.Text = OpenD.FileName
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim SaveD As SaveFileDialog = New SaveFileDialog
SaveD.Filter = "PDF文件(*.PDF)|*.PDF"
If SaveD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox2.Text = SaveD.FileName
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim OpenD As OpenFileDialog = New OpenFileDialog
OpenD.Filter = "图像文件(*.jpg;*.jpg;*.jpeg;*.gif;*.png)|*.jpg;*.jpeg;*.gif;*.png"
If OpenD.ShowDialog = Windows.Forms.DialogResult.OK Then
Me.TextBox3.Text = OpenD.FileName
End If
End Sub
End Class
【iTextSharp_PDF加图片水印.rar】点击下载此文件
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports iTextSharp.text.pdf
Imports System.IO
Imports iTextSharp.text
创建一个显示指定图片的pdf
''' <summary>
''' 创建一个显示指定图片的pdf
''' </summary>
''' <param name="picPdfPath"></param>
''' <param name="picPath"></param>
''' <returns></returns>
Public Shared Function CreatePDFByPic(picPdfPath As String, picPath As String) As Boolean
'新建一个文档
Dim doc As New Document()
Try
'建立一个书写器(Writer)与document对象关联
PdfWriter.GetInstance(doc, New FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite))
'打开一个文档
doc.Open()
'向文档中添加内容
Dim img As Image = Image.GetInstance(picPath)
'img.SetAbsolutePosition();
doc.Add(img)
Return True
Catch ex As Exception
Return False
Throw ex
Finally
If doc IsNot Nothing Then
doc.Close()
End If
End Try
End Function
''' 创建一个显示指定图片的pdf
''' </summary>
''' <param name="picPdfPath"></param>
''' <param name="picPath"></param>
''' <returns></returns>
Public Shared Function CreatePDFByPic(picPdfPath As String, picPath As String) As Boolean
'新建一个文档
Dim doc As New Document()
Try
'建立一个书写器(Writer)与document对象关联
PdfWriter.GetInstance(doc, New FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite))
'打开一个文档
doc.Open()
'向文档中添加内容
Dim img As Image = Image.GetInstance(picPath)
'img.SetAbsolutePosition();
doc.Add(img)
Return True
Catch ex As Exception
Return False
Throw ex
Finally
If doc IsNot Nothing Then
doc.Close()
End If
End Try
End Function
加图片水印
''' <summary>
''' 加图片水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="ModelPicName"></param>
''' <param name="top"></param>
''' <param name="left"></param>
''' <returns></returns>
Public Shared Function PDFWatermark(inputfilepath As String, outputfilepath As String, ModelPicName As String, top As Single, left As Single) As Boolean
'throw new NotImplementedException();
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
Dim numberOfPages As Integer = pdfReader.NumberOfPages
Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
Dim width As Single = psize.Width
Dim height As Single = psize.Height
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
Dim waterMarkContent As PdfContentByte
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ModelPicName)
‘image.GrayFill = 20
‘无效
'透明度,灰色填充
'image.Rotation//旋转
'image.RotationDegrees//旋转角度
'水印的位置
If left < 0 Then
left = width / 2 - image.Width + left
End If
'image.SetAbsolutePosition(left, (height - image.Height) - top);
image.SetAbsolutePosition(left, (height / 2 - image.Height) - top)
'每一页加水印,也可以设置某一页加水印
Dim i As Integer = 1
While i <= numberOfPages
'waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印
waterMarkContent = pdfStamper.GetOverContent(i)
'内容上层加水印
waterMarkContent.AddImage(image)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
'strMsg = "success";
Return True
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Function
''' 加图片水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="ModelPicName"></param>
''' <param name="top"></param>
''' <param name="left"></param>
''' <returns></returns>
Public Shared Function PDFWatermark(inputfilepath As String, outputfilepath As String, ModelPicName As String, top As Single, left As Single) As Boolean
'throw new NotImplementedException();
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
Dim numberOfPages As Integer = pdfReader.NumberOfPages
Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
Dim width As Single = psize.Width
Dim height As Single = psize.Height
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
Dim waterMarkContent As PdfContentByte
Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(ModelPicName)
‘image.GrayFill = 20
‘无效
'透明度,灰色填充
'image.Rotation//旋转
'image.RotationDegrees//旋转角度
'水印的位置
If left < 0 Then
left = width / 2 - image.Width + left
End If
'image.SetAbsolutePosition(left, (height - image.Height) - top);
image.SetAbsolutePosition(left, (height / 2 - image.Height) - top)
'每一页加水印,也可以设置某一页加水印
Dim i As Integer = 1
While i <= numberOfPages
'waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印
waterMarkContent = pdfStamper.GetOverContent(i)
'内容上层加水印
waterMarkContent.AddImage(image)
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
'strMsg = "success";
Return True
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Function
添加普通偏转角度文字水印
''' <summary>
''' 添加普通偏转角度文字水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="waterMarkName"></param>
''' <param name="permission"></param>
Public Shared Sub setWatermark(inputfilepath As String, outputfilepath As String, waterMarkName As String)
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
Dim total As Integer = pdfReader.NumberOfPages + 1
Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
Dim width As Single = psize.Width
Dim height As Single = psize.Height
Dim content As PdfContentByte
Dim font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim gs As New PdfGState()
Dim i As Integer = 1
While i < total
content = pdfStamper.GetOverContent(i)
'在内容上方加水印
'content = pdfStamper.GetUnderContent(i);//在内容下方加水印
'透明度
gs.FillOpacity = 0.3F
content.SetGState(gs)
'content.SetGrayFill(0.3f);
'开始写入文本
content.BeginText()
content.SetColorFill(BaseColor.LIGHT_GRAY)
content.SetFontAndSize(font, 100)
content.SetTextMatrix(0, 0)
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55)
'content.SetColorFill(BaseColor.BLACK);
'content.SetFontAndSize(font, 8);
'content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0);
content.EndText()
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Sub
''' 添加普通偏转角度文字水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="waterMarkName"></param>
''' <param name="permission"></param>
Public Shared Sub setWatermark(inputfilepath As String, outputfilepath As String, waterMarkName As String)
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
Dim total As Integer = pdfReader.NumberOfPages + 1
Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
Dim width As Single = psize.Width
Dim height As Single = psize.Height
Dim content As PdfContentByte
Dim font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim gs As New PdfGState()
Dim i As Integer = 1
While i < total
content = pdfStamper.GetOverContent(i)
'在内容上方加水印
'content = pdfStamper.GetUnderContent(i);//在内容下方加水印
'透明度
gs.FillOpacity = 0.3F
content.SetGState(gs)
'content.SetGrayFill(0.3f);
'开始写入文本
content.BeginText()
content.SetColorFill(BaseColor.LIGHT_GRAY)
content.SetFontAndSize(font, 100)
content.SetTextMatrix(0, 0)
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55)
'content.SetColorFill(BaseColor.BLACK);
'content.SetFontAndSize(font, 8);
'content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0);
content.EndText()
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Sub
添加倾斜水印
''' <summary>
''' 添加倾斜水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="waterMarkName"></param>
''' <param name="userPassWord"></param>
''' <param name="ownerPassWord"></param>
''' <param name="permission"></param>
Public Shared Sub setWatermark(inputfilepath As String, outputfilepath As String, waterMarkName As String, userPassWord As String, ownerPassWord As String, permission As Integer)
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
' 设置密码
'pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);
Dim total As Integer = pdfReader.NumberOfPages + 1
Dim content As PdfContentByte
Dim font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim gs As New PdfGState()
gs.FillOpacity = 0.2F
'透明度
Dim j As Integer = waterMarkName.Length
Dim c As Char
Dim rise As Integer = 0
Dim i As Integer = 1
While i < total
rise = 500
content = pdfStamper.GetOverContent(i)
'在内容上方加水印
'content = pdfStamper.GetUnderContent(i);//在内容下方加水印
content.BeginText()
content.SetColorFill(BaseColor.DARK_GRAY)
content.SetFontAndSize(font, 50)
' 设置水印文字字体倾斜 开始
If j >= 15 Then
content.SetTextMatrix(200, 120)
Dim k As Integer = 0
While k < j
content.SetTextRise(rise)
c = waterMarkName(k)
content.ShowText(c + "")
rise -= 20
System.Math.Max(System.Threading.Interlocked.Increment(k),k - 1)
End While
Else
content.SetTextMatrix(180, 100)
Dim k As Integer = 0
While k < j
content.SetTextRise(rise)
c = waterMarkName(k)
content.ShowText(c + "")
rise -= 18
System.Math.Max(System.Threading.Interlocked.Increment(k),k - 1)
End While
End If
' 字体设置结束
' 画一个圆
'content.Ellipse(250, 450, 350, 550);
'content.SetLineWidth(1f);
'content.Stroke();
content.EndText()
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Sub
''' 添加倾斜水印
''' </summary>
''' <param name="inputfilepath"></param>
''' <param name="outputfilepath"></param>
''' <param name="waterMarkName"></param>
''' <param name="userPassWord"></param>
''' <param name="ownerPassWord"></param>
''' <param name="permission"></param>
Public Shared Sub setWatermark(inputfilepath As String, outputfilepath As String, waterMarkName As String, userPassWord As String, ownerPassWord As String, permission As Integer)
Dim pdfReader As PdfReader Is Nothing
Dim pdfStamper As PdfStamper Is Nothing
Try
pdfReader = New PdfReader(inputfilepath)
pdfStamper = New PdfStamper(pdfReader, New FileStream(outputfilepath, FileMode.Create))
' 设置密码
'pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);
Dim total As Integer = pdfReader.NumberOfPages + 1
Dim content As PdfContentByte
Dim font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
Dim gs As New PdfGState()
gs.FillOpacity = 0.2F
'透明度
Dim j As Integer = waterMarkName.Length
Dim c As Char
Dim rise As Integer = 0
Dim i As Integer = 1
While i < total
rise = 500
content = pdfStamper.GetOverContent(i)
'在内容上方加水印
'content = pdfStamper.GetUnderContent(i);//在内容下方加水印
content.BeginText()
content.SetColorFill(BaseColor.DARK_GRAY)
content.SetFontAndSize(font, 50)
' 设置水印文字字体倾斜 开始
If j >= 15 Then
content.SetTextMatrix(200, 120)
Dim k As Integer = 0
While k < j
content.SetTextRise(rise)
c = waterMarkName(k)
content.ShowText(c + "")
rise -= 20
System.Math.Max(System.Threading.Interlocked.Increment(k),k - 1)
End While
Else
content.SetTextMatrix(180, 100)
Dim k As Integer = 0
While k < j
content.SetTextRise(rise)
c = waterMarkName(k)
content.ShowText(c + "")
rise -= 18
System.Math.Max(System.Threading.Interlocked.Increment(k),k - 1)
End While
End If
' 字体设置结束
' 画一个圆
'content.Ellipse(250, 450, 350, 550);
'content.SetLineWidth(1f);
'content.Stroke();
content.EndText()
System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
End While
Catch ex As Exception
Throw ex
Finally
If pdfStamper IsNot Nothing Then
pdfStamper.Close()
End If
If pdfReader IsNot Nothing Then
pdfReader.Close()
End If
End Try
End Sub
调用方法
Dim path As tring = "D://my.pdf"
HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri, path)
'HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(), path);
PDFSetWaterMark.PDFWatermark(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0)
PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST")
HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri, path)
'HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(), path);
PDFSetWaterMark.PDFWatermark(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0)
PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST")
[本日志由 tiancao1001 于 2018-10-24 10:46 AM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |