tab 1
此演示向您展示如何将文本水印或图像水印添加到 pdf 文档。
upload
maximum file size: 1 mb. files accepted: pdf.
click here to browse files.
source file:
filename
fileerrors
set text watermark
text: | |
font: | |
font size: | |
color: | |
downloads
|
set image watermark
image: |
click here to browse files
|
downloads
|
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
import com.spire.pdf.fileformat;
import com.spire.pdf.pdfdocument;
import com.spire.pdf.pdfpagebase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.dimension2d;
import java.awt.geom.point2d;
import java.awt.geom.rectangle2d;
import java.io.bytearrayinputstream;
public class pdfwatermarkdemo {
public void addwatermark(string pdffile, string watermarktype, string text, byte[] imagedata){
string outputfile = "output.pdf";
pdfdocument pdf=new pdfdocument();
pdf.loadfromfile(pdffile);
switch (watermarktype) {
case "text":
addtextwatermark(pdf, text);
break;
case "image":
addimagewatermark(pdf, imagedata);
break;
}
pdf.savetofile(outputfile, fileformat.pdf);
pdf.close();
}
public void addimagewatermark(pdfdocument pdf, byte[] imgdata) {
pdfimage image = pdfimage.fromstream(new bytearrayinputstream(imgdata));
for (int i = 0; i < pdf.getpages().getcount(); i ) {
pdfpagebase page = pdf.getpages().get(i);
page.getcanvas().save();
page.getcanvas().settransparency(0.5f, 0.5f, pdfblendmode.multiply);
page.getcanvas().drawimage(image, new point2d.float(160, 260));
page.getcanvas().restore();
}
}
public void addtextwatermark(pdfdocument pdf, string text) {
for (int i = 0; i < pdf.getpages().getcount(); i ) {
pdfpagebase page = pdf.getpages().get(i);
dimension2d dimension2d = new dimension();
dimension2d.setsize(page.getcanvas().getclientsize().getwidth() / 2, page.getcanvas().getclientsize().getheight() / 3);
pdftilingbrush brush = new pdftilingbrush(dimension2d);
brush.getgraphics().settransparency(0.3f);
brush.getgraphics().save();
brush.getgraphics().translatetransform((float) brush.getsize().getwidth() / 2, (float) brush.getsize().getheight() / 2);
brush.getgraphics().rotatetransform(-45);
brush.getgraphics().drawstring(text, new pdftruetypefont(new font("arial", font.bold, 80), true), pdfbrushes.getviolet(), 0, 0, new pdfstringformat(pdftextalignment.center));
brush.getgraphics().restore();
brush.getgraphics().settransparency(0.5);
rectangle2d lorect = new rectangle2d.float();
lorect.setframe(new point2d.float(0, 0), page.getcanvas().getclientsize());
page.getcanvas().drawrectangle(brush, lorect);
}
}
}
tab 3
using spire.pdf;
using spire.pdf.graphics;
using system.drawing;
using system.io;
namespace demoonlinecode
{
class watermarkdemo
{
public void addwatermark(string pdffile, string watermarktype, string text, byte[] imagedata)
{
string outputfile = "output.pdf";
pdfdocument pdf = new pdfdocument();
pdf.loadfromfile(pdffile);
switch (watermarktype)
{
case "text":
addtextwatermark(pdf, text);
break;
case "image":
addimagewatermark(pdf, imagedata);
break;
}
pdf.savetofile(outputfile, fileformat.pdf);
pdf.close();
}
public void addimagewatermark(pdfdocument pdf, byte[] imgdata)
{
pdfimage image = pdfimage.fromstream(new memorystream(imgdata));
for (int i = 0; i < pdf.pages.count; i )
{
pdfpagebase page = pdf.pages[i];
page.canvas.save();
page.canvas.settransparency(0.5f, 0.5f, pdfblendmode.multiply);
page.canvas.drawimage(image, new system.drawing.pointf(160,260));
page.canvas.restore();
}
}
public void addtextwatermark(pdfdocument pdf, string text)
{
for (int i = 0; i < pdf.pages.count; i )
{
pdfpagebase page = pdf.pages[i];
pdftilingbrush brush = new pdftilingbrush(new sizef(page.canvas.clientsize.width / 2, page.canvas.clientsize.height / 3));
brush.graphics.settransparency(0.3f);
brush.graphics.save();
brush.graphics.translatetransform(brush.size.width / 2, brush.size.height / 2);
brush.graphics.rotatetransform(-45);
brush.graphics.drawstring(text, new pdftruetypefont(new font("arial",80,fontstyle.bold), true), pdfbrushes.violet, 0, 0, new pdfstringformat(pdftextalignment.center));
brush.graphics.restore();
brush.graphics.settransparency(0.5f);
page.canvas.drawrectangle(brush, new rectanglef(new pointf(0, 0), page.canvas.clientsize));
}
}
}
}