C#完成TIF圖象轉PDF文件的辦法。本站提示廣大學習愛好者:(C#完成TIF圖象轉PDF文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成TIF圖象轉PDF文件的辦法正文
本文實例講述了C#完成TIF圖象轉PDF文件的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
這裡引見應用TIFtoPDF的用法。該對象可以將多個TIF圖象文件歸並成一個PDF文件
TIFtoPDF.rar文件點擊此處本站下載。
Program.cs文件以下:
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");
}
}
}
願望本文所述對年夜家的C#法式設計有所贊助。