Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15406

ITextSharp - Different performance between file reading and TIFF page reading

$
0
0
Hello, I'm anticipating that the programming for me is in its infancy, so I expose a problem maybe trivial but for me inexplicable.
I have created a simple SUB that calls a function for all the TIF files contained in a folder, passing it the full path (e.g. c:\test\1.tif) and only the name (e.g. 1.tif).
Each TIF contains 2 sides
The function processes all files and converts them to PDF with iTextSharp version 5.3.0.
https://www.nuget.org/packages/iTextSharp/5.3.3


vb.net Code:
  1. Sub SurroundingSub3(FullName As String, Name As String)
  2.     Dim Document As New iTextSharp.text.Document()
  3.  
  4.     Dim FirstPage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(FullName)
  5.     Dim MyX As Double = FirstPage.ScaledWidth / 100 * 24
  6.     Dim MyY As Double = FirstPage.ScaledHeight / 100 * 24
  7.     FirstPage.ScaleToFit(MyX, MyY)
  8.     FirstPage.SetAbsolutePosition(0, 0)
  9.     Dim FirstPageSize As New iTextSharp.text.Rectangle(MyX, MyY)
  10.     Document.SetPageSize(FirstPageSize)
  11.  
  12.     Dim pdfFILE As String = Application.StartupPath & "" & Name & ".pdf"
  13.     Dim DocumentWriter As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(Document, New IO.FileStream(pdfFILE, IO.FileMode.Create)) 'Inizializza il PDF
  14.     Document.Open()
  15.  
  16.     Dim myImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(FullName)
  17.     myImage.ScaleToFit(MyX, MyY)
  18.     myImage.SetAbsolutePosition(0, 0)
  19.     Dim PageSize As New iTextSharp.text.Rectangle(MyX, MyY)
  20.     Document.SetPageSize(PageSize)
  21.     Document.Add(myImage)
  22.     Document.NewPage()
  23.  
  24.     Document.Close()
  25. End Sub

This code processes 100 test files in about 6 seconds but only takes into account the first page of the TIF.
I then grabbed a second code from the web to have all pages processed and this is it:

vb.net Code:
  1. Sub SurroundingSub(FullName As String, Name As String)
  2.     Dim document As iTextSharp.text.Document = New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
  3.     Dim writer As iTextSharp.text.pdf.PdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, New System.IO.FileStream(Application.StartupPath & "" & Name & ".pdf", System.IO.FileMode.Create))
  4.     Dim bm As System.Drawing.Bitmap = New System.Drawing.Bitmap(FullName)
  5.     Dim total As Integer = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page)
  6.     document.Open()
  7.     Dim cb As iTextSharp.text.pdf.PdfContentByte = writer.DirectContent
  8.  
  9.     For k As Integer = 0 To total - 1
  10.         bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k)
  11.         Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Tiff)
  12.         img.SetAbsolutePosition(0, 0)
  13.         img.ScaleAbsoluteHeight(document.PageSize.Height)
  14.         img.ScaleAbsoluteWidth(document.PageSize.Width)
  15.         cb.AddImage(img)
  16.         document.NewPage()
  17.     Next
  18.     document.Close()
  19. End Sub

This converts the 100 files from before, putting both pages into the PDF, in about 80 seconds.
Is this much difference normal? I mean, I would have expected double the time, maybe a little more, but not more than 13 times as much...
I'd like to understand if I'm doing something wrong or if it's normal...
Obviously the two pages of each TIF are almost identical, if you need I'll share them.

Thank you to those who will take the time to answer me. :)

Viewing all articles
Browse latest Browse all 15406

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>