Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active September 12, 2019 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/c1c79fe703c632a8082961d28360c6d1 to your computer and use it in GitHub Desktop.
Save GroupDocsGists/c1c79fe703c632a8082961d28360c6d1 to your computer and use it in GitHub Desktop.
Batch Watermarking
// Get files from the Documents folder
DirectoryInfo dir = new DirectoryInfo(@"../../Documents/");
FileInfo[] files = dir.GetFiles();
// Iterate through the files
foreach (FileInfo file in files)
{
// Load document
using (Document doc = Document.Load(file.FullName))
{
// Initialize the font to be used for watermark
Font font = new Font("Calibre", 50, FontStyle.Bold | FontStyle.Italic);
// Create watermark
TextWatermark watermark = new TextWatermark("Protected", font);
// Set watermark properties
watermark.ForegroundColor = Color.Red;
watermark.Opacity = 0.5;
watermark.TextAlignment = TextAlignment.Right;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.RotateAngle = -45;
// Apply watermark
doc.AddWatermark(watermark);
// Save document
doc.Save(Path.Combine("../../Output",file.Name));
}
}
// Get files from the Documents folder
File folder = new File("./Documents/");
File[] files = folder.listFiles();
// Iterate through the files
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
Document doc = Document.load(files[i].getPath());
// Create watermark
Font font = new Font("Calibre", 50, FontStyle.Bold | FontStyle.Italic);
TextWatermark watermark = new TextWatermark("Protected", font);
// Set watermark properties
watermark.setForegroundColor(Color.getRed());
watermark.setTextAlignment(TextAlignment.Right);
watermark.setOpacity(0.5);
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
watermark.setRotateAngle(-45);
// Apply watermark to the document
doc.addWatermark(watermark);
// Save document
doc.save("./Output/" + files[i].getName());
doc.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment