using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Media;
using System.IO;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace TIFtoPDF
{
class Program
{
static void Main(string[] args)
{

//tifToPdf(new string[] { @"F:\444.tif" }, @"F:\640.pdf");

// TiffHelper.TiffToPDF(@"F:\640.jpg");

//ConvertJPG2PDF(@"F:\641.jpg", @"F:\641.pdf");

}

private static void tifToPdf(IEnumerable<string> arr, string sFilePdf)
{
FileInfo toFile = new FileInfo(sFilePdf);
Document doc = new Document(PageSize.A4, 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)
{
iTextSharp.text.Image img = TiffImage.GetTiffImage(ra, c + 1);
if (img != null)
{
img.ScalePercent(7200f / img.DpiX, 7200f / img.DpiY);
doc.SetPageSize(new iTextSharp.text.Rectangle(img.ScaledWidth, img.ScaledHeight));
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
doc.NewPage();
++pages;
}
}
ra.Close();// 关闭
}
// 关闭文档对象,释放资源
doc.Close();
}

private static void ImageToPdf(string imgFilePath,string pdfFilePath)
{
var doc = new Document();
var stream = new FileStream(pdfFilePath, FileMode.Create, FileAccess.Write, FileShare.None);

using (stream)
{
PdfWriter.GetInstance(doc,stream);
doc.Open();
using (var imageStream = new FileStream(imgFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{

}
}

}

private static void ConvertJPG2PDF(string jpgfile, string pdf)
{
var document = new Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = iTextSharp.text.Image.GetInstance(imageStream);
if (image.Height > iTextSharp.text.PageSize.A4.Height )
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width , iTextSharp.text.PageSize.A4.Height );
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width )
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width , iTextSharp.text.PageSize.A4.Height );
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.Add(image);
}

document.Close();
}
}
}
public class TiffHelper
{
/// <summary>
/// Tiff 转为PDF格式
/// </summary>
/// <param name="path"></param>
/// <param name="src"></param>
/// https://social.msdn.microsoft.com/Forums/silverlight/zh-CN/c9b73655-877a-424d-9b3d-78fd552173a7/c2580520316222702925565288tiffjpgpng3156165289?forum=visualcshartzhchs
public static void TiffToPDF(string path)
{
string name = System.IO.Path.GetFileNameWithoutExtension(path);
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
Guid guid = (Guid)img.FrameDimensionsList.GetValue(0);
FrameDimension dimension = new FrameDimension(guid);
int totalPage = img.GetFrameCount(dimension);
List<PdfPageSize> list = new List<PdfPageSize>();
for (int i = 0; i < totalPage; i++)
{
System.Console.WriteLine(name + " tiff " + i);
img.SelectActiveFrame(dimension, i);
string jpg = AppDomain.CurrentDomain.BaseDirectory + string.Format("{0}_{1}", name, i);
img.Save(jpg, System.Drawing.Imaging.ImageFormat.Png);
list.Add(new PdfPageSize() { Width = img.Width, Height = img.Height, ImagePath = jpg });
}

FileStream fs = new FileStream("tiffpdf.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
iTextSharp.text.Document doc = new iTextSharp.text.Document();
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, fs);
doc.Open();
for (int k = 0; k < list.Count; k++)
{
System.Console.WriteLine("tiff " + k);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(list[k].ImagePath);
iTextSharp.text.Rectangle size = new iTextSharp.text.Rectangle((list[k].Width * 1.0f) / Zomm, (list[k].Height * 1.0f) / Zomm);
float margin = 1.0f;
if (image.Height > size.Height - margin)
{
image.ScaleToFit(size.Width - margin, size.Height - margin);
}
else if (image.Width > size.Width - margin)
{
image.ScaleToFit(size.Width - margin, size.Height - margin);
}
image.SetAbsolutePosition(0, 0);
doc.SetPageSize(size);
doc.NewPage();
writer.DirectContent.AddImage(image, false);
}
doc.Close();
fs.Close();
}

private static readonly float Zomm = 2.78f;

/// <summary>
/// PDF宽度和高度
/// </summary>
private class PdfPageSize
{
public int Width { get; set; }
public int Height { get; set; }
public string ImagePath { get; set; }
}
}

}

图像转pdf(c#版)的更多相关文章

  1. Web开发入门经典:使用PHP6、Apache和MySQL 中文pdf扫描版​

    通过学习本书,读者很快就能明白为什么PHP.Apache和MySQL会迅速成为开发动态网站最流行的方式,本书将为读者理解这3个核心组件如何独立工作和协同工作奠定良好的基础,引导读者充分利用它们提供的各 ...

  2. 新编html网页设计从入门到精通 (龙马工作室) pdf扫描版​

    新编html网页设计从入门到精通共分为21章,全面系统地讲解了html的发展历史及4.0版的新特性.基本概念.设计原则.文件结构.文件属性标记.用格式标记进行页面排版.使用图像装饰页面.超链接的使用. ...

  3. HTML5 Canvas核心技术图形动画与游戏开发 ((美)David Geary) 中文PDF扫描版​

    <html5 canvas核心技术:图形.动画与游戏开发>是html5 canvas领域的标杆之作,也是迄今为止该领域内容最为全面和深入的著作之一,是公认的权威经典.amazon五星级超级 ...

  4. HTML5 Canvas核心技术:图形、动画与游戏开发 PDF扫描版​

    HTML5 Canvas核心技术:图形.动画与游戏开发 内容简介: <HTML5 Canvas核心技术:图形.动画与游戏开发>中,畅销书作家David Geary(基瑞)先生以实用的范例程 ...

  5. Head First HTML与CSS(第2版) 中文pdf扫描版​

    是不是已经厌倦了那些深奥的HTML书?你可能在抱怨,只有成为专家之后才能读懂那些书.那么,找一本新修订的<Head First HTML与CSS(第2版)>吧,来真正学习HTML.你可能希 ...

  6. CSS+DIV网页样式布局实战从入门到精通 中文pdf扫描版

    CSS+DIV网页样式布局实战从入门到精通通过精选案例引导读者深入学习,系统地介绍了利用CSS和DIV进行网页样式布局的相关知识和操作方法. 全书共21章.第1-5章主要介绍网页样式布局的基础知识,包 ...

  7. HTML5与CSS3基础教程(第7版) 高清PDF扫描版​

    HTML5与CSS3基础教程(第7版)试读不仅介绍了文本.图像.链接.列表.表格.表单.多媒体等网页元素,也介绍了如何为网页设计结构.布局,添加动态效果.格式化等形式,此外还涉及调试和发布.聚合和吸引 ...

  8. HTML5与CSS3基础教程(第8版) PDF扫描版​

    <HTML5与CSS3基础教程(第8版)>自第1版至今,一直是讲解HTML和CSS入门知识的经典畅销书,全面系统地阐述HTML5和CSS3基础知识以及实际运用技术,通过大量实例深入浅出地分 ...

  9. HTML与CSS入门经典(第9版)试读 附随书源码 pdf扫描版​

    HTML与CSS入门经典(第9版)是经典畅销图书<HTML与CSS入门经典>的最新版本,与过去的版本相同,本书采用直观.循序渐进的方法,为读者讲解使用HTML5与CSS3设计.创建并维护世 ...

随机推荐

  1. windows 下 bat 计划任务删除保留时间内文件

    date  windows 打印时间戳  年:echo %date:~,% 月:echo %date:~,% 日:echo %date:~,% 星期:echo %date:~,% 小时:echo %t ...

  2. vscode 正则表达式替换

    比如把1.aa2.bbb替换成 1.aa2.bbb则,查找\d+ 替换成 \n$0 $0为查找的正则匹配项editplus为\0

  3. 使用Notepad++开发Java程序

    安装NppExec插件(已安装可跳过) 插件下载地址 我选择了最新的RC2 根据软件位数下载对应的版本,我直接下载了32位对应的dll 解压后里面有两个文件夹和一个dll文件 拷贝到Notepad++ ...

  4. Spring系列(三) Bean装配的高级技术

    profile 不同于maven的profile, spring的profile不需要重新打包, 同一个版本的包文件可以部署在不同环境的服务器上, 只需要激活对应的profile就可以切换到对应的环境 ...

  5. HT for Web框架使用心得

    一.简单介绍 在HT for Web的官网首页写着,构建先进2D和3D可视化所需要的一切. 是的,只要你看过官网,你就会知道,这是一个企业的.并非开源的且需要收费的框架. 因为公司的业务需要,且公司使 ...

  6. 【全网最全的博客美化系列教程】08.自定义地址栏Logo

    全网最全的博客美化系列教程相关文章目录 [全网最全的博客美化系列教程]01.添加Github项目链接 [全网最全的博客美化系列教程]02.添加QQ交谈链接 [全网最全的博客美化系列教程]03.给博客添 ...

  7. 【原创】数据库基础之Mysql(1)常用命令

    1 创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 比如 create user 'test_user'@'%' identi ...

  8. WPF 单个触发器、多个触发器、多条件触发器

    Trigger的使用.利用Trigger对象,我们可以接收到属性变化或者事件发生,并据此做出适当的响应.Trigger本身也是支持多种类型的,下面是一个属性Trigger的例子: <Style ...

  9. 十一.keepalived高可用服务实践部署

    期中集群架构-第十一章-keepalived高可用集群章节======================================================================0 ...

  10. redis-hash操作

    hset(name, key, value) # name对应的hash中设置一个键值对(不存在,则创建:否则,修改) # 参数: # name,redis的name # key,name对应的has ...