日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# JPG转PDF

發布時間:2023/12/9 C# 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# JPG转PDF 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需先引用 itextsharp.dll

//方法一 /// <summary>/// JPG轉PDF/// </summary>/// <param name="jpgfile">圖片路徑</param>/// <param name="pdf">生成的PDF路徑</param>/// <param name="pageSize">A4,A5</param>/// <param name="Vertical">T:縱向,F橫向</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;}}

總結

以上是生活随笔為你收集整理的C# JPG转PDF的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。