博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iText操作PDF
阅读量:7222 次
发布时间:2019-06-29

本文共 2784 字,大约阅读时间需要 9 分钟。

using (FileStream fs = new FileStream(tempPdfFilePath, FileMode.Create))

{

Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
writer.InitialLeading = 20;
document.Open();

//A4纸张分成两列打印

PdfPTable table = new PdfPTable(2);

table.TotalWidth = 575f;
table.LockedWidth = true;

//两列的比例

float[] widths = new float[] { 1.0f, 1.0f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;

 

var cell=CreateCell("测试内容", 2, ITextSharpHelper.GetChineseFont(8, FontStyle.NORMAL, FontFamily.宋体, FontColor.BLACK), 0, 13);

table.AddCell(cell);

document.Add(table);

document.Close();
fs.Close();
}

 

private PdfPCell CreateCell(string context, int colSpan, iTextSharp.text.Font font, int horizontalAlignment = 0, int minimumHeight = 9)

{
Phrase phrase = new Phrase();
//®
if (context.IndexOf("®") != -1)
{
phrase.Font = font;
Chunk c1 = new Chunk(context.Substring(0, context.IndexOf("®")), font);
c1.SetUnderline(0.6f, -3f);

Chunk subscript = new Chunk("®", font);

subscript.SetTextRise(2f);
subscript.SetUnderline(0.6f, -3f);

Chunk c2 = new Chunk(context.Substring(context.IndexOf("®") + 1, context.Length - context.IndexOf("®") - 1), font);

c2.SetUnderline(0.6f, -3f);

phrase.Add(c1);

phrase.Add(subscript);
phrase.Add(c2);
}
else
{
phrase = new Phrase(context, font);
}
PdfPCell cell = new PdfPCell(phrase);
cell.Border = 0;
cell.Colspan = colSpan;
cell.PaddingRight = 0f;
cell.MinimumHeight = minimumHeight;
// cell.SetLeading( = 20.0f;
cell.HorizontalAlignment = horizontalAlignment;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
return cell;
}

//插入图片

private PdfPCell CreateImgCell( int colSpan, int rowSpan)

{
//照片

iTextSharp.text.Image image = null;

string photoPath ="图片路径";

if (File.Exists(photoPath))

{
try
{
image = iTextSharp.text.Image.GetInstance(photoPath);
}
catch (Exception ex)
{
ex._Log();
System.Drawing.Bitmap image1 = new System.Drawing.Bitmap(100, 100);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image1);
System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
g.DrawString("读取照片失败", new System.Drawing.Font("宋体", 10, System.Drawing.FontStyle.Regular), b, 2, 30);
image = Image.GetInstance(image1, BaseColor.WHITE);
}
}
else
{
string noPhoto = Path.Combine(InitInfo.Config_WebRunPath, "Images\\NoPhoto.jpg");
image = iTextSharp.text.Image.GetInstance(noPhoto);
}
image.ScaleAbsolute(50, 60);
image.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
image.IndentationLeft = 9f;
PdfPCell cellKsPhoto = new PdfPCell(image);
cellKsPhoto.Padding = 1f;
cellKsPhoto.Rowspan = rowSpan;
cellKsPhoto.Colspan = colSpan;
cellKsPhoto.BorderWidth = 0;
cellKsPhoto.HorizontalAlignment = Element.ALIGN_CENTER;
return cellKsPhoto;
}

 

转载于:https://www.cnblogs.com/hobby0524/p/6734179.html

你可能感兴趣的文章
Android交互
查看>>
提醒我喝水chrome插件开发指南
查看>>
列表数据转树形数据
查看>>
eclipse的离线汉化
查看>>
Java新版本的开发已正式进入轨道,版本号18.3
查看>>
从零开始的webpack生活-0x009:FilesLoader装载文件
查看>>
在electron中实现跨域请求,无需更改服务器端设置
查看>>
gitlab-ci配置详解(一)
查看>>
centos安装java运行环境jdk+tomcat
查看>>
《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
查看>>
分享自己折腾多时的一套 vue 组件 --we-vue
查看>>
使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
查看>>
Akka系列(七):Actor持久化之Akka persistence
查看>>
创建一个Struts2项目maven 方式
查看>>
数据库写操作弃用“SELECT ... FOR UPDATE”解决方案
查看>>
纯 javascript 半自动式下滑一定高度,导航栏固定
查看>>
「前端」从UglifyJSPlugin强制开启css压缩探究webpack插件运行机制
查看>>
Rancher-k8s加速安装文档
查看>>
create-react-app项目添加less配置
查看>>
ucore操作系统实验笔记 - 重新理解中断
查看>>