仅作为操作记录,大佬请跳过。
背景
博主查了很长时间的资料,终于把一张本地图片插入到了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();
}
}
}