图像转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; }
}
}


}

posted @ 2018-06-28 16:53  安然mlg  阅读(5451)  评论(0编辑  收藏  举报