C# 将多个图片合成 pdf 及 多个pdf 合成 pdf 方案
2021/4/9 12:25:21
本文主要是介绍C# 将多个图片合成 pdf 及 多个pdf 合成 pdf 方案,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
获取 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 方案的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#