好久没写博客了,今天给大家分享一个图片转PDF的相关操作,也算是一次总结吧。

  首先需要准备动态库itextsharp.dll,这个dll去网上下载,都可以下载到,C#对PDF的操作都是基于这个类库来实现的。话不多说,直接上代码。

        /// <summary>
        /// 导出PDF
        /// </summary>
        /// <param name="FilePath">图片的路劲</param>
		/// <param name="FileName">图片的名称</param>
        private bool PrintToPdf(string FilePath,string FileName )
        {			

            iTextSharp.text.Rectangle rec = new iTextSharp.text.Rectangle(0, 0, iTextSharp.text.PageSize.A4.Width, iTextSharp.text.PageSize.A4.Height);
            iTextSharp.text.Image image;
            iTextSharp.text.Document document = null;
            FileStream fs = null;
            PdfWriter writer = null;
            try
            {
                //生成PDF
                document = new iTextSharp.text.Document(rec);
                fs = new FileStream(FilePath + FileName + ".pdf", FileMode.Create);

                PdfWriter.GetInstance(document, fs);
                document.Open();
                image = iTextSharp.text.Image.GetInstance(FilePath + FileName + ".jpg");
                //调整图片大小,使之适合A4
                if (image.Height > rec.Height)
                {
                    image.ScaleToFit(rec.Width, rec.Height);
                }
                else if (image.Width > rec.Width)
                {
                    image.ScaleToFit(rec.Width, rec.Height);
                }

                //调整图片位置,使之居中
                image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                document.NewPage();
                document.Add(image);
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (document.IsOpen())
                {
                    document.Close();
                }
                fs.Close();

                //最后删除生成的图片
                File.Delete(FilePath + FileName + ".jpg");
            }

            try
            {
                //添加水印图片,水印图片采取的是先生成图片,再添加到PDF文件中
                document = new iTextSharp.text.Document(rec);
                fs = new FileStream(FilePath + FileName + ".pdf", FileMode.Open);
                writer = PdfWriter.GetInstance(document, fs);
                iTextSharp.text.Image imgWater = CreateWaterImage();
                PdfReader reader = new PdfReader(fs);
                document.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage newPage;
                int iPageNum = reader.NumberOfPages;
                for (int j = 1; j <= iPageNum; j++)
                {
                    document.NewPage();
                    newPage = writer.GetImportedPage(reader, j);

                    //pdf旋转
                    if (newPage.Width > newPage.Height)
                    {
                        cb.AddTemplate(newPage, 0, -1f, 1f, 0, 0, newPage.Width);
                    }
                    else
                    {
                        cb.AddTemplate(newPage, 0, 0);
                    }
                    cb.AddImage(imgWater);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
            finally
            {
                if (document.IsOpen())
                {
                    document.Close();
                }
                writer.Close();
                fs.Close();
            }
        }

        /// <summary>
        /// 生成水印图片
        /// </summary>
        /// <returns></returns>
        private iTextSharp.text.Image CreateWaterImage()
        {
            string WaterText = "水印文本";
            Bitmap bitmap = new Bitmap(595, 842);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                //重置图像
                g.ResetTransform();
                //设置旋转中心
                g.TranslateTransform(bitmap.Width / 2, bitmap.Height / 2);
                //旋转50度 顺时针
                g.RotateTransform(-50);
                //指定文本呈现的质量 解决文字锯齿问题
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 14);
                SolidBrush solidBrush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));

                //设置文字、字体、大小、颜色、起始位置
                g.DrawString(WaterText, font, solidBrush, new Point(-240, -100));

                g.DrawString(WaterText, font, solidBrush, new Point(-340, 100));
            }
			
			//把水印图片保存在系统指定位置
            bitmap.Save("C:\\WaterTemp.jpg");

            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance("C:\\WaterTemp.jpg");
            //透明度,灰色填充
            image.GrayFill = 20;
            image.SetAbsolutePosition(0, 0);

            return image;
        }
06-22 00:00