PDF full page watermark using MVC and iTextSharp
Download : Source Code
Most
of the time we have requirement to generate pdf with water mark to
secure and tried to protect from forgery. Here, I will explain how to implement
iTextSharp dll in MVC application to create full page watermark in existing pdf.
Step 1:
First
of all create MVC web application and install iTextSharp package through Nuget package
manager or nugget command mentioned below.
More
detail: https://www.nuget.org/packages/itextsharp.xfaworker.
Step 2:
Open
the controller Home from controller folder in MVC application and add the
following ActionResult
method in
the page.
public ActionResult GetWaterMarkPdf() { // Source File Path string originalFile = Server.MapPath("~/Attachment/Sample.pdf"); // Destination File path string destinationpath = Server.MapPath("~/Attachment/SamplewithWaterMark.pdf"); // Read file from file location PdfReader reader = new PdfReader(originalFile); //define font size and style Font font = new Font(FontFamily.HELVETICA, 10); using (MemoryStream memoryStream = new MemoryStream()) { using (var pdfStamper = new PdfStamper(reader, memoryStream, '\0')) { // Getting total number of pages of the Existing Document int pageCount = reader.NumberOfPages; // Create two New Layer for Watermark PdfLayer layer = new PdfLayer("WatermarkLayer", pdfStamper.Writer); PdfLayer layer2 = new PdfLayer("WatermarkLayer2", pdfStamper.Writer); // Loop through each Page string layerwarkmarktxt = "water mark"; // define text for string Layer2warkmarktxt = "Confidential"; for (int i = 1; i <= pageCount; i++) { // Getting the Page Size Rectangle rect = reader.GetPageSize(i); // Get the ContentByte object PdfContentByte cb = pdfStamper.GetUnderContent(i); // Tell the cb that the next commands should be "bound" to this new layer // Start Layer cb.BeginLayer(layer); PdfGState gState = new PdfGState(); gState.FillOpacity = 0.1f; // define opacity level cb.SetGState(gState); // set font size and style for layer water mark text cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10); List<string> watermarkList = new List<string>(); float singleWaterMarkWidth = cb.GetEffectiveStringWidth(layerwarkmarktxt, false); float fontHeight = 10; //Work out the Watermark for a Single Line on the Page based on the Page Width float currentWaterMarkWidth = 0; while (currentWaterMarkWidth + singleWaterMarkWidth < rect.Width) { watermarkList.Add(layerwarkmarktxt); currentWaterMarkWidth = cb.GetEffectiveStringWidth(string.Join(" ", watermarkList), false); } //Fill the Page with Lines of Watermarks float currentYPos = rect.Height; //cb.BeginText(); while (currentYPos > 0) { ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(string.Join(" ", watermarkList), font), rect.Width / 2, currentYPos, 0, PdfWriter.RUN_DIRECTION_RTL, 1); currentYPos -= fontHeight; } cb.EndLayer(); // End First Layer //**************************************************************// // Start Layer 2 // Tell the cb that the next commands should be "bound" to this new layer cb.BeginLayer(layer2); Font f = new Font(FontFamily.HELVETICA, 50); gState = new PdfGState(); gState.FillOpacity = 0.1f; cb.SetGState(gState); cb.SetColorFill(BaseColor.BLACK); //cb.BeginText(); ColumnText.ShowTextAligned(cb, Element.ALIGN_CENTER, new Phrase(string.Join(" ", Layer2warkmarktxt), f), rect.Width / 2, rect.Height / 2, 45f, PdfWriter.RUN_DIRECTION_RTL, 1); // Close the layer cb.EndLayer(); // End Layer 2 } } // Save file to destination location if required if (System.IO.File.Exists(destinationpath)) { System.IO.File.Delete(destinationpath); } System.IO.File.WriteAllBytes(destinationpath, memoryStream.ToArray()); } // send file to browse to open it from destination location. return File(destinationpath, "application/pdf"); }
Now after paste this code we need to call GetWaterMarkPdf() method from view
Open the Index View in Home folder delete all the html text and add one action link like shown as below.
@Html.ActionLink("Download water mark pdf", "GetWaterMarkPdf", "Home", null, new { target = "_blank" })
After adding action link in the view run the web applicaiton and you will see the page looks like below.
Press the download water mark pdf button and then browser will open the watermark pdf file as shown below.
You can see in the background watermark text emboss below the text and image as well as "confidential" written in the middle of the page at 45 degree angle.
No comments:
Post a Comment