如何将图片、html等格式转成pdf

        const int WWidth = 600;
        const int HHeight = 800;
        List<System.Drawing.Image> AllName = new List<System.Drawing.Image>();
        string FileName = "d://result.pdf";
        protected void Page_Load(object sender, EventArgs e)
        {
            //固定高宽,便于后续处理
            List<string> SourceImage = new List<string>();
            //SourceImage.Add("img/1.jpg");
            SourceImage.Add(@"C:\img\1.jpg");
            
            // SourceImage[0] = "img/1.jpg";
            TurnTheImageToPdf(ref SourceImage);
        }
        public void TurnTheImageToPdf(ref List<string> SourceImage)
        {
            ChangeTheImageToS(ref SourceImage);
            Document document = new Document();
            document.SetPageSize(new iTextSharp.text.Rectangle(WWidth + 72f, HHeight + 72f));
            PdfWriter write = PdfWriter.GetInstance(document, new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write));
            document.Open();
            iTextSharp.text.Image jpg;

            for (int i = 0; i < AllName.Count; ++i)
            {
                jpg = iTextSharp.text.Image.GetInstance(AllName[i],ImageFormat.Jpeg);
                document.NewPage();
                document.Add(jpg);
            }
            if (document != null && document.IsOpen())
            {
                document.Close();
            }
            if (write != null)
            {
                write.Close();
            }
        }
        private void ChangeTheImageToS(ref List<string> ImageName)
        {
            for (int i = 0; i < ImageName.Count; ++i)
            {
                Bitmap src = new Bitmap(ImageName[i]);
                Bitmap bmImage = new Bitmap(WWidth, HHeight);
                Graphics g = Graphics.FromImage(bmImage);
                g.InterpolationMode = InterpolationMode.Low;
                g.DrawImage(src, new System.Drawing.Rectangle(0, 0, bmImage.Width, bmImage.Height), new System.Drawing.Rectangle(0, 0, src.Width, src.Height), GraphicsUnit.Pixel);
                g.Dispose();
                AllName.Add(bmImage);
            }
        }

下载地址:http://download.csdn.net/download/happy09li/6468669

posted @ 2017-03-17 17:19  我是菜.鸟  阅读(1598)  评论(0编辑  收藏  举报