一:url抓取图片存入本地 c#
二:将图片和文字存入PDF c#
给大家提供程序下载地址:https://download.csdn.net/download/us2019/12888680
将url网页上获取的图片存入到项目的根目录下,然后将本地的图片存入到pdf中,会在bin/debug目录下生成pdf。
一:url抓取图片存入本地 c#
创建一个字典批量存储一下:
private void button1_Click(object sender, EventArgs e)
{
Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
dic.Add("http://www.shuoshuoxq.com/uploads/images/2020/20202.jpg", new List<string>() { "", "", "第一张图 ", "识别时间2020-09-09 09:00:12", "经度120.68328086θ", "维度 31.31032642", " 事件分析,浓度中等", " 打印工单:是" });
dic.Add("http://www.shuoshuoxq.com/uploads/images/2020/20273.jpg", new List<string>() { "", "", "第二张图 ", "识别时间2020-10-09 09:00:12", "经度120.68328086θ", "维度 31.31032642", " 事件分析,浓度中等", " 打印工单:是" });
dic.Add("http://www.shuoshuoxq.com/uploads/images/2020/20132.jpg", new List<string>() { "", "", "第三张图 ", "识别时间2020-11-09 09:00:12", "经度120.68328086θ", "维度 31.31032642", " 事件分析,浓度中等", " 打印工单:是" });
dic.Add("http://www.shuoshuoxq.com/uploads/images/2020/20186.jpg", new List<string>() { "", "", "第四张图 ", "识别时间2020-12-09 09:00:12", "经度120.68328086θ", "维度 31.31032642", " 事件分析,浓度中等", " 打印工单:是" });
bool b = InsertIntoPDF(dic);
if (b)
MessageBox.Show("ok");
else
MessageBox.Show("false");
}
调用此方法会在bin/debug目录下生成pdf。
//调用此方法会在bin/debug目录下生成pdf。
public static bool InsertIntoPDF(Dictionary<string, List<string>> dic)
{
bool f = true;
int ns = 0;
try
{
//这个循环即可存入图片到本地了
foreach (var item in dic)
{
string strImageURL = item.Key;
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(strImageURL, "" + ns++ + ".jpg");
}
//下面是生产pdf存入图片和文字
PdfDocument doc = new PdfDocument();
PdfImage im_ = PdfImage.FromFile(0.ToString() + ".jpg");
float width_ = im_.Width;
float height_ = im_.Height;
PdfPageBase title = doc.Pages.Add(new SizeF(width_ + 110, 30), new PdfMargins(140, 0, 0, 0));
PdfTrueTypeFont font_ = new PdfTrueTypeFont(new Font("楷体", 14f), true);
PdfPen pen_ = new PdfPen(Color.Black);
title.Canvas.DrawString("这里是标题(中国扫黄影视资料备份组)", font_, pen_, 15, 13);
int i = 0;
foreach (var item in dic)
{
if (i < dic.Count)
{
PdfImage im = PdfImage.FromFile(i.ToString() + ".jpg");
float width = im.Width;
float height = im.Height;
PdfPageBase page = doc.Pages.Add(new SizeF(width+110, height), new PdfMargins(0, 0, 0, 0));
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("楷体", 11f), true);
PdfPen pen = new PdfPen(Color.Black);
List<string> list = item.Value;
string strBu = "";
foreach (var str in item.Value)
{
strBu += str + "\r\n";
}
//page.Canvas.DrawString(strBu, font, pen, 15, 13);
page.Canvas.DrawString(strBu, font, pen, 410, 0);
//page.Canvas.DrawImage(im, 0, 0, width, height);
page.Canvas.DrawImage(im, 0, 0, 400, 350);
//PdfPageBase page3 = doc.Pages.Add(new SizeF(width, height), new PdfMargins(0, 0, 0, 0));
//PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("楷体", 11f), true);
//PdfPen pen = new PdfPen(Color.Black);
//List<string> list= item.Value;
//string strBu = "";
//foreach (var str in item.Value)
//{
// strBu += str+"\r\n";
//}
//page3.Canvas.DrawString(strBu, font, pen, 15, 13);
i++;
}
}
#region 备份
//下面是单独生成一个
//PdfImage im2 = PdfImage.FromFile("021.jpg");
//float width2 = im2.Width;
//float height2 = im2.Height;
//PdfPageBase page2 = doc.Pages.Add(new SizeF(width2, height2), new PdfMargins(0, 0, 0, 0));
//page2.Canvas.DrawImage(im2, 0, 0, width2, height2);
自定义PdfTrueTypeFont、PdfPen实例,设置字体类型、字号和字体颜色
//PdfPageBase page3 = doc.Pages.Add(new SizeF(width2, height2), new PdfMargins(0, 0, 0, 0));
//PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("楷体", 11f), true);
//PdfPen pen = new PdfPen(Color.Black);
//string text = ("《蝶恋花 送春》 \n 楼外垂杨千万缕,欲系青春,少住春还去。犹自风前飘柳絮,随春且看归何处?\n 绿满山川闻杜宇,便作无情,莫也愁人苦。把酒送春春不语,黄昏却下潇潇雨。");
//page3.Canvas.DrawString(text, font, pen, 15, 13);
#endregion
//Save pdf file.
string pdfName = DateTime.Now.ToString();
doc.SaveToFile(""+ pdfName + ".pdf");
doc.Close();
//UploadFile(doc, @"C:\bluealgea", "172.27.8.11", "dhail", "bluerw@Huawei123");
}
catch (Exception ex)
{
f = false;
}
return f;
}
调用此方法会在bin/debug目录下生成pdf。
启动程序:
给大家提供程序下载地址:https://download.csdn.net/download/us2019/12888680