VB.net给PDF文件通过iTextSharp添加水印

目录

一、选择文件

二、所选文件处理以及添加水印后的文件存放位置

 三、添加水印


以下使用VB.net代码的实例

一、选择文件

        '选择文件
        Dim ofd As New OpenFileDialog
        '设置可以选中的文件格式
        ofd.Filter = "PDF文件 (*.pdf)|*.pdf"
        '打开多选
        ofd.Multiselect = True
        '通过数组存放选择的文件
        Dim FilePaths As String()
        If ofd.ShowDialog = DialogResult.OK Then
            FilePaths = ofd.FileNames
        Else
            '退出此过程
            Exit Sub
        End If
        '加水印,并将list集合转换为数组
        Dim arrayPaths As String() = Class_BidDatabase.Add_PDF_Watermark(FilePaths).ToArray()

二、所选文件处理以及添加水印后的文件存放位置

    ''' <summary>
    ''' 添加水印
    ''' </summary>
    ''' <param name="FilePaths">需添加水印的文件</param>
    ''' <returns></returns>
    Public Shared Function Add_PDF_Watermark1(FilePaths As String()) As List(Of String)
        Dim folderName As String = "TemporaryPDF"
        '将两个字符串组合成一个路径
        Dim tempFolderPath As String = Path.Combine("C:\temp\", folderName)
        ' 检查临时文件夹是否已存在
        If Not Directory.Exists(tempFolderPath) Then
            ' 创建临时文件夹
            Directory.CreateDirectory(tempFolderPath)
            Console.WriteLine("临时文件夹已成功创建!")
        Else
            Console.WriteLine("临时文件夹已存在!")
        End If
        '加水印
        Dim Paths As New List(Of String)
        For i = 0 To FilePaths.Length - 1
            Dim OpenFile As String = FilePaths(i)
            '截取文件名称
            Dim h_str As String = OpenFile.Split("\").Last
            '加水印后的存放位置
            Dim OpenNewFile As String = tempFolderPath + "\" + h_str
            ''普通水印
            Dim result As Boolean = ITextSharpHelper.setWatermark(OpenFile, OpenNewFile, "*****水印*****")
            If result Then
                Paths.Add(OpenNewFile)
            End If
        Next
        Return Paths
    End Function

例子中是将文件放在“C:\temp\”内;也可通过获取桌面路径替换“C:\temp\”

Dim desktopPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

 三、添加水印

    ''' <summary>
    ''' 添加普通偏转角度文字水印
    ''' </summary>
    ''' <param name="OpenFile">文件路径</param>
    ''' <param name="OpenNewFile">添加水印后另存的路径</param>
    ''' <param name="waterMarkName1">水印文字</param>
    ''' <returns></returns>
    Shared Function setWatermark(OpenFile As String, OpenNewFile As String, waterMarkName1 As String) As Boolean
        Dim isSuccess As Boolean = False
        Dim pdfReader As PdfReader = Nothing
        Dim pdfStamper As PdfStamper = Nothing
        Try
            pdfReader = New PdfReader(OpenFile)
            pdfStamper = New PdfStamper(pdfReader, New FileStream(OpenNewFile, FileMode.Create))
            Dim total As Integer = pdfReader.NumberOfPages + 1
            Dim psize As iTextSharp.text.Rectangle = pdfReader.GetPageSize(1)
            Dim width As Double = psize.Width
            Dim height As Double = psize.Height
            Dim content As PdfContentByte
            '字体
            Dim Font As BaseFont = BaseFont.CreateFont("C:\WINDOWS\Fonts\STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
            Dim gs As PdfGState = New PdfGState
            For i = 1 To total - 1
                content = pdfStamper.GetOverContent(i) '在内容上方加水印
                'content = pdfStamper.GetUnderContent(i)'在内容下方加水印
                '透明度
                gs.FillOpacity = 0.6F
                content.SetGState(gs)
                '开始写入文本
                content.BeginText()
                content.SetColorFill(BaseColor.LIGHT_GRAY)
                content.SetFontAndSize(Font, 35)
                content.SetTextMatrix(0, 0)
                '水印位置,可通过偏移度自行调节
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4, height / 4, 55)
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4, height / 2, 55)
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4, height / 4 * 3, 55)
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4 * 3, height / 4, 55)
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4 * 3, height / 2, 55)
                content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName1, width / 4 * 3, height / 4 * 3, 55)
                content.EndText()
            Next
            isSuccess = True
            Return isSuccess
        Catch ex As Exception
            Throw ex
            Return isSuccess
        Finally
            If pdfStamper IsNot Nothing Then
                pdfStamper.Close()
            End If

            If pdfReader IsNot Nothing Then
                pdfReader.Close()
            End If
        End Try
    End Function

添加NuGet包和引用:

Imports System.IO
Imports iTextSharp.text.pdf
Imports iTextSharp.text

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值