码迷,mamicode.com
首页 > Windows程序 > 详细

C# 将多个图片合成 pdf 及 多个pdf 合成 pdf 方案

时间:2021-04-09 13:38:52      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:++   bool   ret   dwr   删除   wpa   readwrite   out   void   

获取 itextsharp 包

 

 

    files 图片文件路径列表

    pdfFileName 输出的 pdf文件

public bool MergeFileToPDF(List<string> files,string pdfFileName)
{
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(pdfFileName, FileMode.Create, FileAccess.ReadWrite));
document.Open();
iTextSharp.text.Image image;
foreach (var item in files)
{
if (String.IsNullOrEmpty(item)) break;
image = iTextSharp.text.Image.GetInstance(item);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage();
document.Add(image);
}
document.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine("转换失败,原因:" + ex.Message);
}
return false;

}

 

 

合并两个PDF文件类

 

public class PdfHelper
{

/// <summary>
/// /// 读取合并的pdf文件名称
/// /// </summary>
/// /// <param name="Directorypath">目录</param>
/// /// <param name="outpath">导出的路径</param>
public static void MergePDF(string Directorypath, string outpath)
{
List<string> filelist2 = new List<string>();
System.IO.DirectoryInfo di2 = new System.IO.DirectoryInfo(Directorypath);
FileInfo[] ff2 = di2.GetFiles("*.pdf");
BubbleSort(ff2);
foreach (FileInfo temp in ff2)
{ filelist2.Add(Directorypath + "\\" + temp.Name); }
mergePDFFiles(filelist2, outpath);
//DeleteAllPdf(Directorypath);
}

/// <summary>
/// /// 冒泡排序
/// /// </summary>
/// /// <param name="arr">文件名数组</param>
public static void BubbleSort(FileInfo[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
for (int j = i; j < arr.Length; j++)
{
if (arr[i].LastWriteTime > arr[j].LastWriteTime)//按创建时间(升序)
{
FileInfo temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}


/// <summary>
/// /// 合成pdf文件
/// /// </summary>
/// /// <param name="fileList">文件名list</param>
/// /// <param name="outMergeFile">输出路径</param>
public static void mergePDFFiles(List<string> fileList, string outMergeFile)
{
PdfReader reader;
//Rectangle rec = new Rectangle(1660, 1000);
//Document document = new Document(rec);

iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage newPage;
for (int i = 0; i < fileList.Count; i++)
{
reader = new PdfReader(fileList[i]);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j <= iPageNum; j++)
{
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
}
document.Close();
}


/// <summary>
/// /// 删除一个文件里所有的文件
/// /// </summary>
/// /// <param name="Directorypath">文件夹路径</param>
public static void DeleteAllPdf(string Directorypath)
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Directorypath);
if (di.Exists)
{
FileInfo[] ff = di.GetFiles("*.pdf");
foreach (FileInfo temp in ff)
{
File.Delete(Directorypath + "\\" + temp.Name);
}
}
}
}

 

C# 将多个图片合成 pdf 及 多个pdf 合成 pdf 方案

标签:++   bool   ret   dwr   删除   wpa   readwrite   out   void   

原文地址:https://www.cnblogs.com/lhxsoft/p/14636308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!