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. javascript节点移除

    var itemdel = document.getElementById("test"); itemdel.removeChild(lis[0]); 兼容性较好 itemdel. ...

  2. [物理学与PDEs]第1章第2节 预备知识 2.2 Ampere-Biot-Savart 定律, 静磁场的散度与旋度

    1. 电流密度, 电荷守恒定律 (1) 电荷的定向移动形成电流. (2) 电流密度 ${\bf j}$, 是描述导体内一点在某一时刻电流流动情况的物理量, 用单位时间内通过垂直于电流方向的单位面积的电 ...

  3. fetch 的控制器和观察者

    因为受 cancelable promise 的拖延,fetch 一直没有传统的 XHR 所拥有的 abort() 和 onprogress 功能,去年年底 cancelable promise 草案 ...

  4. sqlserver 生成脚本执行创建索引

    create or alter proc SP_CreateIndex as begin if exists(select * from sys.objects where name='execsql ...

  5. CCPC-Wannafly Winter Camp Day5 (Div2, onsite) Sorting(线段树)

    题目链接 题意 对序列进行三种操作: 1.区间求和. 2.将区间小于等于$x$的数不改变相对顺序的前提下放到$x$左边,用同样规则将比$x$大的放到右边. 3.将区间大于$x$的数不改变相对顺序的前提 ...

  6. 请求超时VUE axios重新再次请求

    //在main.js设置全局的请求次数,请求的间隙 axios.defaults.retry = 4; axios.defaults.retryDelay = 1000; axios.intercep ...

  7. 微信小程序【获取验证码】倒计时效果

    最近开始接触微信小程序,会记录一些相关的小功能——例如这次是点击[获取验证码]按钮出现的倒计时效果. 原文: http://blog.csdn.net/Wu_shuxuan/article/detai ...

  8. spring cloud 注册中心--eureka注册与发现

    本文详细介绍spring cloud微服务的默认注册中心--eureka注册与发现.开发环境需要Windows系统.jdk和intellij idea.与zookeeper注册中心相比,eureka不 ...

  9. NB卡开卡注意事项【转】

    转自:https://blog.csdn.net/cheng401733277/article/details/83276436 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...

  10. asyncio协议

    服务端 import asyncio import logging import sys from typing import Optional SERVER_ADDRESS = ('localhos ...