In this article I will explain with an example, how to add Watermark to existing
PDF using
iTextSharp in ASP.Net Core (.Net Core) Razor Pages.
Download iTextSharp and XmlWorkerHelper Libraries
You can download the iTextSharp and XmlWorkerHelper libraries from the following links.
PDF File and Watermark image Location
The sample PDF file and Watermark image are located inside the PDF and Images Folder (Directory) of wwwroot Folder (Directory).
Namespaces
You will need to import the following namespace.
using iTextSharp.text.pdf;
Razor PageModel (Code-Behind)
Inside the PageModel, first the private property IWebHostEnvironment interface is created.
Then, the interface is injected into the PageModel (IndexModel) using Dependency Injection method and the injected object is assigned to private property (created earlier).
The PageModel consists of following Handler method.
Handler method for handling GET operation
This Handler method left empty as it is not required.
Handler method for handling POST operation
Inside this Handler method, first the BYTE Array of the sample PDF file (explained earlier) is determined using IWebHostEnvironment interface.
Then, the Watermark image is read using GetInstance method of Image class and its position is set using SetAbsolutePosition method of Image class.
After that, an object of PdfReader class is created which accepts BYTE Array of the sample PDF file as a parameter.
A PdfStamper class object is also created which accepts objects of PdfReader and MemoryStream class as a parameter.
Then, PDF document is created using existing PDF file and a FOR loop is executed over all the pages.
Adding Watermark
Inside the loop, the Watermark image is added to the PDF document using AddImage method of PdfContentByte class.
Finally, the File function is called which accepts the BYTE Array of the PDF document and the name of the file, which initiates the file download operation.
public class IndexModel : PageModel
{
private IWebHostEnvironment Environment { get; }
public IndexModel(IWebHostEnvironment _environment)
{
this.Environment = _environment;
}
public void OnGet()
{
}
public FileResult OnPostExport()
{
byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(this.Environment.WebRootPath,"PDF\\Sample.pdf"));
// Reading watermark image.
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Path.Combine(this.Environment.WebRootPath,"Images\\Watermark.png"));
// Setting watermark image position.
img.SetAbsolutePosition(100, 200);
using (MemoryStream stream = new MemoryStream())
{
// Reading pdf.
PdfReader reader = new PdfReader(bytes);
using (PdfStamper stamper = new PdfStamper(reader, stream))
{
// Loop through all pages.
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfContentByte pdfContentByte = stamper.GetUnderContent(i);
// Adding Image to the pdf.
pdfContentByte.AddImage(img);
}
}
bytes = stream.ToArray();
return File(bytes,"application/pdf","Sample_With_Watermark.pdf");
}
}
}
Razor Page (HTML)
HTML Markup
Inside the Razor Page the ASP.Net TagHelpers is inherited.
The HTML of Razor Page consists of a Form which consists of a Submit Button which when clicked, the Form is submitted.
Note: In the Razor PageModel, the Handler method name is OnPostExport but here it will be specified as Export when calling from the Razor HTML Page.
@page
@modeliTextSharp_Add_Watermark_Core_Razor.Pages.IndexModel
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form method="post">
<input id="btnSubmit" type="submit" value="Export" asp-page-handler="Export" />
</form>
</body>
</html>
Screenshots
PDF before Exporting
Form
PDF after Exporting
Downloads