c#向pdf插入图片,使用iTextSharp【实测成功】


仅作为操作记录,大佬请跳过。

背景

博主查了很长时间的资料,终于把一张本地图片插入到了pdf文档里。一直陷在了图片的路径问题上。

解决方案

仅需三行代码:
(与using System.Web;System.Web.HttpContext.Current.Server.MapPath无关,因为博主插入的时本地的图片)

string imagepath = @"E:\大四上\现代医学仪器设计\READ\software\visualcsharp";
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath + "/pulse.png");
document.Add(image);

完整代码分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web;

namespace a2
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("E:\\大四上\\现代医学仪器设计\\READ\\software\\visualcsharp\\helloworld21.pdf", FileMode.Create));
            document.Open();
            document.Add(new Paragraph("hello world"));
            document.AddTitle("c#生成pdf");
            document.AddSubject("c#生成一个简易的pdf文档的实例");
            document.AddKeywords("关键字");
            document.AddCreator("visual studio 2019");
            document.AddAuthor("captain飞虎大队");

			//添加图片
			string imagepath = @"E:\大四上\现代医学仪器设计\READ\software\visualcsharp";
            iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath + "/XXX.png");
            document.Add(image);



            document.Close();


        }


    }
}            

界面展示
在这里插入图片描述

前言
如想用c#生成一个简易的pdf文档的实例,可参考博主的博文——c#生成一个简易的pdf文档 【实测成功】


补充——对图片大小进行修改,以适应pdf页面的大小

如果想对图片大小进行修改,以适应pdf页面的大小 的时候,可参考优秀博主的文章——传送门

在这里插入图片描述
源代码更新如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web;

namespace a2
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("E:\\大四上\\现代医学仪器设计\\READ\\software\\visualcsharp\\helloworld22.pdf", FileMode.Create));
            document.Open();
            document.Add(new Paragraph("hello world"));
            document.AddTitle("c#生成pdf");
            document.AddSubject("c#生成一个简易的pdf文档的实例");
            document.AddKeywords("关键字");
            document.AddCreator("visual studio 2019");
            document.AddAuthor("captain飞虎大队");

            document.NewPage();
            string imagepath = @"E:\大四上\现代医学仪器设计\READ\software\visualcsharp";
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagepath + "/XXX.png");


            float percentage = 1;
            //这里都是图片最原始的宽度与高度  
            float resizedWidht = img.Width;
            float resizedHeight = img.Height;

            //这时判断图片宽度是否大于页面宽度减去也边距,如果是,那么缩小,如果还大,继续缩小,  
            //这样这个缩小的百分比percentage会越来越小  
            while (resizedWidht > (document.PageSize.Width - document.LeftMargin - document.RightMargin) * 0.8)
            {
                percentage = percentage * 0.9f;
                resizedHeight = img.Height * percentage;
                resizedWidht = img.Width * percentage;
            }

            while (resizedHeight > (document.PageSize.Height - document.TopMargin - document.BottomMargin) * 0.8)
            {
                percentage = percentage * 0.9f;
                resizedHeight = img.Height * percentage;
                resizedWidht = img.Width * percentage;
            }

            //这里用计算出来的百分比来缩小图片  
            img.ScalePercent(percentage * 100);
            //图片定位,页面总宽283,高416;这里设置0,0的话就是页面的左下角 让图片的中心点与页面的中心店进行重合  
            img.SetAbsolutePosition(document.PageSize.Width / 2 - resizedWidht / 2, document.PageSize.Height / 2 - resizedHeight / 2);
            document.Add(img);
            //*****************************************************************************************************************************************

            //document.Add(image);



            document.Close();


        }


    }
}            
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值