Advertisement

05.14.2008 at 12:39PM PDT, ID: 23402862
[x]
Attachment Details

How do I create an iTextSharp watermark? I can't get this code to work.

Asked by freezegravity in C# Programming Language, Miscellaneous Programming, Microsoft Visual C#.Net

Tags: C#

Hello,

Problem: I want to add an image watermark (like an image in the background) on the first page of a pdf when it is uploaded to a site that I coded in .Net 2.0

Attempted Solution: used the open-source library iTextSharp version 4.1.1 but the sample contains a watermark class that is now non-existent.

Attached: code snippet that does not work, inspired largely by research on google. It doesn't work and I do not know why. I know that the code can open the files that the user enters into two textboxes, so what else could be the problem?

I downloaded the iTextSharp dll version 4.1.1 from source-forge and have a reference to it on my website.

Attached is sample code. Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
 
using iTextSharp.text;
using iTextSharp.text.pdf;
 
public partial class watermark_again : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnInsertWatermark_Click(object sender, EventArgs e)
    {
        MemoryStream strm = new MemoryStream();
        PdfReader pdfReader = new PdfReader(txtFileLocation.Text);
        PdfStamper stamp = new PdfStamper(pdfReader, strm);       
 
        iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(txtWatermarkLocation.Text);
        img.SetAbsolutePosition(250, 300);
 
        PdfContentByte waterMark = stamp.GetUnderContent(1);
        waterMark.AddImage(img);
        stamp.FormFlattening = true;
        stamp.Close();       
    }
}
[+][-]05.14.2008 at 09:55PM PDT, ID: 21570735

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Miscellaneous Programming, Microsoft Visual C#.Net
Tags: C#
Sign Up Now!
Solution Provided By: IndianOcean
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.15.2008 at 08:34AM PDT, ID: 21574784

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.15.2008 at 10:29PM PDT, ID: 21580227

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628