i am usinf itextsharp to add metadata to a pdf file which is already existing. I am using pdfStamper class to do this but i should not create a new pdf file only i need to modify the existing one and save the changes to it. How do i go about doing this please
help me.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As FileStream
Dim sr As StreamReader
Dim mydocument As Document
Dim i As Integer
fs = New FileStream("c:\LOGFILE.txt", FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
Dim t As String
t = "X"
While t <> ""
If i < 1 Then
t = sr.ReadToEnd
i = i + 1
mydocument = New Document
PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Create))
mydocument.AddTitle("SamplePDF")
mydocument.AddAuthor("Srinivas")
mydocument.AddCreator("PDM Technologies")
mydocument.AddKeywords("First testing program for checking the metada in pdf")
mydocument.AddSubject("PDF Creator")
mydocument.AddHeader("PDM Technologies", "PDM Technologies")
Else
t = sr.ReadToEnd
i = i + 1
If t <> "" Then
mydocument = New Document
PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Append, FileAccess.Write))
mydocument.Open()
mydocument.Add(New Paragraph(t))
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 sr As StreamReader
Dim t As String
Dim m As MemoryStream = New MemoryStream
Dim md As MetaData = New MetaData
Dim Reader As PdfReader = New PdfReader("c:\csharp.pdf")
md.info1 = Reader.Info
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
Public Class MetaData
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)
info.Add("Author", Value)
End Set
End Property
Public Property Title() As String
Get
Return info1(Title)
End Get
Set(ByVal Value As String)
info.Add("Title", Value)
End Set
End Property
Public Property Subject() As String
Get
Return info1(Subject)
End Get
Set(ByVal Value As String)
info.Add("Subject", Value)
End Set
End Property
Public Property Keywords() As String
Get
Return info1(Keywords)
End Get
Set(ByVal Value As String)
info.Add("Keywords", Value)
End Set
End Property
Public Property Producer() As String
Get
Return info1(Producer)
End Get
Set(ByVal Value As String)
info.Add("Producer", Value)
End Set
End Property
Public Property Creator() As String
Get
Return info1(Creator)
End Get
Set(ByVal Value As String)
info.Add("Creator", Value)
End Set
End Property
Public Property Version() As String
Get
Return info1(Version)
End Get
Set(ByVal Value As String)
info.Add("Version", Value)
End Set
End Property
Public Property Header() As String
Get
Return info1(Header)
End Get
Set(ByVal Value As String)
info.Add("Header", Value)
End Set
End Property
Member
11 Points
107 Posts
ItextSharp
Jun 24, 2008 01:43 AM|Geetha Naidu C. P.|LINK
HI all,
i am usinf itextsharp to add metadata to a pdf file which is already existing. I am using pdfStamper class to do this but i should not create a new pdf file only i need to modify the existing one and save the changes to it. How do i go about doing this please help me.
Participant
1519 Points
381 Posts
Re: ItextSharp
Jun 24, 2008 04:25 AM|kalyan1982|LINK
Hi there,
Why dont you generate the PDF file on the fly once you hit the button?
Member
11 Points
107 Posts
Re: ItextSharp
Jun 27, 2008 03:05 AM|Geetha Naidu C. P.|LINK
How do i go about doing this?
Participant
1519 Points
381 Posts
Re: ItextSharp
Jun 27, 2008 03:40 AM|kalyan1982|LINK
Refer below links!
http://www.developerfusion.co.uk/show/5682/
http://www.codeproject.com/KB/aspnet/Creating_PDF_documents_in.aspx
If you wanna try a different appraoach:
http://www.aspnetworld.com/articles/2004011801.aspx
Participant
1519 Points
381 Posts
Re: ItextSharp
Jun 27, 2008 03:41 AM|kalyan1982|LINK
You can try the below code:
Add the itextsharp.dll in the references
Add 2 command buttons
Add 6 text boxes
Add the Class MetaData with in the form class
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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As FileStream
Dim sr As StreamReader
Dim mydocument As Document
Dim i As Integer
fs = New FileStream("c:\LOGFILE.txt", FileMode.Open, FileAccess.Read)
sr = New StreamReader(fs)
Dim t As String
t = "X"
While t <> ""
If i < 1 Then
t = sr.ReadToEnd
i = i + 1
mydocument = New Document
PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Create))
mydocument.AddTitle("SamplePDF")
mydocument.AddAuthor("Srinivas")
mydocument.AddCreator("PDM Technologies")
mydocument.AddKeywords("First testing program for checking the metada in pdf")
mydocument.AddSubject("PDF Creator")
mydocument.AddHeader("PDM Technologies", "PDM Technologies")
mydocument.Open()
mydocument.Add(New Paragraph(t))
mydocument.Close()
Else
t = sr.ReadToEnd
i = i + 1
If t <> "" Then
mydocument = New Document
PdfWriter.GetInstance(mydocument, New FileStream("c:\csharp.pdf", FileMode.Append, FileAccess.Write))
mydocument.Open()
mydocument.Add(New Paragraph(t))
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 sr As StreamReader
Dim t As String
Dim m As MemoryStream = New MemoryStream
Dim md As MetaData = New MetaData
Dim Reader As PdfReader = New PdfReader("c:\csharp.pdf")
md.info1 = Reader.Info
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
Public Class MetaData
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)
info.Add("Author", Value)
End Set
End Property
Public Property Title() As String
Get
Return info1(Title)
End Get
Set(ByVal Value As String)
info.Add("Title", Value)
End Set
End Property
Public Property Subject() As String
Get
Return info1(Subject)
End Get
Set(ByVal Value As String)
info.Add("Subject", Value)
End Set
End Property
Public Property Keywords() As String
Get
Return info1(Keywords)
End Get
Set(ByVal Value As String)
info.Add("Keywords", Value)
End Set
End Property
Public Property Producer() As String
Get
Return info1(Producer)
End Get
Set(ByVal Value As String)
info.Add("Producer", Value)
End Set
End Property
Public Property Creator() As String
Get
Return info1(Creator)
End Get
Set(ByVal Value As String)
info.Add("Creator", Value)
End Set
End Property
Public Property Version() As String
Get
Return info1(Version)
End Get
Set(ByVal Value As String)
info.Add("Version", Value)
End Set
End Property
Public Property Header() As String
Get
Return info1(Header)
End Get
Set(ByVal Value As String)
info.Add("Header", Value)
End Set
End Property
End Class
Member
11 Points
107 Posts
Re: ItextSharp
Jun 30, 2008 01:51 AM|Geetha Naidu C. P.|LINK
i got the idea, but how do i read the content in a MS word document(.doc)
None
0 Points
2 Posts
Re: ItextSharp
Aug 01, 2011 03:03 AM|Himvj|LINK
Hi kalyan,
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.Dictionary`2[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.
Regards
Himvj
None
0 Points
2 Posts
Re: ItextSharp
Aug 01, 2011 03:03 AM|Himvj|LINK
Hi kalyan,
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.Dictionary`2[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.
Regards
Himvj