locked
ItextSharp RRS feed

  • Question

  • User-1542157572 posted

    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.

    Tuesday, June 24, 2008 1:43 AM

All replies

  • User1723862003 posted

    Hi there,

    Why dont you generate the PDF file on the fly once you hit the button?

    Tuesday, June 24, 2008 4:25 AM
  • User-1542157572 posted

    How do i go about doing this?

    Friday, June 27, 2008 3:05 AM
  • User1723862003 posted

    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

    Friday, June 27, 2008 3:41 AM
  • User-1542157572 posted

    i got the idea, but how do i read the content in a MS word document(.doc)

    Monday, June 30, 2008 1:51 AM
  • User-1912177094 posted

    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

    Monday, August 1, 2011 3:03 AM
  • User-1912177094 posted

    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

    Monday, August 1, 2011 3:03 AM