[C#] TIF图像转PDF文件的工具 →→→→→进入此内容的聊天室

来自 , 2019-09-18, 写在 C#, 查看 103 次.
URL http://www.code666.cn/view/7edccc66
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. using iTextSharp.text;
  6. using iTextSharp.text.pdf;
  7. using iTextSharp.text.pdf.codec;
  8.  
  9. namespace TIFtoPDF
  10. {
  11.     class Program
  12.     {
  13.         //将多个tif文件合并成一个pdf文件
  14.         private static void tifToPdf(IEnumerable<string> arr, string sFilePdf)
  15.         {
  16.             FileInfo _toFile = new FileInfo(sFilePdf);
  17.             // 创建一个文档对象
  18.             Document doc = new Document(PageSize.A3, 0, 0, 0, 0);
  19.             int pages = 0;
  20.             FileStream fs=new FileStream(sFilePdf,FileMode.OpenOrCreate);
  21.             // 定义输出位置并把文档对象装入输出对象中
  22.             PdfWriter writer = PdfWriter.GetInstance(doc, fs);
  23.             // 打开文档对象
  24.             doc.Open();
  25.             foreach(string sFileTif in arr)
  26.             {
  27.                 PdfContentByte cb = writer.DirectContent;
  28.                 RandomAccessFileOrArray ra = new RandomAccessFileOrArray(sFileTif);
  29.                 int comps = TiffImage.GetNumberOfPages(ra);
  30.                 for (int c = 0; c < comps; ++c)
  31.                 {
  32.                     Image img = TiffImage.GetTiffImage(ra, c + 1);
  33.                     if (img != null)
  34.                     {
  35.                         img.ScalePercent(7200f / img.DpiX, 7200f / img.DpiY);
  36.                         doc.SetPageSize(new Rectangle(img.ScaledWidth, img
  37.                             .ScaledHeight));
  38.                         img.SetAbsolutePosition(0,0);
  39.                         cb.AddImage(img);
  40.                         doc.NewPage();
  41.                         ++pages;
  42.                     }
  43.                 }
  44.                 ra.Close();// 关闭
  45.             }
  46.             // 关闭文档对象,释放资源
  47.             doc.Close();
  48.         }
  49.          
  50.         public static void Main(string[] args)
  51.         {
  52.             tifToPdf(new string[]{@"C:\test.tif"},@"C:\test.pdf");
  53.         }
  54.     }
  55. }

回复 "TIF图像转PDF文件的工具"

这儿你可以回复上面这条便签

captcha