For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Thursday, August 14, 2014

Watermark and heading in existing PDF Using itextSharp C#

Watermark and heading in existing PDF Using itextSharp C#

In recently my project of Learning Management System,ICT training programmes,academic software modules and teaching application software/technologies for researchers in the institutions of higher education
in Dot Net, I have faced a problem to Add Watermark and heading in existing PDF Using itextSharp C# at the same time. I have solved this problem. So If you are looking for the solution you can use my code.


  string sourceFile="c://aa.pdf", string outputFile="c://bb.pdf", string watermarkText="Heading Water Mark";
            iTextSharp.text.pdf.PdfReader reader = null;
            iTextSharp.text.pdf.PdfStamper stamper = null;
            iTextSharp.text.pdf.PdfGState gstate = null;
            iTextSharp.text.pdf.PdfContentByte contentByte = null;
            iTextSharp.text.Rectangle rect = null;
            int pageCount = 0;
            BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
            iTextSharp.text.Font black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.UNDEFINED, 30f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.GRAY);
            float fontSize = 50, xPosition = 300, yPosition = 400, angle = 55;
            try
            {
                reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
                rect = reader.GetPageSizeWithRotation(1);
                stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.Create));
                gstate = new iTextSharp.text.pdf.PdfGState();
                pageCount = reader.NumberOfPages;
                for (int i = 1; i <= pageCount; i++)
                {
                    contentByte = stamper.GetOverContent(i);
                    contentByte.SaveState();
                    contentByte.SetGState(gstate);
                    if (i == 1)
                    {
                        PdfPTable table = new PdfPTable(1);
                        float[] widths = new float[] { 500f };
                        table.SetWidths(widths);
                        table.TotalWidth = 600f;
                        table.LockedWidth = true;
                        Chunk chk_subject = new Chunk("My Heading Text", black);
                        Phrase ph_subject = new Phrase();
                        ph_subject.Add(chk_subject);
                        PdfPCell cell = new PdfPCell(ph_subject);
                        cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                        cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
                        cell.BorderWidthLeft = 1f;
                        cell.BorderWidthRight = 1f;
                        cell.BorderWidthTop = 1f;
                        cell.BorderWidthBottom = 1f;
                        cell.BorderColor = BaseColor.WHITE;
                        table.AddCell(cell);
                        table.WriteSelectedRows(0, -1, 0, 690, stamper.GetOverContent(1));
                    }
                    gstate.FillOpacity = (float)0.3;
                    gstate.StrokeOpacity = (float)0.3;
                    contentByte.SetGState(gstate);
                    contentByte.BeginText();
                    contentByte.SetColorFill(BaseColor.LIGHT_GRAY);
                    contentByte.SetFontAndSize(baseFont, fontSize);
                    contentByte.SetTextMatrix(30, 30);
                    contentByte.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
                    contentByte.EndText();
                }
                stamper.Close();
                reader.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            MessageBox.Show("Done");

Share:

0 comments:

Post a Comment

Multiple attribute passing in querySelectorAll

Multiple attribute passing in querySelectorAll     Here I am demonstrating code to how to pass multiple attributes in querySelectorAll. <...

Ads Inside Post

Powered by Blogger.

Arsip