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
    Aug 2009
    Posts
    2

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

    Thank you. I am in the process of building 10 more nice applications that can be of use to lot of people.

  2. #2
    New Member
    Join Date
    Sep 2009
    Posts
    8

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

    This is excellent code and this is what i needed. BTW, I have question if this is possible, can the watermark appear behind page?

    Thanks

  3. #3

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

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

    Quote Originally Posted by BigJRofC View Post
    This is excellent code and this is what i needed. BTW, I have question if this is possible, can the watermark appear behind page?

    Thanks
    Yes, in the code I posted, the water mark is written to the over content of the page... This will ensure that the water mark will always be visible (even when the page contains an image, for example). If your page contains mostly text, it is recommended to put the water mark on the under content of the page instead. All you have to do is changing this line:
    Code:
    underContent = stamper.GetOverContent(i)
    To this
    Code:
    underContent = stamper.GetUnderContent(i)
    The rest of the code remains unchanged.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    New Member
    Join Date
    Sep 2009
    Posts
    8

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

    Thanks, Stanav. This is great...

  5. #5
    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