pdf文件转图片 C# 源码 图片转pdf源码

1.pdf转图片(需要代码和O2S.Components.PDFRender4NET.dll 请进以下网址:https://download.csdn.net/download/Fighting515/16590038)

 public enum Definition
    {
     	   One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 	10
    }
    
    /// <summary>
    /// 将PDF转换为图片的方法
    /// </summary>
    /// <param name="pdfInputPath">PDF文件路径</param>
    /// <param name="imageOutputPath">图片输出路径</param>
    /// <param name="imageName">生成图片的名字</param>
    /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
    /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
    /// <param name="imageFormat">设置所需图片格式</param>
    /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
    public string[] PdfToPng(string pdfInputPath, string imageOutputPath,
        string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat, Definition definition)
    {
        List<string> outFileList = new List<string>();

        PDFFile pdfFile = PDFFile.Open(pdfInputPath);
        if (!Directory.Exists(imageOutputPath))
        {
            Directory.CreateDirectory(imageOutputPath);
        }
        // validate pageNum
        if (startPageNum <= 0)
        {
            startPageNum = 1;
        }
        if (endPageNum > pdfFile.PageCount)
        {
            endPageNum = pdfFile.PageCount;
        }
        if (startPageNum > endPageNum)
        {
            int tempPageNum = startPageNum;
            startPageNum = endPageNum;
            endPageNum = startPageNum;
        }
       

        if (endPageNum == 1)
        {
            Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * (int)definition);
            pageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat);
            pageImage.Dispose();
            outFileList.Add(imageOutputPath + imageName + "." + imageFormat);
        }
        else
        {
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                pageImage.Save(imageOutputPath + imageName + i + "." + imageFormat, imageFormat);
                pageImage.Dispose();
                outFileList.Add(imageOutputPath + imageName + i + "." + imageFormat);
            }
        }
        pdfFile.Dispose();
        return outFileList.ToArray(); ;
    }

2.图片转pdf(源码地址:https://download.csdn.net/download/Fighting515/16590038)

void ConvertJPG2PDF(string jpgfile, string pdf, bool isRotation)
    {
        var document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
        using (var stream = new FileStream(pdf, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            PdfWriter.GetInstance(document, stream);
            document.Open();
            using (var imageStream = new FileStream(jpgfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var image = iTextSharp.text.Image.GetInstance(imageStream);
                if (isRotation)
                    image.RotationDegrees = 90;

                if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
                {
                    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                }
                else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
                {
                    image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
                }

                image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
                document.Add(image);
            }

            document.Close();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Un-Fighting

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值