我写了一些代码,通过Itext7将tiff文件转换为pdf文件。但是,当我启动它时,它在方法Image.ImageDataFactory.CreateTiff
上陷入了僵局。如果用foreach替换Parallel.Foreach,则所有工作正常。我做错了什么?
void Run(List<string> tiffsPaths)
{
Parallel.ForEach(tiffsPaths, ConvertTiffToPdf)
}
void ConvertTiffToPdf(string tiffPath)
{
var fileName = Path.GetFileNameWithoutExtension(tiffPath);
var pdfPath = Path.Combine(this.outputPicPath, fileName + ".pdf");
var pdfWriter = new PdfWriter(pdfPath);
var document = new PdfDocument(pdfWriter);
var workDir = Path.GetDirectoryName(SourceFile) ?? string.Empty;
var pageNum = 1;
var tiffImage = Image.ImageDataFactory.CreateTiff(new Uri(tiffPath), true, pageNum, true);
var tiffPageSize = new Geom.Rectangle(tiffImage.GetWidth(),
tiffImage.GetHeight());
var newPage = document.AddNewPage(new Geom.PageSize(tiffPageSize));
var canvas = new PdfCanvas(newPage);
canvas.AddImage(tiffImage, tiffPageSize, false);
document.Close();
pdfWriter.Close();
}
UPD
在类public IRandomAccessSource CreateSource(Uri url)
的方法itext.io.source.RandomAccessSourceFactory
中的 IText7 源代码中发现了发生死锁的位置。这段代码:
WebRequest wr = WebRequest.Create(url);
wr.Credentials = CredentialCache.DefaultCredentials;
Stream isp = wr.GetResponse().GetResponseStream();
答案 0 :(得分:0)
将var tiffImage = Image.ImageDataFactory.CreateTiff(new Uri(tiffPath), true, pageNum, true);
替换为var tiffImage = Image.ImageDataFactory.CreateTiff(File.ReadAllBytes(path), true, pageNum, true);
,一切正常