如何将图片、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

转载于:https://www.cnblogs.com/zhujiantao/p/6567428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值