Tuesday, April 11, 2017

Textual description of firstImageUrl

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


                        PdfGState gState = new PdfGState();

                        gState.FillOpacity = 0.1f; // define opacity level

                        // set font size and style for layer water mark text to generate full page
                                 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)
                            currentWaterMarkWidth = cb.GetEffectiveStringWidth(string.Join(" ", watermarkList), false);

                        //Fill the Page with Lines of Watermarks
                        float currentYPos = rect.Height;

                        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;


                        // End First Layer


                        // Start Layer 2

                        // Tell the cb that the next commands should be "bound" to this new layer
                        cb.SetFontAndSize(bf, 50);
                        Font fl = new Font(bf, 50);
                        gState = new PdfGState();
                        gState.FillOpacity = 0.1f;


                        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

                        // End Layer 2


                // Save file to destination location if required
                if (System.IO.File.Exists(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

1 comment:

  1. I simply want to tell you that I’m all new to blogs and truly liked you’re blog site. Very likely I’m likely to bookmark your site .You surely come with remarkable articles. Cheers for sharing your website page.
    Digital Marketing Company in Chennai


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  gen...