62,243
社区成员




/// <summary>
/// 把图片转换成PDF
/// </summary>
/// <param name="pathName">上传的图片路径和文件名</param>
/// <returns></returns>
public static string ImageToPDF(List<string> listPath)
{
iTextSharp.text.Document document = new iTextSharp.text.Document();
string newFileName = RandomNum(listPath[0]);
string tagrt = GetFolder() + GetNewFileName(newFileName) + ".pdf";
if (tagrt == null)
{
return null;
}
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(tagrt, FileMode.CreateNew));
document.Open();
iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font Titlefont = new iTextSharp.text.Font(bfChinese, 16, iTextSharp.text.Font.BOLD, new iTextSharp.text.BaseColor(0, 0, 0));
for (int i = 0; i < listPath.Count; i++)
{
document.Add(new iTextSharp.text.Paragraph("", Titlefont));
iTextSharp.text.Image je = iTextSharp.text.Image.GetInstance(listPath[i]);//path就是函数入参,即你要转成PDF的图片路径
je.ScalePercent(72f);
document.Add(je);
}
}
catch (Exception de)
{
document.Close();
return null;
}
document.Close();
return tagrt;
}