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: | The font size must be a valid number. The font size must be a valid number. |
Color: | The format of color value must be rrggbb. |
downloads
|
Set image watermark
Image: |
Click here to browse files
CustomValidator |
downloads
|
- Demo
- Java
- C# source
This demo shows you how to add text watermark or image watermark to a PDF document.
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); } } }
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)); } } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.