Foxtable(狐表)用户栏目专家坐堂 → [求助]如何给PDF文件添加水印


  共有2740人关注过本帖树形打印复制链接

主题:[求助]如何给PDF文件添加水印

帅哥哟,离线,有人找我吗?
有点甜
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/22 0:07:00 [显示全部帖子]

mark pdf水印

 

Dim waterMarkName As String = "aaaaaabbbbcccc"
Dim reader As New iTextSharp.text.pdf.PdfReader("d:\test.pdf")

Dim msWater As new io.FileStream("d:\123.pdf", io.FileMode.Create)


Dim total  As Integer = reader.NumberOfPages
Dim pdfStamper As new iTextSharp.text.pdf.PdfStamper(Reader, msWater)

Dim psize = Reader.GetPageSize(1)
Dim width = psize.Width
Dim height = psize.Height
Dim gs As new iTextSharp.text.pdf.PdfGState
Dim basefont  As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMYOU.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
For i As Integer = 1 To total
   
    Dim waterContent = pdfStamper.GetOverContent(i)
    waterContent.SetGState(gs)
    waterContent.BeginText()
    waterContent.SetColorFill( iTextSharp.text.BaseColor.RED)
    waterContent.SetFontAndSize(basefont, 18)
    waterContent.SetTextMatrix(0, 0)
    waterContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, waterMarkName, width / 2, height / 2, 55)
    waterContent.EndText()
Next

pdfStamper.close
reader.Close()


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/22 0:12:00 [显示全部帖子]

2楼的代码,你要先引用dll

 

下载信息  [文件大小:1.3 MB  下载次数:5]
图片点击可在新窗口打开查看点击浏览该文件:itextsharp.zip


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2018/6/22 10:11:00 [显示全部帖子]

参考代码

 

 

Dim waterMarkName As String = "aaaaaabbbbcccc" & chr(13) & "123456" & chr(13) & "11111"
Dim reader As New iTextSharp.text.pdf.PdfReader("d:\test.pdf")

Dim msWater As new io.FileStream("d:\123.pdf", io.FileMode.Create)

Dim total  As Integer = reader.NumberOfPages
Dim pdfStamper As new iTextSharp.text.pdf.PdfStamper(Reader, msWater)

Dim psize = Reader.GetPageSize(1)
Dim width = psize.Width
Dim height = psize.Height
Dim gs As new iTextSharp.text.pdf.PdfGState
Dim basefont  As iTextSharp.text.pdf.BaseFont = iTextSharp.text.pdf.BaseFont.CreateFont("C:\WINDOWS\Fonts\SIMYOU.TTF", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
For i As Integer = 1 To total
    Dim waterContent = pdfStamper.GetOverContent(i) 'GetUnderContent(i)
    waterContent.SetGState(gs)
    waterContent.BeginText()
    waterContent.SetColorFill( iTextSharp.text.BaseColor.RED)
    waterContent.SetFontAndSize(basefont, 18)
    waterContent.SetTextMatrix(0, 0)
    Dim ary = waterMarkName.split(chr(13))
    For j As Integer = 0 To ary.length-1
        Dim s As String = ary(j)
        s = s.PadRight(20)
        waterContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_left, s, (width-50)/2+j*20, height / 2-30*j, 45)
    Next
    waterContent.EndText()
Next

pdfStamper.close
reader.Close()


 回到顶部