性能不佳使用ITextSharp将TIF转换为pdf [英] Bad performance convert tif to pdf using ITextSharp
问题描述
摘要:如何减少使用itextsharp
将tif转换为pdf所需的时间?
Summary: How can I reduce the amount of time it takes to convert tifs to pdfs using itextsharp
?
背景:我正在使用C#和itextsharp
将一些相当大的tif转换为pdf,但性能却变得非常差.每个tif文件大约50kb,某些文档最多包含150个单独的tif文件(每个代表一页).对于一份132页的文档(约6500 kb),转换大约需要13分钟.在转换过程中,其上运行的单个CPU服务器以100%的速度运行,这使我相信该进程受CPU限制.输出的pdf文件为3.5 MB.我可以接受这个大小,但是花费的时间对我来说似乎有点高.
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.
代码:
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();
}
}
推荐答案
将GetInstance方法参数修改为
Modify GetInstance method argument to
GetInstance(bm, ImageFormat.Tiff)
这可能会提高性能
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, ImageFormat.Tiff);
这篇关于性能不佳使用ITextSharp将TIF转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!