如何用itext对网站生成的pdf文件每页加水印

如何用itext对网站生成的pdf文件每页加水印,第1张

具体是这样的,我们现在在开发一个项目,其中有个功能是将用户填写的报表在线生成pdf下载,是通过流直接输出到用户的电脑里,服务器上不会存在生成的pdf文件,但是现在有一个问题,因为pdf文件页数是不确定的,所以在每一页加水印现在无法实现,请问怎样弄才能实现这一功能?我之前的思路是这样的,在生成pdf的java代码最后加个循环,通过得到document的页数然后用pdfContentByte在每一页加水印,但是我没找到可以定位到第几页的方法。部分代码:Java coderesponse.setContentType("text/htmlcharset=GBK")

response.setContentType("application/pdf")

response.setHeader("Content-Disposition", "attachmentfilename="

+ fileName + ".pdf")

OutputStream outs = response.getOutputStream()// 获取输出流

PdfWriter writer = PdfWriter.getInstance(doc, outs)

document.open()

PdfContentByte under = writer.getDirectContentUnder()

Java code//添加水印

under.beginText()

under.setColorFill(BaseColor.LIGHT_GRAY)

under.setFontAndSize(bfTitle, 100)

under.setTextMatrix(70, 0)

int rise = 200

for (int k = 0k <waterMarkName.length()k++) {

under.setTextRise(rise)

char c = waterMarkName.charAt(k)

under.showText(c + " ")

rise += 100

}

under.endText()

document.close()//关闭

public class WaterMark

{

static string strPath = @"C:\Users\Administrator\Desktop\Test\TEST\ConsoleApplication1\bin\Debug\pic\"

public static void CreateWaterMark()

{

try

{

//////参数说明

//inputPath 需要增加水印的pdf文件位置

//outputPath 增加水印后,新生成的pdf文件位置

//watermarkimagepath 水印图片的位置

///////////////

string inputPath = @"C:\Users\Administrator\Desktop\Test\TEST\TEMP_PDF\Chap0605.pdf"

string outputPath = @"C:\Users\Administrator\Desktop\Test\TEST\TEMP_PDF\Chap0605_new.pdf"

string watermarkPath = strPath + "logo.png"

PdfReader pdfReader = new PdfReader(inputPath)

int numberOfPages = pdfReader.NumberOfPages

FileStream outputStream = new FileStream(outputPath, FileMode.Create)

PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream)

PdfContentByte waterMarkContent

string watermarkimagepath = watermarkPath

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(watermarkimagepath)

image.GrayFill = 100

image.RotationDegrees = -45

iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1)

float width = psize.Width

float height = psize.Height

float left = width / 2 - image.Width + 80

float top = height / 2 - image.Height

image.SetAbsolutePosition(left, top)

for (int i = 1i <= numberOfPagesi++)

{

//waterMarkContent = pdfStamper.GetUnderContent(i)

waterMarkContent = pdfStamper.GetOverContent(i)

PdfGState gs = new PdfGState()

gs.FillOpacity = 0.3f

waterMarkContent.SetGState(gs)

waterMarkContent.AddImage(image)

}

pdfStamper.Close()

pdfReader.Close()

}

catch (Exception ex)

{

// WriteLog.Log(ex.ToString())

throw ex

}

}

}

首先需要的jar包为iText-2.1.2u.jar、iTextAsian.jar。

import java.awt.Color

import java.io.BufferedOutputStream

import java.io.File

import java.io.FileOutputStream

import java.io.IOException

import java.text.SimpleDateFormat

import java.util.Calendar

import com.lowagie.text.DocumentException

import com.lowagie.text.Element

import com.lowagie.text.pdf.BaseFont

import com.lowagie.text.pdf.PdfContentByte

import com.lowagie.text.pdf.PdfGState

import com.lowagie.text.pdf.PdfReader

import com.lowagie.text.pdf.PdfStamper

public class TestWaterPrint {

public static void main(String[]args) throws DocumentException, IOException{

//要输出的pdf文件

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("E:/abc.pdf")))

Calendar cal = Calendar.getInstance()

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")

//将pdf文件先加水印然后输出

setWatermark(bos,"E:/pdf源文件.pdf",format.format(cal.getTime()) + " 下载使用人:" + "测试user", 16)

}

public static void setWatermark(BufferedOutputStream bos, String input,

String waterMarkName, int permission)

throws DocumentException, IOException {

PdfReader reader = new PdfReader(input)

PdfStamper stamper = new PdfStamper(reader, bos)

int total = reader.getNumberOfPages() + 1

PdfContentByte content

BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",

BaseFont.EMBEDDED)

PdfGState gs = new PdfGState()

for (int i = 1i <totali++) {

content = stamper.getOverContent(i)//在内容上方加水印

//content = stamper.getUnderContent(i)//在内容下方加水印

gs.setFillOpacity(0.2f)

// content.setGState(gs)

content.beginText()

content.setColorFill(Color.LIGHT_GRAY)

content.setFontAndSize(base, 50)

content.setTextMatrix(70, 200)

content.showTextAligned(Element.ALIGN_CENTER, "公司内部文件,请注意保密!", 300,

350, 55)

content.setColorFill(Color.BLACK)

content.setFontAndSize(base, 8)

content.showTextAligned(Element.ALIGN_CENTER, "下载时间:"

+ waterMarkName + "", 300, 10, 0)

content.endText()

}

stamper.close()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11815169.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存