C# 多張圖片生成PDF

 

//存放image路徑圖片

List<string> imageList = new List<string>();
imageList.Add(@"D:\image1.png");
imageList.Add(@"D:\image2.png");
//生成PDF路徑
string fileName = string.Empty;
fileName = @"D:\testpdf.pdf";
iTextSharp.text.Rectangle page = new iTextSharp.text.Rectangle(300f, 250f);
Document document = new Document(page,15f, 20f, 35f, 10f);
//設置紙張橫向
//document.SetPageSize(page.Rotate());
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
for (int i = 0; i < imageList.Count; i++)
{
      document.Open();
      iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imageList[i]);
      img.ScaleAbsolute(170f, 170f);string

      //圖片居中
      img.Alignment = iTextSharp.text.Image.MIDDLE_ALIGN;
      //圖片絕對定位
      img.SetAbsolutePosition(55, 30);it

      //圖片打印到PDF
      writer.DirectContent.AddImage(img);
      document.NewPage();
}io

document.Close();pdf

相關文章
相關標籤/搜索