[c#]代码库
using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;
namespace TIFtoPDF
{
class Program
{
//将多个tif文件合并成一个pdf文件
private static void tifToPdf(IEnumerable<string> arr, string sFilePdf)
{
FileInfo _toFile = new FileInfo(sFilePdf);
// 创建一个文档对象
Document doc = new Document(PageSize.A3, 0, 0, 0, 0);
int pages = 0;
FileStream fs=new FileStream(sFilePdf,FileMode.OpenOrCreate);
// 定义输出位置并把文档对象装入输出对象中
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
// 打开文档对象
doc.Open();
foreach(string sFileTif in arr)
{
PdfContentByte cb = writer.DirectContent;
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(sFileTif);
int comps = TiffImage.GetNumberOfPages(ra);
for (int c = 0; c < comps; ++c)
{
Image img = TiffImage.GetTiffImage(ra, c + 1);
if (img != null)
{
img.ScalePercent(7200f / img.DpiX, 7200f / img.DpiY);
doc.SetPageSize(new Rectangle(img.ScaledWidth, img
.ScaledHeight));
img.SetAbsolutePosition(0,0);
cb.AddImage(img);
doc.NewPage();
++pages;
}
}
ra.Close();// 关闭
}
// 关闭文档对象,释放资源
doc.Close();
}
public static void Main(string[] args)
{
tifToPdf(new string[]{@"C:\test.tif"},@"C:\test.pdf");
}
}
}
[源代码打包下载]