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));
}
}
}
}