首页 > 编程语言 >C# JPG转PDF

C# JPG转PDF

时间:2023-02-15 17:24:26浏览次数:42  
标签:PageSize C# text image JPG iTextSharp new PDF document

 需先引用 itextsharp.dll
//方法一 /// <summary> /// JPG转PDF /// </summary> /// <param name="jpgfile">图片路径</param> /// <param name="pdf">生成的PDF路径</param> /// <param name="pageSize">A4,A5</param> /// <param name="Vertical">True:纵向,False横向</param> public void ConvertJPG2PDF(string jpgfile, string pdf, string pageSize, bool Vertical = true) { float width = 0, height = 0; Document document; #region 根据纸张大小,纵横向,设置画布长宽 if (pageSize.ToUpper() == "A4") { if (Vertical)//纵向 { width = iTextSharp.text.PageSize.A4.Width; height = iTextSharp.text.PageSize.A4.Height; } else//横向 { width = iTextSharp.text.PageSize.A4.Height; height = iTextSharp.text.PageSize.A4.Width; } } else if (pageSize.ToUpper() == "A5") { if (Vertical) { width = iTextSharp.text.PageSize.A5.Width; height = iTextSharp.text.PageSize.A5.Height; } else { width = iTextSharp.text.PageSize.A5.Height; height = iTextSharp.text.PageSize.A5.Width; } } iTextSharp.text.Rectangle pageSizeNew = new iTextSharp.text.Rectangle(width, height); document = new Document(pageSizeNew); #endregion 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); //缩放图像比例 image.ScaleToFit(width, height); image.SetAbsolutePosition(0, 0); image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE; document.Add(image); } document.Close(); } }
//方法二
/// <summary>
        /// jpg转PDF
        /// </summary>
        /// <param name="pdfPath">pdf存放路径</param>
        /// <param name="jpegPath">jpg路径</param>
        /// <returns></returns>
        private bool jpegTopdf(string pdfPath, string jpegPath)
        {
            try
            {
                System.Drawing.Image B = System.Drawing.Image.FromFile(jpegPath);
                System.Drawing.Bitmap image = new System.Drawing.Bitmap(B);
                B.Dispose();

                Document document = new Document();
                //document.SetPageSize(new iTextSharp.text.Rectangle(image.Width + 72f, image.Height + 72f));
                document.SetPageSize(new iTextSharp.text.Rectangle(image.Width, image.Height));
                PdfWriter write = PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.OpenOrCreate, FileAccess.Write));
                document.Open();
                iTextSharp.text.Image jpg;

                jpg = iTextSharp.text.Image.GetInstance(image, ImageFormat.Jpeg);
                document.NewPage();
                document.Add(jpg);

                if (document != null && document.IsOpen())
                {
                    document.Close();
                }
                if (write != null)
                {
                    write.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

  

标签:PageSize,C#,text,image,JPG,iTextSharp,new,PDF,document
From: https://www.cnblogs.com/lydj/p/17123861.html

相关文章

  • 【git】解决git clone时fatal: unable to access ‘https://gitee.com/XXX.git/‘: Th
    一、前言我的电脑一直录入的是我的gitee账号,平常和同事协作开发,gitpull、gitclone等git操作都没有报错过。但是,今天要gitclone另一个gitee账号的项目代码,出现报错403(如......
  • 免交互与Expect基本命令
    一、HereDocument免交互概念1.定义HereDocument是标准输入的一种替代品,可以帮助脚本开发人员不必使用临时文件来构建输入信息,而是直接就地生产出一个文件并用作命令......
  • <select>中<option>的互斥
    背景:我需要将单选和多选功能组合到一个控件中。具体来说,我有很多选项<option>。第一个选项是相互排斥的。因此,如果我选择第一个独占选项,则需要取消选中所有其他选项。如果......
  • C# 删除文件夹
    ///<summary>///删除文件夹///</summary>///<paramname="strPath">文件夹路径</param>///<returns></returns>publ......
  • 怎样用cmd命令给电脑锁屏
    当进行远程控制操作电脑要锁屏被控制电脑但是不锁屏本地电脑时可以通过cmd命令执行锁屏后可以再次远程连进去,但是休眠了就没法再直接远程连进去了锁屏rundll32.exeuser......
  • 华为云发布分布式编译构建系统CodeArts Build
    摘要:2月14日,华为云发布分布式编译构建系统CodeArtsBuild,旨在支撑企业实现高效的软件开发,缩短产品上市周期,帮助企业的软件产品快速形成关键竞争力。本文分享自华为云社区......
  • fastai在Windows10多线程处理受限提示 “Due to IPython and Windows limitation, pyt
    fastai在Win10直接使用Jupyternotebook,会有这个限制提示dls=TextDataLoaders.from_folder(untar_data(URLs.IMDB),valid='test')“DuetoIPythonandWindowslimit......
  • 【算法训练营day43】LeetCode1049. 最后一块石头的重量II LeetCode494. 目标和 LeetCo
    LeetCode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II独上高楼,望尽天涯路一开始还是没有想到怎么转化成01背包问题,所以直接看题解找思路慕然回首,灯......
  • VScode、PHP运行
    下载vscode,并安装PHP插件打开小皮面板创建一个新的网站输入域名,选择根目录并点击确定创建成功浏览器输入http://myblog验证在vscode打开新建的myblog文件夹新建一个index.p......
  • 创建myblog程序、VScode中编辑程序,运行
    先打开vscode,点击扩展,在搜索框中搜索php,下载前三个(也可以只下载一个)然后打开小皮面板,点击网站,创建网站,输入域名和改根目录(如果你能找到根目录的话,可改可不改),点击确认,就......