Summary: How can I reduce the amount of time it takes to convert tifs to pdfs using itextsharp
?
Background: I'm converting some fairly large tif's to pdf using C# and itextsharp
, and I am getting extremely bad performance. The tif files are approximately 50kb a piece, and some documents have up to 150 seperate tif files (each representing a page). For one 132 page document (~6500 kb) it took about 13 minutes to convert. During the conversion, the single CPU server it was running on was running at 100%, leading me to believe the process was CPU bound. The output pdf file was 3.5 MB. I'm ok with the size, but the time taken seem a bit high to me.
Code:
private void CombineAndConvertTif(IList<FileInfo> inputFiles, FileInfo outputFile)
{
using (FileStream fs = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = writer.DirectContent;
foreach (FileInfo inputFile in inputFiles)
{
using (Bitmap bm = new Bitmap(inputFile.FullName))
{
int total = bm.GetFrameCount(FrameDimension.Page);
for (int k = 0; k < total; ++k)
{
bm.SelectActiveFrame(FrameDimension.Page, k);
//Testing shows that this line takes the lion's share (80%) of the time involved.
iTextSharp.text.Image img =
iTextSharp.text.Image.GetInstance(bm, null, true);
img.ScalePercent(72f / 200f * 100);
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
document.NewPage();
}
}
}
document.Close();
writer.Close();
}
}