Generating PDf using iTextSharp – .Net 2.0 – Modified namespace

A few days ago I was researching for something that could allow me to dynamically generate / edit a PDF file. Back in 2006 (when I first needed it) I wasn’t able to find such thing but now, it seems a hundred percent doable.

This iTextSharp library allow the programmer to easily manipulate PDF files, by that I mean create and edit it.

Ok, but once that I already gave the tip of the existence of such thing as iTS library and the link for you to download it, what would make this post worth or reading?

Perhaps the thing that this library was first wrote for .Net 1.1 (as you can see in Technical Requirements section in the main page of iTS project), so, if like myself you want to use it in 2.0 you need to convert it, right?

The thing is that after using .Net 2.0 conversion Tool I started to get an error of “document has no pages”, which [according to this post] is related to some *.afm embedded resources stored under “text/pdf/fonts/” folder. The conversion tool seems not to be able to keep these files as embedded after apply the changes needed to run it under .Net 2.0.

Even though I’ve manually set all .afm files as embedded resources, that annoying message was still there. At that time I saw that the only way would be to debugg it and try to see WTH was going on under the hood.

Turned out that the “BaseFont.cs” class under “text/pdf/” is the one responsible for “reading” those embedded font files. I noticed that the istr variable (that is filled by reflection) had null as value:

// Try to use resource loader to load the properties file.
  Assembly assm = Assembly.GetExecutingAssembly();
  istr = assm.GetManifestResourceStream(key);
}catch {}

By that I get to the real problem that was ME changing the name of the project. Right in the top of this same file you’ll find the following line:

/** The path to the font resources. */
public const string RESOURCE_PATH = "iTextSharp.text.pdf.fonts.";

This is the place you need to change to the current namespace you’re working on.

You may want to use the following code to create your very first “Please god, make it work” pdf file:

Document cd = new Document();
    PdfWriter.GetInstance(cd, new FileStream("C:\\temp\\test\\a.pdf", FileMode.Create));
    cd.Add(new Paragraph("Please god, make it work"));
catch (Exception e)
    /// Your handler

More info regarding iTextSharp can be found at:

2 comentários

  1. Hi there!!I am facing the same issue but unable to resolve it. Actually i was using dll but on server it gave Security Exception.So, after goggling i found that it can be resolved by implementing the source code in the website itself.I have copied the source code within folder into my App_Code, folders copied are iTextSharp, srcbc and System now the issue is same as you have written. Error is in “Assembly assm = Assembly.GetExecutingAssembly();istr = assm.GetManifestResourceStream(key);”lines which are unable to find the font Helvetica. Can you please tell me how you got rid of error…Thanks


  2. Hello there Amit. So, first of all I just wold like to add the I think that you might be running towards the most extreme solution of embedding the code into your solution to avoid the Security Exception…The thing about the fonts on iTS is that they area kinda fixed in the cod by using the constant RESOURCE as shown in my post.. have you checked that against the one you have in your project?My best regards!


Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google

Você está comentando utilizando sua conta Google. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s

%d blogueiros gostam disto: