导航:首页 > 文档加密 > javapdf加水印

javapdf加水印

发布时间:2024-09-25 17:50:36

Ⅰ 怎么用itext对网站生成的pdf文件每页加水印

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

Ⅱ 虚心求教,向PDF文件里面加水印问题,做过的请进来看看

import com.lowagie.text.pdf.PdfStamper;

iTextSharp的确好用,但我加水印一般在初始化的时候写两行就行了,不需要这么麻烦,只是图片得自己做淡一些
if(System.IO.File.Exists("./waterMark/waterMark.JPG") == false)
MessageBox.Show("waterMark加载失败,请确定文件waterMark.JPG放在waterMark文件夹下!");
else
{
Watermark watermark = new Watermark(iTextSharp.text.Image.getInstance("./waterMark/waterMark.JPG"), wm_left, wm_top);
document.Add(watermark);
}

wm_left, wm_top是水印的位置,从第几页开始显示水印就把上面的代码加到第几页

补充:watermark是iTextSharp里的,不需要其他引用
iTextSharp.text.watermark 检查是否输入正确

.\itextsharp-0.04-src\iTextSharp\text\watermark.cs 。。。。。。
或者你可以搜索一下?
代码全文:
using System;

/*
* $Id: Watermark.cs,v 1.3 2003/05/15 01:49:58 geraldhenson Exp $
* $Name: $
*
* Copyright 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License"); you may not use this file except in compliance with the License.
* You may obtain a of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation;
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/

namespace iTextSharp.text {
/// <summary>
/// A <CODE>Watermark</CODE> is a graphic element (GIF or JPEG)
/// that is shown on a certain position on each page.
/// </summary>
/// <seealso cref="T:iTextSharp.text.Element"/>
/// <seealso cref="T:iTextSharp.text.Jpef"/>
/// <seealso cref="T:iTextSharp.text.Gif"/>
/// <seealso cref="T:iTextSharp.text.Png"/>
public class Watermark : Image, IElement {

// membervariables

/// <summary> This is the offset in x-direction of the Watermark. </summary>
private float offsetX = 0;

/// <summary> This is the offset in y-direction of the Watermark. </summary>
private float offsetY = 0;

// Constructors

/// <summary>
/// Constructs a <CODE>Watermark</CODE>-object, using an <CODE>Image</CODE>.
/// </summary>
/// <param name="image">an <CODE>Image</CODE>-object</param>
/// <param name="offsetX">the offset in x-direction</param>
/// <param name="offsetY">the offset in y-direction</param>
public Watermark(Image image, float offsetX, float offsetY) : base(image) {
this.offsetX = offsetX;
this.offsetY = offsetY;
}

// implementation of the Element interface

/// <summary>
/// Gets the type of the text element.
/// </summary>
/// <value>a type</value>
public override int Type {
get {
return type;
}
}

// methods to retrieve information

/// <summary>
/// Returns the offset in x direction.
/// </summary>
/// <value>a value</value>
public float OffsetX {
get {
return offsetX;
}
}

/// <summary>
/// Returns the offset in y direction.
/// </summary>
/// <value>an offset</value>
public float OffsetY {
get {
return offsetY;
}
}
}
}

恩,给你发过去了,带有word教程和示例代码,国庆快乐

Ⅲ java的html转pdf水印没有了

在使用Java将HTML转换为PDF时发现水印消失了,可能有几种原因:
1、HTML页面本身就没有水印:你需要检查HTML页面源代码,确认其中是否包含水印的元素。
2、PDF生成库不支持水印:你使用的Java库可能不支持在转换HTML到PDF时添加水印。在这种情况下,你可能需要尝试使用其他支持水印的库,例如iText或Apache PDFBox。
3、水印被PDF的页眉或页脚覆盖:在一些情况下,如果水印被放置在页眉或页脚上,它可能会被其他内容覆盖。你需要检查生成的PDF,并确保水印位于页面的正确位置。
4、水印被PDF背景覆盖:在某些情况下,如果水印被设置为PDF的背景,它可能会被其他内容覆盖。你需要确保水印位于PDF的正确位置。

Ⅳ java 如何给pdf文件加水印

Java生成PDF 加密 水印

1、iText简介

iText是一个开放源码的Java类库,可以用来方便地生成PDF文件。大家通过访问http://sourceforge.net/project/showfiles.php?group_id=15255&release_id=167948
下载最新版本的类库,下载完成之后会得到一个.jar包,把这个包加入JDK的classpath即可使用。

如果生成的PDF文件中需要出现中文、日文、韩文字符,则还需要通过访问http://itext.sourceforge.net/downloads/iTextAsian.jar
下载iTextAsian.jar包。

关于iText类库的使用,http://www.lowagie.com/iText/tutorial/index.html
有比较详细的教程。该教程从入门开始,比较系统地介绍了在PDF文件中放入文字、图片、表格等的方法和技巧。

读完这片教程,大致就可以做一些从简单到复杂的PDF文件了。不过,试图通过教程解决在生成PDF文件过程中遇到的所有困难无疑是一种奢望。所以,阅读iText的api文档显得非常重要。读者在下载类库的同时,也可以下载类库的文档。

注:如果以上两个下载链接无法下载而且通过网络也找不到这个jar包的同志可以留下邮箱地址,我会在两个工作日之内发邮件过去。

以下部分我是我调试通过的源代码,提供大家参考:
import java.awt.*;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.Font;
import
com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;

/**
* 最近的项目中使用Itext将txt文件转换为PDF文件, 并且实现对文件的一些权限控制。
现实对pdf文件加
*密,添加水印等。
*/
public class PDFConvertBL
{
//
txt原始文件的路径
private static final String txtFilePath = "d:/11.txt";

// 生成的pdf文件路径
private static final String pdfFilePath =
"d:/22.pdf";
// 添加水印图片路径
// private static final String
imageFilePath = "D:/33.jpg";
// 生成临时文件前缀
private static final
String prefix = "tempFile";
// 所有者密码
private static final String
OWNERPASSWORD = "12345678";

/**
* txt文件转换为pdf文件
*
* @param txtFile
txt文件路径
* @param pdfFile pdf文件路径
* @param userPassWord
用户密码
* @param waterMarkName 水印内容
* @param permission
操作权限
*/
public static void generatePDFWithTxt(String txtFile,
String pdfFile, String userPassWord, String
waterMarkName,
int permission)
{

try
{
// 生成临时文件
File file =
File.createTempFile(prefix, ".pdf");
//
创建pdf文件到临时文件
if (createPDFFile(txtFile, file))

{
// 增加水印和加密
waterMark(file.getPath(),
pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission);

}
}
catch (Exception e)
{

e.printStackTrace();
}

}

/**
* 创建PDF文档
*
* @param txtFilePath
txt文件路径(源文件)
* @param pdfFilePath pdf文件路径(新文件)
*/
private
static boolean createPDFFile(String txtFilePath, File file)
{

// 设置纸张
Rectangle rect = new Rectangle(PageSize.A4);
//
设置页码
HeaderFooter footer = new HeaderFooter(new Phrase("页码:",
setChineseFont()), true);

footer.setBorder(Rectangle.NO_BORDER);
// step1
Document
doc = new Document(rect, 50, 50, 50, 50);

doc.setFooter(footer);
try
{
FileReader
fileRead = new FileReader(txtFilePath);
BufferedReader read = new
BufferedReader(fileRead);
// 设置pdf文件生成路径 step2

PdfWriter.getInstance(doc, new FileOutputStream(file));
//
打开pdf文件 step3
doc.open();
// 实例化Paragraph
获取写入pdf文件的内容,调用支持中文的方法. step4
while (read.ready())

{
// 添加内容到pdf(这里将会按照txt文件的原始样式输出)

doc.add(new Paragraph(read.readLine(), setChineseFont()));

}
// 关闭pdf文件 step5
doc.close();

return true;
}
catch (Exception e)

{
e.printStackTrace();
return false;

}
}

/**
* 在pdf文件中添加水印
*
* @param inputFile
原始文件
* @param outputFile 水印输出文件
* @param waterMarkName
水印名字
*/
private static void waterMark(String inputFile, String
outputFile, String userPassWord, String ownerPassWord,

String waterMarkName, int permission)
{
try

{
PdfReader reader = new PdfReader(inputFile);

PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(outputFile));
// 设置密码

stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(),
permission, false);
BaseFont base =
BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() +
1;
// Image image =
Image.getInstance(imageFilePath);
//
image.setAbsolutePosition(200, 400);
PdfContentByte
under;
int j = waterMarkName.length();
char c =
0;
int rise = 0;
for (int i = 1; i < total;
i++)
{
rise = 500;
under =
stamper.getUnderContent(i);
// 添加图片
//
under.addImage(image);
under.beginText();

under.setColorFill(Color.CYAN);
under.setFontAndSize(base,
30);
// 设置水印文字字体倾斜 开始
if (j >=
15)
{
under.setTextMatrix(200,
120);
for (int k = 0; k < j;
k++)
{

under.setTextRise(rise);
c =
waterMarkName.charAt(k);
under.showText(c +
"");
rise -= 20;

}
}
else

{
under.setTextMatrix(180, 100);

for (int k = 0; k < j; k++)

{
under.setTextRise(rise);

c = waterMarkName.charAt(k);
under.showText(c +
"");
rise -= 18;

}
}
// 字体设置结束

under.endText();
// 画一个圆
//
under.ellipse(250, 450, 350, 550);
//
under.setLineWidth(1f);
// under.stroke();

}
stamper.close();
}
catch (Exception
e)
{
e.printStackTrace();
}
}

/**
* 设置中文
*
* @return Font
*/

private static Font setChineseFont()
{
BaseFont base =
null;
Font fontChinese = null;
try

{
base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
fontChinese = new Font(base, 12,
Font.NORMAL);
}
catch (DocumentException e)

{
e.printStackTrace();
}
catch (IOException
e)
{
e.printStackTrace();
}

return fontChinese;
}

public static void main(String[] args)
{

generatePDFWithTxt(txtFilePath, pdfFilePath, "123", "水印文字", 16);

}
}

文章参考一些网络博客稍加修改调试,特此申明
http://hi..com/sx_python/item/15081531ad7d1bc21b96965e

Ⅳ java中打印的pdf怎么添加图片水印

添加水印可以参考使用控件来添加的方法,如下:

1. 添加单个图片水印效果:

import com.spire.pdf.*;

import java.awt.geom.Rectangle2D;

public class watermark {

public static void main(String[] args) {

//加载PDF文档

PdfDocument doc = new PdfDocument();

doc.loadFromFile("C:\Users\Administrator\Desktop\Sample.pdf");

//获取第一页

PdfPageBase page = doc.getPages().get(0);

//设置背景图片

page.setBackgroundImage("C:\Users\Administrator\Desktop\logo.png");

//设置背景区域

Rectangle2D.Float rect = new Rectangle2D.Float();

rect.setRect(280, 300, 150, 150);

page.setBackgroundRegion(rect);

//保存文档

doc.saveToFile("output/imageWaterMark.pdf");

doc.close();

}

}

2. 添加平铺图片水印效果

import com.spire.pdf.*;

import com.spire.pdf.graphics.PdfImage;

import com.spire.pdf.graphics.PdfTilingBrush;

import java.awt.*;

import java.awt.geom.Dimension2D;

import java.awt.geom.Rectangle2D;

public class AddBackground {

public static void main(String[] args) {

//创建PdfDocument对象,并加载PDF测试文档

PdfDocument pdf = new PdfDocument();

pdf.loadFromFile("test.pdf");

//遍历文档每一页,加载图片,并设置成平铺背景(水印)

for (int i = 0; i < pdf.getPages().getCount();i++)

{

PdfPageBase page = pdf.getPages().get(i);

Dimension2D dimension2D = new Dimension();

dimension2D.setSize(page.getCanvas().getSize().getWidth()/4, page.getCanvas().getSize().getHeight()/3);

PdfTilingBrush brush = new PdfTilingBrush(dimension2D);

brush.getGraphics().setTransparency(0.2f);

brush.getGraphics().translateTransform(brush.getSize().getWidth()/10,brush.getSize().getHeight()/10);

brush.getGraphics().rotateTransform(30);

PdfImage image = PdfImage.fromImage("logo.png");

brush.getGraphics().drawImage(image,brush.getSize().getWidth()-image.getWidth()/2,(brush.getSize().getHeight())/2);


Rectangle2D rectangle2D = new Rectangle2D.Float();

rectangle2D.setFrame(new Point(0,0),page.getCanvas().getClientSize());

page.getCanvas().drawRectangle(brush,rectangle2D);

}


//保存文档

pdf.saveToFile("SetTiledBackground.pdf");

pdf.dispose();

}

}

注:这里使用的是free Spire.Pdf.jar(以上代码参考自文章1、文章2)

Ⅵ java给tif格式图片加文字水印

packagecom.coderli.image;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;

importjavax.imageio.ImageIO;

importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("restriction")
publicfinalclassImageUtils{
publicImageUtils(){

}


/**
*打印文字水印图片
*
*@parampressText
*--文字
*@paramtargetImg--
*目标图片
*@paramfontName--
*字体名
*@paramfontStyle--
*字体样式
*@paramcolor--
*字体颜色
*@paramfontSize--
*字体大小
*@paramx--
*偏移量
*@paramy
*/

publicstaticvoidpressText(StringpressText,StringtargetImg,
StringfontName,intfontStyle,Colorcolor,intfontSize,intx,
inty){
try{
File_file=newFile(targetImg);
Imagesrc=ImageIO.read(_file);
intwidth=src.getWidth(null);
intheight=src.getHeight(null);
BufferedImageimage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsg=image.createGraphics();
g.drawImage(src,0,0,width,height,null);
g.setColor(color);
g.setFont(newFont(fontName,fontStyle,fontSize));

g.drawString(pressText,width-fontSize-x,height-fontSize
/2-y);
g.dispose();
FileOutputStreamout=newFileOutputStream(targetImg);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}catch(Exceptione){
System.out.println(e);
}
}

publicstaticvoidmain(String[]args){
pressText("bbs.coderli.com","f:/1.tiff","TimesNewRomas",Font.PLAIN,Color.BLUE,22,150,20);
}
}

这个方法里用的api是支持tiff格式的,你可以试试。

阅读全文

与javapdf加水印相关的资料

热点内容
编译出来的pac包是干嘛的 浏览:317
如何拍照搜索app 浏览:12
应用程序如何访问串口服务器 浏览:428
如何查看服务器的出口ip地址 浏览:437
母根服务器什么时候可以对接成功 浏览:288
安卓如何隐藏手机里的游戏 浏览:518
单片机pwm电机 浏览:985
华为如何能不再下载app 浏览:458
下载压缩包后无法解压 浏览:844
去色算法 浏览:931
云服务器win镜像哪里下载地址 浏览:37
python接口自动化全局变量反射 浏览:65
文件夹怎么开文档 浏览:912
车子解压能不能不需要本人 浏览:293
便携式linux 浏览:105
visualstudio编译脚本 浏览:404
vsual服务器是什么 浏览:448
程序员的入门术语 浏览:417
groovylinux 浏览:524
emp芯片编程软件 浏览:740