C# PDF插入图片并自动保存至相应的文件夹

对于创建PDF时,向PEF中插入图片,坐标比较不好控制,建议先在PDF里画表格,然后将图片放至对应的表格即可。

方法如下:

 1      private void OutPutPDF()
 2                                 {
 3                                     string imagePath = GR_Path; //文件路径
 4                                     string OutPath = @"D:\";
 5                                     string fileName = string.Empty;
 6                                     fileName = "Test";
 7                                     Document document = new Document(PageSize.A4, 10, 10, 40, 10); //左、右、上、下
 8                                     //不显示对话框的导出方式
 9                                     PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(OutPath + "\\" + fileName + ".pdf", FileMode.Create));//保存至目的地
10                                     document.Open();
11                                     PdfPTable table = new PdfPTable(1);
12                                     PdfPCell cell;
13                                     //Insert Image                              
14                                     cell = new PdfPCell(new Phrase());
15                                     cell.Rowspan = 1;
16                                     cell.FixedHeight = 150;
17                                     cell.Border = 0;
18                                     //PDF中插入图片
19                                     iTextSharp.text.Image imgCIE = iTextSharp.text.Image.GetInstance(imagePath + "\\" + "CIE" + ".png");
20                                     imgCIE.ScaleToFit(618f, 281f);
21                                     // Chunk ckCIE = new Chunk(imgCIE, 0, 0);
22                                     cell.AddElement(new Chunk(imgCIE, 0, 0));
23                                     cell.HorizontalAlignment = Element.ALIGN_CENTER;
24                                     cell.VerticalAlignment = Element.ALIGN_BOTTOM;
25                                     table.AddCell(cell);
26                                     document.Add(table);
27                                     document.Close();
28                                 }

 

posted @ 2016-08-24 16:54  Alisa_li  阅读(1683)  评论(0编辑  收藏  举报