0

I am trying to read a string using PDF.Here i have a code which while running giving me error ..

Can not acess a closed file error in Itextsharp 

I am not getting why is this error coming.My code is in VB and i am for the first time working with VB and Itext both so having the problem.Here is my code..

Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim cb As iTextSharp.text.pdf.PdfContentByte = Nothing

Me.Cursor = Cursors.WaitCursor
If File.Exists(SourceFile) Then
    Dim pReader As New PdfReader(SourceFile)

    stamper = New iTextSharp.text.pdf.PdfStamper(pReader, New FileStream(DestinationFile, FileMode.Create))
    PB.Value = 0 : PB.Maximum = pReader.NumberOfPages
    For page As Integer = 1 To pReader.NumberOfPages
        Dim strategy As myLocationTextExtractionStrategy = New myLocationTextExtractionStrategy
        cb = stamper.GetUnderContent(page)

        'Send some data contained in PdfContentByte, looks like the first is always cero for me and the second 100, but i'm not sure if this could change in some cases
        strategy.UndercontentCharacterSpacing = cb.CharacterSpacing
        strategy.UndercontentHorizontalScaling = cb.HorizontalScaling

        'It's not really needed to get the text back, but we have to call this line ALWAYS, 
        'because it triggers the process that will get all chunks from PDF into our strategy Object
        Dim currentText As String = PdfTextExtractor.GetTextFromPage(pReader, page, strategy)

I am getting the error at the last line of the code..

Dim currentText As String = PdfTextExtractor.GetTextFromPage(pReader, page, strategy)

Code that is required as per comment

Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    'Parameter1: Search Text (1 word or many) 
    'Parameter2: Case Sensitiveness
    'Parameter3: Source file with Path 
    'Parameter4: Destination File with Path
    PDFTextGetter("Hello", StringComparison.CurrentCultureIgnoreCase, "D:\\pdf\\18.pdf", "D:\\Bill\\Test.pdf")
End Sub

Please help me .Thanks a lot!

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
user3664608
  • 333
  • 1
  • 3
  • 12
  • Where's your Source File comming from would be a start as provided code shows nothing of it and the root of your issue it seems... – Trevor May 29 '14 at 08:11
  • Also your using a new file stream, its a good practice to go ahead and set a new variable for the file using a' Using' block and then pass that into your PdfStamper... – Trevor May 29 '14 at 08:17
  • @MrCoDeXeR I have updated my post .Please see it. – user3664608 May 29 '14 at 08:30
  • well, it looks like you found the code here: http://stackoverflow.com/questions/2550796/reading-pdf-content-with-itextsharp-dll-in-vb-net-or-c-sharp so the only difference appears to be your `extractionStrategy`. i'm not familiar with this API though. also, catch to see if there's an exception, or make sure the strategy is valid. – porkchop May 29 '14 at 09:38

1 Answers1

1

You are using the pReader object for two different purposes:

  1. For stamping stuff on an existing document (stamper.GetUnderContent(page)). This alters the objects in the reader instance.
  2. To extract text from an existing document.

In other words: you're trying to read from a PdfReader instance while altering it. That won't work. You need to create two different PdfReader instances.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165