添加MergePDFImage类到项目中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
namespace MergePDFAndImage
{
public class MergePDF
{
public MergePDF() { }
/// <summary>
/// 将一个pdf和多张图片进行整合
/// </summary>
/// <param name="oldPdf">老的pdf</param>
/// <param name="imageList">需要整合的圖片</param>
/// <param name="newPdf">整合後新的pdf</param>
/// <param name="ImgX1">圖片1的X座標</param>
/// <param name="ImgY1">圖片1的Y座標</param>
/// <param name="ImgWidth1">圖片1的寬</param>
/// <param name="ImgHeigth1">圖片1的高</param>
/// <param name="TempX1">模版1的X座標</param>
/// <param name="TempY1">模版1的Y座標</param>
/// <param name="ImgX2">圖片2的X座標</param>
/// <param name="ImgY2">圖片2的Y座標</param>
/// <param name="ImgWidth2">圖片2的寬</param>
/// <param name="ImgHeigth2">圖片2的高</param>
/// <param name="TempX2">模版2的X座標</param>
/// <param name="TempY2">模版2的Y座標</param>
public static void mergePDFwithMoreImage(string oldPdf, string[] imageList, string newPdf, float ImgX1, float ImgY1, float ImgWidth1,
float ImgHeigth1, float TempX1, float TempY1, float ImgX2, float ImgY2, float ImgWidth2, float ImgHeigth2, float TempX2, float TempY2)
{
//读取想要修改的PDF
PdfReader reader = new PdfReader(oldPdf);
//得到老PDF的所有页码
int n = reader.NumberOfPages;
//创建一个新的PDF
Document document = new Document(reader.GetPageSizeWithRotation(1));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(newPdf, FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
////读取图片
//for (int j = 0; j < imageList.Length; j++)
//{
// iTextSharp.text.Image mm = iTextSharp.text.Image.GetInstance(imageList[j]);
// //设置图片的位置
// mm.SetAbsolutePosition(50 + j, 50 + j);
// //设置图片的大小
// mm.ScaleAbsolute(50 + j, 50 + j);
//}
int i = 0;
//循环要修改的PDF的页数
while (i < n)
{
i++;
//将拷贝过来的内容添加到模板上
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
//在PDF第一页上面加上签名档
if (i == 1)
{
iTextSharp.text.Image mm;
PdfContentByte cb1 = writer.DirectContent;
//创建一个新的模板,设置模板的大小
PdfTemplate template1 = cb.CreateTemplate(500, 500);
PdfTemplate template2 = cb.CreateTemplate(500, 500);
//读取图片 add by sara
for (int j = 0; j < imageList.Length; j++)
{
if (j == 0)
{
mm = iTextSharp.text.Image.GetInstance(imageList[0]);
//设置图片的位置
//mm.SetAbsolutePosition(50, -10);
mm.SetAbsolutePosition(ImgX1, ImgY1);
//设置图片的大小
//mm.ScaleAbsolute(60, 60);
mm.ScaleAbsolute(ImgWidth1, ImgHeigth1);
template1.AddImage(mm);
}
else
{
mm = iTextSharp.text.Image.GetInstance(imageList[1]);
//设置图片的位置
//mm.SetAbsolutePosition(100, -10);
mm.SetAbsolutePosition(ImgX2, ImgY2);
//设置图片的大小
//mm.ScaleAbsolute(60, 60);
mm.ScaleAbsolute(ImgWidth2, ImgHeigth2);
template2.AddImage(mm);
}
}
//设置添加模板的位置
//cb1.AddTemplate(template1, 90, 50);
cb1.AddTemplate(template1, TempX1, TempY1);
//cb1.AddTemplate(template2, 410, 50);
cb1.AddTemplate(template2, TempX2, TempY2);
}
}
document.Close();
reader.Close();
}
}
}