我正在使用iTextSharp将单页TIF文件转换和拼接为多页PDF文件。单页TIF文件具有不同的位深和压缩率。
这是代码-
private void button1_Click(object sender, EventArgs e)
{
List<string> TIFfiles = new List<string>();
Document document;
PdfWriter pdfwriter;
Bitmap tifFile;
pdfFilename = <file path>.PDF;
TIFfiles = <load the path to each TIF file in this array>;
//Create document
document = new Document();
// creation of the different writers
pdfwriter = PdfWriter.GetInstance(document, new System.IO.FileStream(pdfFilename, FileMode.Create));
document.Open();
document.SetMargins(0, 0, 0, 0);
foreach (string file in TIFfiles)
{
//load the tiff image
tifFile = new Bitmap(file);
//Total number of pages
iTextSharp.text.Rectangle pgSize = new iTextSharp.text.Rectangle(tifFile.Width, tifFile.Height);
document.SetPageSize(pgSize);
document.NewPage();
PdfContentByte cb = pdfwriter.DirectContent;
tifFile.SelectActiveFrame(FrameDimension.Page, 0);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(tifFile, ImageFormat.Tiff);
// scale the image to fit in the page
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
}
document.Close();
}
Run Code Online (Sandbox Code Playgroud)
此代码效果很好,可以将tif拼接和转换为PDF。问题在于处理某些类型的TIF时创建的处理时间和pdf文件大小。
例如
原始TIF->黑白/位深1 /压缩CCITT T.6->处理速度更快,PDF文件大小约为TIF文件大小的1.1倍。
原始TIF->颜色/位深8 /压缩LZW->处理速度更快,PDF文件大小约为TIF文件大小的1.1倍。
原始TIF->颜色/位深24 /压缩JPEG-> 处理速度慢,PDF文件大小约为 TIF文件大小的12.5倍。
为什么不转换颜色/位深24 /压缩JPEG文件会得到与其他tif文件类似的结果?
此外,此问题仅与iTextSharp有关。我有一位同事使用Java-iText测试了同一组TIF样本,结果PDF的尺寸较小(1.1倍),并且处理速度更快。
不幸的是,我需要使用.Net来实现从TIF到PDF的转换,因此一直无法使用iTextSharp。关于如何获取这些JPEG JPEG TIF文件以创建较小尺寸的PDF(对于其他TIF压缩而言)的任何想法/建议?
感谢你的帮助!
问候,AG
小智 5
我能够使用您提供的代码重现您的问题,但是发现一旦我使用Image.GetInstance而不是示例中使用的位图,问题就消失了。使用下面的代码时,Java和C#之间的文件大小和运行时间相同。如果您对样本有任何疑问,请随时提出。
List<string> TIFfiles = new List<string>();
Document document;
PdfWriter pdfwriter;
iTextSharp.text.Image tifFile;
String pdfFilename = pdfFile;
TIFfiles = new List<string>();
TIFfiles.Add(tifFile1);
TIFfiles.Add(tifFile2);
TIFfiles.Add(tifFile3);
TIFfiles.Add(tifFile4);
TIFfiles.Add(tifFile5);
TIFfiles.Add(tifFile6);
TIFfiles.Add(tifFile7);
//Create document
document = new Document();
// creation of the different writers
pdfwriter = PdfWriter.GetInstance(document, new System.IO.FileStream(pdfFilename, FileMode.Create));
document.Open();
document.SetMargins(0, 0, 0, 0);
int i = 0;
while (i < 50)
{
foreach (string file in TIFfiles)
{
//load the tiff image
tifFile = iTextSharp.text.Image.GetInstance(file);
//Total number of pages
iTextSharp.text.Rectangle pgSize = new iTextSharp.text.Rectangle(tifFile.Width, tifFile.Height);
document.SetPageSize(pgSize);
document.NewPage();
PdfContentByte cb = pdfwriter.DirectContent;
// scale the image to fit in the page
tifFile.SetAbsolutePosition(0, 0);
cb.AddImage(tifFile);
}
i++;
}
document.Close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1921 次 |
| 最近记录: |