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

2021-05-16 02:27

阅读:617

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

获取 itextsharp 包

 

 

    files 图片文件路径列表

    pdfFileName 输出的 pdf文件

public bool MergeFileToPDF(List 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
{

///


/// /// 读取合并的pdf文件名称
/// ///

/// /// 目录
/// /// 导出的路径
public static void MergePDF(string Directorypath, string outpath)
{
List filelist2 = new List();
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);
}

///


/// /// 冒泡排序
/// ///

/// /// 文件名数组
public static void BubbleSort(FileInfo[] arr)
{
for (int i = 0; i {
for (int j = i; j {
if (arr[i].LastWriteTime > arr[j].LastWriteTime)//按创建时间(升序)
{
FileInfo temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}


///


/// /// 合成pdf文件
/// ///

/// /// 文件名list
/// /// 输出路径
public static void mergePDFFiles(List 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 {
reader = new PdfReader(fileList[i]);
int iPageNum = reader.NumberOfPages;
for (int j = 1; j {
document.NewPage();
newPage = writer.GetImportedPage(reader, j);
cb.AddTemplate(newPage, 0, 0);
}
}
document.Close();
}


///


/// /// 删除一个文件里所有的文件
/// ///

/// /// 文件夹路径
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


评论


亲,登录后才可以留言!