PDF Arabic watermark using MVC and iTextSharp


PDF full page Arabic 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.
Step 2: 

Add font “ARIAL.TTF” in your solution download from download font or search in google.

Step 3:


Open the controller Home from controller folder in MVC application and add the following ActionResult method in the page.

 public ActionResult GetArabicWaterMarkPdf()
{
// Source File Path
string originalFile = Server.MapPath("~/Attachment/Sample.pdf");

// Destination File path
string destinationpath = Server.MapPath("~/Attachment/SamplewithArabicWaterMark.pdf");

// Read file from file location
PdfReader reader = new PdfReader(originalFile);

//define font size and style
// use specific font for arabic text
BaseFont bf = BaseFont.CreateFont(Server.MapPath("~/fonts/ARIAL.TTF"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);

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 = "نموذج"; // define text for
string Layer2warkmarktxt = "خاص وسري";
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 to generate full page
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), f), 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);
cb.SetFontAndSize(bf, 50);
Font fl = new Font(bf, 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), fl), 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 Arabicwatermark pdf", "GetArabicWaterMarkPdf", "Home", null, new { target = "_blank", @class = "btn btn-primary" })
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 “خاص وسري” written in the middle of the page at 45 degree angle.

Download : Source Code

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s