图像转pdf(c#版)
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#版)的更多相关文章
- Web开发入门经典:使用PHP6、Apache和MySQL 中文pdf扫描版
通过学习本书,读者很快就能明白为什么PHP.Apache和MySQL会迅速成为开发动态网站最流行的方式,本书将为读者理解这3个核心组件如何独立工作和协同工作奠定良好的基础,引导读者充分利用它们提供的各 ...
- 新编html网页设计从入门到精通 (龙马工作室) pdf扫描版
新编html网页设计从入门到精通共分为21章,全面系统地讲解了html的发展历史及4.0版的新特性.基本概念.设计原则.文件结构.文件属性标记.用格式标记进行页面排版.使用图像装饰页面.超链接的使用. ...
- HTML5 Canvas核心技术图形动画与游戏开发 ((美)David Geary) 中文PDF扫描版
<html5 canvas核心技术:图形.动画与游戏开发>是html5 canvas领域的标杆之作,也是迄今为止该领域内容最为全面和深入的著作之一,是公认的权威经典.amazon五星级超级 ...
- HTML5 Canvas核心技术:图形、动画与游戏开发 PDF扫描版
HTML5 Canvas核心技术:图形.动画与游戏开发 内容简介: <HTML5 Canvas核心技术:图形.动画与游戏开发>中,畅销书作家David Geary(基瑞)先生以实用的范例程 ...
- Head First HTML与CSS(第2版) 中文pdf扫描版
是不是已经厌倦了那些深奥的HTML书?你可能在抱怨,只有成为专家之后才能读懂那些书.那么,找一本新修订的<Head First HTML与CSS(第2版)>吧,来真正学习HTML.你可能希 ...
- CSS+DIV网页样式布局实战从入门到精通 中文pdf扫描版
CSS+DIV网页样式布局实战从入门到精通通过精选案例引导读者深入学习,系统地介绍了利用CSS和DIV进行网页样式布局的相关知识和操作方法. 全书共21章.第1-5章主要介绍网页样式布局的基础知识,包 ...
- HTML5与CSS3基础教程(第7版) 高清PDF扫描版
HTML5与CSS3基础教程(第7版)试读不仅介绍了文本.图像.链接.列表.表格.表单.多媒体等网页元素,也介绍了如何为网页设计结构.布局,添加动态效果.格式化等形式,此外还涉及调试和发布.聚合和吸引 ...
- HTML5与CSS3基础教程(第8版) PDF扫描版
<HTML5与CSS3基础教程(第8版)>自第1版至今,一直是讲解HTML和CSS入门知识的经典畅销书,全面系统地阐述HTML5和CSS3基础知识以及实际运用技术,通过大量实例深入浅出地分 ...
- HTML与CSS入门经典(第9版)试读 附随书源码 pdf扫描版
HTML与CSS入门经典(第9版)是经典畅销图书<HTML与CSS入门经典>的最新版本,与过去的版本相同,本书采用直观.循序渐进的方法,为读者讲解使用HTML5与CSS3设计.创建并维护世 ...
随机推荐
- [再寄小读者之数学篇](2014-04-01 from 2103471050@qq.com 曲线积分)
求 $\int_\vGa y^2\rd s$, 其中 $\vGa$ 由 $\dps{\sedd{\ba{rl} x^2+y^2+z^2&=a^2\\ x+z&=a \ea}}$ 决定. ...
- Git——如何将本地项目提交至远程仓库(第一次)
1.(先进入项目文件夹)通过命令 git init 把这个目录变成git可以管理的仓库. git init 2.把文件添加到版本库中,使用命令 git add .添加到暂存区里面去,不要忘记后面的小数 ...
- 使用tablayout和recyclerview的时候,报重复添加Fragment错误
原因: 在添加的子Fragment报错了, 出现了空值错误, 此时报出来错误是前一个Fragment重复添加
- 解决只读时ios下input光标问题
应用场景:在ios手机下对只读的input设置readonly=readonly属性还是会出现光标 解决方法: //解决ios日期光标问题 $("#Stime ,#provinceCity& ...
- 第一周——数据分析之表示 —— Numpy 数据存取与函数
数据的CSV文件的存取 CSV文件:CSV (Comma‐Separated Value, 逗号分隔值) CSV是一种常见的文件格式,用来存储批量数据 np.savetxt(frame, array, ...
- left join on和where
left join on: 向左关联某个表记录,意思是以左边的表记录为基准,通过关联条件会从左表返回所有的行,即使在右表中没有匹配的行. 举个例子: select * from A left join ...
- Linux 首先基本包安装(vim啊什么的),源,源优化,项目架构介绍, (LNMuWsgi)Django项目相关软件mysql,redies,python(相关模块)安装配置测试
内容 补充: 查看已启动服务的端口 netstat -tulnp |grep (方式1) ss -tulnp|grep (方式2) 前期铺垫: . Linux要能上网 . 掌握Linux软件包安装方法 ...
- Maven全局配置
Maven的全局配置文件是Maven安装目录conf/settings.xml文件,该文件可以配置仓库.代理.profile.镜像.插件等 <settings> <localRepo ...
- HTML5 图片下载
1. 概述 1.1 说明 在项目过程中,有时候需要下载某一展示图片,html5中定义了<a> download属性,download属性规定被下载的超链接目标,该属性可以设置一个值来规定下 ...
- jqGrid合并单元格
两个参数 /**合并单元格:合并指定 gridName表格的NoName 列,合并的标准是参考CellName+CellNameTwo列内单元格的值. * gridName :表格名称 * NoNam ...