[5682] iTextSharp :library to create PDF Files on fly in C#.net

• 10 years ago

Thank you for the Shortest Explanation
I was able to work out with vb.net using your help

Srinivas

Add the itextsharp.dll in the references

copy the code in the respective places and run

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.Data
Imports System.Text
Imports System.Drawing
Imports System.ComponentModel
Imports System.Collections

Private Sub Button1Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim fs As FileStream
Dim mydocument As Document
Dim i As Integer

fs = New FileStream("c:\LOGFILE.txt", FileMode.Open, FileAccess.Read)

Dim t As String

t = "X"
While t <> ""
If i < 1 Then
i = i + 1

mydocument = New Document

PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Create))

mydocument.Open()
mydocument.Close()

Else
i = i + 1
If t <> "" Then
mydocument = New Document

PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Append, FileAccess.Write))

mydocument.Open()

mydocument.Close()
End If
End If

End While

sr.Close()
fs.Close()

End Sub

Private Sub Button2
Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim fs As FileStream
Dim sw As StreamWriter
Dim mydocument As Document
Dim fr As FileStream
Dim t As String

Dim m As MemoryStream = New MemoryStream

TextBox1.Text = md.info1("Author")

TextBox2.Text = md.info1("Subject")

TextBox3.Text = md.info1("Title")

TextBox4.Text = md.info1("Creator")

TextBox5.Text = md.info1("Producer")

TextBox6.Text = md.info1("Keywords")

End Sub

Private info As Hashtable = New Hashtable

Public Property info1()
Get
Return info
End Get
Set(ByVal Value)
info = Value
End Set
End Property

Public Property Author() As String
Get
Return info1(Author)
End Get
Set(ByVal Value As String)
End Set
End Property
Public Property Title() As String
Get
Return info1(Title)
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property Subject() As String
Get
Return info1(Subject)
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property Keywords() As String
Get
Return info1(Keywords)
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property Producer() As String
Get
Return info1(Producer)
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property Creator() As String
Get
Return info1(Creator)
End Get
Set(ByVal Value As String)
End Set
End Property

Public Property Version() As String
Get
Return info1(Version)
End Get
Set(ByVal Value As String)
End Set
End Property
Get
End Get
Set(ByVal Value As String)
End Set
End Property

End Class

• 10 years ago

Is there any other way to create our own schemas(Custom) for writing  and reading the meta data

Thank you

• 10 years ago

hi there,

nice and cool stuff , but works with text files only; what if i have images to be converted into pdf, or a word document with images.

• 10 years ago

Hi

i can find in  http://www.lowagie.com/iText/

• 10 years ago

Hi

i can't find in  http://www.lowagie.com/iText/

• 10 years ago

I tried the example, but I got an error message --
Compiler Error Message: CS0117: 'iTextSharp.text.pdf.PdfWriter' does not contain a definition for 'getInstance'

I'm wondering what is wrong.

Thanks,
Mike

• 10 years ago

I tried the example, but I got an error message --
Compiler Error Message: CS0117: 'iTextSharp.text.pdf.PdfWriter' does not contain a definition for 'getInstance'

I'm wondering what is wrong.

Thanks,
Mike

• 10 years ago

Try:

PdfWriter.GetInstance(mydocument, New FileStream("filename.pdf", FileMode.Create));

If you're using C# it's case sensitive and the "g" needs to be uppercase.

• 10 years ago
I have a problem using itextsharp: I want to append a document to one existing, therefore i write:
PdfWriter.GetInstance(doc,new FileStream(filename,FileMode.Append,FileAccess.Write));
I also tried a StreamWriter:
StreamWriter sw = new StreamWriter(filename,true);
sw.AutoFlush = true;
PdfWriter.GetInstance(doc,sw.BaseStream);

Now the Problem is, that the Filesize of the pdf document is getting bigger and bigger, but I can always just see the last page added to the document

does anyone has any idea?? thx
• 10 years ago

http://sourceforge.net/project/showfiles.php?group_id=72954&package_id=72901&release_id=458860
it's for CSharp (.NET)

But I need Tutorial on iTextSharp as I need to genetrate *.JTF , *.PDL file by "iTextSharp"

can any one help regards this

• 10 years ago

both C# and VB.NET(over 100 code examples)

• 10 years ago
This package cannot create PDF file from a string variable that contains Arabic letters

When creating PDF file of Arabic letters, there is no text in the output file.

• 9 years ago

Hello,I found out how to generate Arabic PDF files, here is an example:

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

// step 2

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("righttoleft.pdf",FileMode.Create));

// step 3

document.Open();

BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\times.ttf", BaseFont.IDENTITY_H, true);

iTextSharp.text.

Font f2 = new iTextSharp.text.Font(bf, 24, iTextSharp.text.Font.NORMAL, iTextSharp.text.Color.BLUE);

String atext = "اهلا";

PdfPTable table = new PdfPTable(1);

table.RunDirection=

PdfWriter.RUN_DIRECTION_RTL;

PdfPCell cell = new PdfPCell(new Phrase(10, atext, f2));

document.Close();

• 9 years ago

Hello,

Iam using itextsharp 4.0.2.0 and vc++.net 2003.
Could you please tell me how to create an array of image objects in Itextsharp
I tried this
iTextSharp::text::Image *in[]=new iTextSharp::text::Image[50];
for (int i=0;i<Length; i++)

in[i]= iTextSharp::

text::Image::GetInstance(imgfilename[i]);  //imgfilename[i]- path of image

in[i]->ScalePercent(95);
}

but it does not work.

• 9 years ago

Hello,

Iam using itextsharp 4.0.2.0 and vc++.net 2003.
Could you please tell me how to create an array of image objects in Itextsharp
I tried this
iTextSharp::text::Image *in[]=new iTextSharp::text::Image[50];
for (int i=0;iin[i]= iTextSharp::text::Image::GetInstance(imgfilename[i]);//imgfilename[i])- path of image

in[i]->ScalePercent(95);
}

but it does not work.

• 7 years ago

Hello, this code works fine and display URDU in pdf file but it did not display correctly. actually it break the font. May be there is a rendering issue in it. but i can not solve it.

so if any one can help me to resolve this issue i will be thankful to him. Thanks

Regards Ali bin zubair

• 6 years ago

Hi lenda,

I can't find the class iTextSharp.text.Color and the table is just added to the document without the arabic text I am using itextsharp version 5.0.2, I need to write in Arabic but it's not implemented till now

• 5 years ago

Hi ,

I tried this code to create pdf from text file it is working fine on button 1 click

it is creating pdf and assinging properties to it.

but when i am reading the properties value from the pdf on button2 click getting the following error.

Unable to cast object of type 'System.Collections.Generic.Dictionary2[System.String,System.String]' to type 'System.Collections.Hashtable'.

Public Property info1() Get Return info End Get Set(ByVal Value) info = Value End Set End Property

in this particular line

info = value

how to resolve that....

• 5 years ago

Hi,

I tried this code to create pdf from text file it is working fine on button 1 click

it is creating pdf and assinging properties to it.

but when i am reading the properties value from the pdf on button2 click getting the following error.

Unable to cast object of type 'System.Collections.Generic.Dictionary2[System.String,System.String]' to type 'System.Collections.Hashtable'.

Public Property info1() Get Return info End Get Set(ByVal Value) info = Value End Set End Property

in this particular line

info = value

how to resolve that....

• 5 years ago

Hi,

I tried this code to create pdf from text file it is working fine on button 1 click it is creating pdf and assinging properties to it. but when i am reading the properties value from the pdf on button2 click getting the following error.

Unable to cast object of type 'System.Collections.Generic.Dictionary2[System.String,System.String]' to type 'System.Collections.Hashtable'.

Public Property info1()
Get
Return info
End Get
Set(ByVal Value)
info = Value
End Set
End Property
`

in this particular line

info = value

how to resolve that.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”