C#实现TIF图像转PDF文件的方法

   2015-07-07 0
核心提示:这篇文章主要介绍了C#实现TIF图像转PDF文件的方法,涉及C#使用TIFtoPDF工具实现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#程序设计有所帮助。

 
标签: C# PDF
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

点击排行