You can add source-based watermarks and text watermarks to a PDF. Source-based means the source of the watermark is another PDF.
To add a source-based watermark to a PDF, do the following:
- Create a PDFXpress object and initialize it.
- Create a Document object from an existing PDF file.
- Create a Document object from an existing PDF file to be used as the watermark source.
- Create a PageArtifactOptions object.
- Set the PageArtifactOptions:
- ArtifactType - specifies the type of watermark, in this case SourceWatermark.
- Opacity – we set this to .5, to make the watermark translucent
- PageList – we set this to specify to what pages the watermark will be added
- SourceDocument – we set this to our source PDF watermark
- SourcePageNumber – we set this to 0, to specify the first page
- Call AddWatermark() on the Document object to add the watermark.
- Create a SaveOptions object.
- Call Save() on the Document object to save the watermarked pdf.
C# Example | Copy Code |
---|---|
using System; using Accusoft.PdfXpressSdk; namespace PDFWatermark { class Watermark { static void Main(string[] args) { using (PdfXpress pdf = new PdfXpress()) { pdf.Initialize(@"C:\Users\Public\Documents\Accusoft\PDFXpress\v5.0\Support\Font", @"C:\Users\Public\Documents\Accusoft\PDFXpress\v5.0\Support\CMap"); using (Document doc = new Document(pdf, "C:\\myfile.pdf")) { using (Document sourceDoc = new Document(pdf, "C:\\source.pdf")) { //create the page artifact options PageArtifactOptions po = new PageArtifactOptions(); po.ArtifactType = TypeOfArtifact.SourceWatermark; po.Opacity = .5; PageRange pageRange = new PageRange(); pageRange.PageCount = 1; pageRange.StartPageNumber = 0; PageList pageList = new PageList(); pageList.Add(pageRange); po.PageList = pageList; po.SourceDocument = sourceDoc; po.SourcePageNumber = 0; //add the watermark doc.AddWatermark(po); //set the save options SaveOptions so = new SaveOptions(); so.Filename = "C:\\watermarked.pdf"; so.Overwrite = true; so.Linearized = true; //save the document doc.Save(so); } } } } } } |