图像转pdf(c#版)

摘要:
usingiTextSharp.text;usingiTextSharp.text.pdf;usingiTextSharp.text.pdf.codec;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Media.Imaging;usingSystem

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#版)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇给折腾ramdisk的朋友们一点建议C# 获取一个byte数据中某一位的值下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#读取Excel sheet方法

最近忙着思考项目优化的工作,发现以前 导入excel的时候,再执行“如何获取excel要导入的sheet名称”的时候特别慢,今天在网上找了比较好,并且更方便的方法; 原先导入excel代码如下: private void EcxelToGridView()        {            labelControl1.Text = " ";    ...

pytest文档40-pytest.ini配置用例查找规则(面试题)

前言 面试题:pytest如何执行不是test开头的用例?如执行 xxx_*.py这种文件的用例。 pytest.ini 配置文件可以修改用例的匹配规则。 pytest命令行参数 cmd打开输入pytest -h 查看命令行参数找到 [pytest] ini-options python_files (args) 匹配 python 用例文件, 如tes...

MongoDB基础入门003--使用官方驱动操作mongo,C#

本篇先简单介绍一下,使用官方驱动来操作MongoDB。至于MongoDB原生的增删改查语句,且等以后再慢慢学习。 一、操作MongoDB的驱动主要有两个   1.官方驱动:https://github.com/mongodb/mongo-csharp-driver/downloads,更新的还是比较及时的,目前已经支持大部门linq语法。   2.samu...

C#--反射基础

以下是学习笔记: 一,反射的基本信息 DLL/EXE: 主要区别EXE文件有一个入口,DLL文件不能运行,但是DLL能拿到其他地方去使用 metadata(元数据):描述exe/dll文件的一个清单,记录了exe/dll文件中有哪些类,属性,特性,字段。。。 Reflection(反射):用来操作或获取元数据metadata 有什么作用: 1,更新程序(...

java中对list集合中的数据按照某一个属性进行分组

import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; p...

Java实现 “ 将数字金额转为大写中文金额 ”

前言:输入数字金额参数,运行程序得到其对应的大写中文金额;例如:输入 12.56,输出 12.56 : 壹拾贰元伍角陆分;重点来了:本人亲测有效。 奉上代码:/*** @Title: ConvertUpMoney* @Description: 将数字金额转换为大写中文金额* @date: 2019年6月18日 下午10:52:27*/public clas...