這篇文章主要介紹了C#實(shí)現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法,涉及C#使用TIFtoPDF工具實(shí)現(xiàn)pdf文件轉(zhuǎn)換的技巧,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
這里介紹使用TIFtoPDF的用法。該工具可以將多個(gè)TIF圖像文件合并成一個(gè)PDF文件
TIFtoPDF.rar文件點(diǎn)擊此處本站下載。
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
 - {
 - //將多個(gè)tif文件合并成一個(gè)pdf文件
 - private static void tifToPdf(IEnumerable<string> arr, string sFilePdf)
 - {
 - FileInfo _toFile = new FileInfo(sFilePdf);
 - // 創(chuàng)建一個(gè)文檔對(duì)象
 - Document doc = new Document(PageSize.A3, 0, 0, 0, 0);
 - int pages = 0;
 - FileStream fs=new FileStream(sFilePdf,FileMode.OpenOrCreate);
 - // 定義輸出位置并把文檔對(duì)象裝入輸出對(duì)象中
 - PdfWriter writer = PdfWriter.GetInstance(doc, fs);
 - // 打開(kāi)文檔對(duì)象
 - 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();// 關(guān)閉
 - }
 - // 關(guān)閉文檔對(duì)象,釋放資源
 - doc.Close();
 - }
 - public static void Main(string[] args)
 - {
 - tifToPdf(new string[]{@"C:/test.tif"},@"C:/test.pdf");
 - }
 - }
 - }
 
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注