public static void ConvertPicture2PDF(string jpgfile, string pdf)
{
try
{
var document = new Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
PdfWriter.GetInstance(document, stream);
document.Open();
using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var image = Image.GetInstance(imageStream);
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.Add(image);
}
document.Close();
}
}
catch (Exception e)
{
MessageBox.Show("上传文件不符合要求,请重新上传!");
}
}
/// <summary>
/// 合成pdf文件
/// </summary>
/// <param name="fileList">文件名list</param>
/// <param name="outMergeFile">输出路径</param>
public static void MergePDF(List<string> fileList, string outMergeFile)
{
PdfReader reader;
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
writer.CloseStream = true;
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();
}