C#将图片转换成pdf

刚开始的时候以为不是很好做,后来网上搜了一下才发现有现成的dll,就是iTextsharp.dll, 主要是用来处理文档的。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace ReadingSystem
{
    class ImageToPdf
    {
        //固定高宽,便于后续处理
        const int WWidth = 600;
        const int HHeight = 800;
        List<System.Drawing.Image> AllName = new List<System.Drawing.Image>();
        string FileName = "C://result.pdf";
        //It is the construction function;
        public ImageToPdf()
        {
            //if (File.Exists(FileName))
            //{
            //    File.Delete(FileName);
            //}
        }
        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();
            }
        }
        //change all the image to standard
        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);
            }           
        }
    }
}
这个可以很好的实现将不同大小的图片一起放在一个pdf文档中。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值