Cleaven Cleaven - 1 year ago 127
C# Question

I introduced an ambiguous reference between iTextSharp.text.Rectangle and System.Drawing.Rectangle

After I add the following

statements to my code, I get plenty of ambiguous reference errors:

using iTextSharp.text;
using iTextSharp.text.pdf;

This is a screen shot of the errors:


Does anybody know what's causing these errors? How I can introduce iTextSharp and avoid these errors?

Answer Source

You are experiencing something that is very normal and that no developer would describe as "creating errors." When a developer writes C# code, he creates classes in a certain namespace. For instance: at iText, we created classes such as Font in the namespace iTextSharp.text.

Obviously, the class name Font isn't very original, there are other libraries that use the same name in a different namespace. There's even a class named Font in the C# programming language, more specifically in the namespace System.Drawing.

That is perfectly OK. A compiler can easily see the difference between iTextSharp.text.Font and System.Drawing.Font because they have a different fully qualified name (that is: the full name of the class, including the namespace).

In your code, you confuse the compiler. You add a using statement, that allows the compiler to use the iTextSharp.text.Font class. However, when you use the Font class in your code, you don't specify which Font class you intend to use. Your compiler is asking you: do you mean to use iTextSharp.text.Font or System.Drawing.Font? And also: do you mean to use iTextSharp.text.Rectangle or System.Drawing.Rectangle?

You can solve this problem by using the fully qualified name so that the compiler is no longer confused.

For instance, without the namespace, your compiler doesn't know if you meant:

pnlReport.DrawToBitMap(bm, new System.Drawing.Rectangle(0, 0, width, height));


pnlReport.DrawToBitMap(bm, new iTextSharp.text.Rectangle(0, 0, width, height));

So you have to use:

pnlReport.DrawToBitMap(bm, new System.Drawing.Rectangle(0, 0, width, height));

instead of:

pnlReport.DrawToBitMap(bm, Rectangle(0, 0, width, height));

You can also use namespace aliases like:

using sys = System.Drawing;

to simplify callings like this:

pnlReport.DrawToBitMap(bm, new sys.Rectangle(0, 0, width, height));