Results 1 to 34 of 34

Thread: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Hybrid View

  1. #1
    New Member
    Join Date
    Oct 2009
    Posts
    2

    Cool Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

    For those of us who find this solution via a web search and want a C# (C-Sharp) version, here it is, tested and working.

    Foremost: Major thx to stanav; excellent post.

    You'll note there are 2 functions, with the first functions calling the second function. This mimics the "OPTIONAL" params in VB

    C-Sharp Code:
    1. public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText)
    2.   {
    3.     iTextSharp.text.pdf.BaseFont tWatermarkFont = null;
    4.     float tWatermarkFontSize = 48F;
    5.     iTextSharp.text.Color tWatermarkFontColor = null;
    6.     float tWatermarkFontOpacity = 0.3F;
    7.     float tWatermarkRotation = 45.0F;
    8.  
    9.     tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
    10.     tWatermarkFontColor = iTextSharp.text.Color.BLUE;
    11.     AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);
    12.   }//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText )
    13.  
    14.  
    15.   public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)
    16.   {
    17.  
    18.     iTextSharp.text.pdf.PdfReader reader = null;
    19.     iTextSharp.text.pdf.PdfStamper stamper = null;
    20.     iTextSharp.text.pdf.PdfGState gstate = null;
    21.     iTextSharp.text.pdf.PdfContentByte underContent = null;
    22.     iTextSharp.text.Rectangle rect = null;
    23.     float currentY = 0.0F;
    24.     float offset = 0.0F;
    25.     int pageCount = 0;
    26.  
    27.     try
    28.     {
    29.       reader = new iTextSharp.text.pdf.PdfReader(sourceFile);
    30.       rect = reader.GetPageSizeWithRotation(1);
    31.       stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.Create));
    32.       if (watermarkFont == null)
    33.       {
    34.         watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
    35.       }//if (watermarkFont == null)
    36.  
    37.       if (watermarkFontColor == null)
    38.       {
    39.         watermarkFontColor = iTextSharp.text.Color.BLUE;
    40.       }//if (watermarkFontColor == null)
    41.  
    42.       gstate = new iTextSharp.text.pdf.PdfGState();
    43.       gstate.FillOpacity = watermarkFontOpacity;
    44.       gstate.StrokeOpacity = watermarkFontOpacity;
    45.       pageCount = reader.NumberOfPages;
    46.       for (int i = 1; i <= pageCount; i++)
    47.       {
    48.         underContent = stamper.GetOverContent(i);
    49.         underContent.SaveState();
    50.         underContent.SetGState(gstate);
    51.         underContent.SetColorFill(watermarkFontColor);
    52.         underContent.BeginText();
    53.         underContent.SetFontAndSize(watermarkFont, watermarkFontSize);
    54.         underContent.SetTextMatrix(30, 30);
    55.         if (watermarkText.Length > 1)
    56.         {
    57.           currentY = (rect.Height / 2) + ((watermarkFontSize * watermarkText.Length) / 2);
    58.         }//if (watermarkText.Length > 1)
    59.         else
    60.         {
    61.           currentY = (rect.Height / 2);
    62.         }//else if (watermarkText.Length > 1)
    63.  
    64.         for (int j = 0; j < watermarkText.Length; j++)
    65.         {
    66.           if (j > 0)
    67.           {
    68.             offset = (j * watermarkFontSize) + (watermarkFontSize / 4) * j;
    69.           }//if (j > 0)
    70.           else
    71.           {
    72.             offset = 0.0F;
    73.           }//else if (j > 0)
    74.  
    75.           underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText[j], rect.Width / 2, currentY - offset, watermarkRotation);
    76.         }//for (int j = 0; j < watermarkText.Length; j++)
    77.  
    78.         underContent.EndText();
    79.         underContent.RestoreState();
    80.       }//for (int i = 1; i <= pageCount; i++)
    81.  
    82.       stamper.Close();
    83.       reader.Close();
    84.     }
    85.     catch (Exception ex)
    86.     {
    87.       throw ex;
    88.     }
    89.   }//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)

    Signed,

    MacSpudster / GNoter
    ==
    ASPX = Apple Simply Performs eXcellently
    Last edited by gnoter; Oct 8th, 2009 at 05:30 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width