itextsharp在ios中可用,亲测

(一)生成文档

        Document document = new Document(new Rectangle(, , , ), , , , );
//Document document = new Document(PageSize.A4.Rotate(), 0, 0, 50, 0);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
//创建摘要
document.AddTitle("");
document.AddAuthor("");
document.AddSubject("");
document.AddCreator("");     //添加pdf内容     document.Close();

(二)定义字体

        BaseFont heiBaseFont = BaseFont.CreateFont(Path.Combine(Application.streamingAssetsPath, "GBK.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(heiBaseFont, );
font.Color = new iTextSharp.text.Color(, , );

(三)定义段落并添加到document中

        Paragraph Pmodel = new Paragraph("段落内容", font);
Paragraph Pmodel_1 = new Paragraph("段落内容1", font);
document.Add(Pmodel_1 );
   document.Add(Pmodel );

可以定义段落的左右边距等如下

        Paragraph p = new Paragraph();
p.IndentationLeft = ;//左边距
p.IndentationRight = ;//右边距
p.SpacingBefore = 0.2f;//前边距
p.SpacingAfter = 0.2f;//后边距
p.Alignment = Element.ALIGN_LEFT;//靠左

段落可以通过add添加表格,图片以及文字元素。

(四)生成表格

1)定义表格

        Table table = new Table();//根据列数据创建表格
List<int> widths = new List<int>();
//widths.Add(200);
widths.Add();//定义每一列的宽度,但是如果不对数值进行lock则是相对值
widths.Add();
widths.Add();
widths.Add();
widths.Add();
table.SetWidths(widths.ToArray()); table.Cellpadding = ;//表格中内容padding
table.Cellspacing = ;

通过pdfpTable定义表格并锁定宽度

            PdfPTable pdfPTable = new PdfPTable(currentList.Count);
//pdfPTable.le
pdfPTable.TotalWidth = currentList.Count * unitWidth;
//PdfPTable pdfPTable = new PdfPTable(column);
//pdfPTable.TotalWidth = column * unitWidth;
pdfPTable.LockedWidth = true;
pdfPTable.HorizontalAlignment = Element.ALIGN_LEFT;

2)添加内容

每一个单元格为cell元素,table添加cell,pdfpTable添加pdftcell,要添加图片则只能用pdftcell

cell添加

            CellElem elem = list[i];
Cell cellElem = new Cell(new Phrase(""));
cellElem.HorizontalAlignment = Element.ALIGN_CENTER;
cellElem.VerticalAlignment = Element.ALIGN_MIDDLE;
cellElem.BackgroundColor = elemHead.backGroundColor; Paragraph text = new Paragraph(elem.texts[ii], font);
cellElem.AddElement(text); table.AddCell(cellElem);

pdfpCell添加

      PdfPTable pdfPTable = new PdfPTable(currentList.Count);
      PdfPCell pdfPCell = new PdfPCell(new Phrase(title, font));
      pdfPCell.Colspan = column;
      pdfPCell.HorizontalAlignment = Element.ALIGN_CENTER;
      pdfPTable.AddCell(pdfPCell);

table添加单元格是按行来添加,当一行添加满后自动从下一行添加,如果某一单元格是三行三列的,则可设置其Colspan和Rowspan均为三即可(即表示此单元个列和行均占用三列和三行),但是低版本的pdfpCell无Rowspan,所以简易采用高版本

head.Rowspan = userTable.numRow;

3)添加表格到文档中

可以直接采用document.Add()直接将表格添加到文档中,也可以先把表格添加到段落Paragraph中,然后在将段落添加到document中,采用其一种方式无法设置表格的左右边距,后一种方法可以通过段落实现左右边距

(五)添加图片

                var image = Image.GetInstance(new FileStream(imageElem.imageBytes,FileMode.Open,FileAccess.Read));//可以直接添加bytes
image.ScalePercent();
image.Alignment = Element.ALIGN_CENTER;

图片可以直接添加到document中也可以添加到表格中,在通过表格添加到文档中。但是只能pdfpTable中可以添加图片。table不可以

                PdfPCell pdfPCellImage = new PdfPCell(image);
pdfPCellImage.PaddingTop = ;
pdfPCellImage.PaddingBottom = ;
pdfPCellImage.HorizontalAlignment = ;
pdfPTable.AddCell(pdfPCellImage);

由于用的itextsharp插件版本较低,在把Image插入pdfpCell时,报空指针异常,原因为在new PdfPCell(image)时相当于又进行了一次Image.GetInstance()操作,ios对此有问题,所以只能把图片插入到document文档中,所以排版有问题。默认一个图片一个段落,如果强制修改其absolutePostion则会跟问题表格覆盖,所以处理比较复杂。从好处理角度进行分页

(五)添加表单

对于可编辑的pdf文件(即点击可编辑的文字时,可以像inputfield那样更改),是由专业版本的pdf编辑器,通过添加form形成的,通过itextsharp也可以制作相关pdf,但是最简单的方法就是先编辑一个模板,通过关键字来更改模板的文字,缺点是不够灵活。

生成图片时,要把背景设置为无,否则不显示图片

    public static void CreateFormPdf(string fileName)
{
//string str = Path.Combine(Application.streamingAssetsPath, "Test.pdf");
if (File.Exists(fileName)) File.Delete(fileName);
FileStream stream = new FileStream(fileName, FileMode.Create);
//PdfReader reader = new PdfReader(Path.Combine(Application.streamingAssetsPath, "LindePDF1025.pdf"));
PdfReader reader = new PdfReader(Path.Combine(Application.streamingAssetsPath, "xxxxx.pdf"));//xxxxx.pdf为pdf模板
PdfStamper stamper = new PdfStamper(reader, stream);
AcroFields af = stamper.AcroFields;
af.SetField("Name", "WangWang");
af.SetField("Date", "2012-11-11");
//PushbuttonField addedButton = new PushbuttonField(stamper.Writer,new Rectangle(100,100,100,100), "NewAddBtn"); //stamper.AddAnnotation(addedButton.Field, 1); string imagepath = Path.Combine(Application.streamingAssetsPath, "Example.png");
if (!string.IsNullOrEmpty(imagepath) && File.Exists(imagepath))
{
//var image = Image.GetInstance(new FileStream(imagepath, FileMode.Open, FileAccess.Read));
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
if (image != null)
{
//PushbuttonField bf = af.GetNewPushbuttonFromField("Header");
//bf.Image = image; //af.ReplacePushbuttonField("Header", bf.Field); //af.RemoveField("Picture01"); //PushbuttonField bf1 = af.GetNewPushbuttonFromField("Picture02");
//bf1.Image = image;
//af.ReplacePushbuttonField("Picture02", bf1.Field); PushbuttonField bf2 = af.GetNewPushbuttonFromField("Picture02");
bf2.Image = image;
af.ReplacePushbuttonField("Picture02", bf2.Field); //PushbuttonField addedButton = new PushbuttonField(stamper.Writer, new Rectangle(100, 100, 100, 100), "NewAddBtn");
//addedButton.Image = image;
//addedButton.Layout = PushbuttonField.LAYOUT_ICON_ONLY; //stamper.AddAnnotation(addedButton.Field, 0);
}
} //stamper.FormFlattening=true; 是否将 pdf Form 转为 pdf文档
stamper.Close(); }

在生成的pdf文档中,默认打开时看不到文字,只有点击输入时才会看到,此时是由于缺少字体支持原因,添加如下代码即可实现字体支持

FontFactory.Register(Path.Combine(Application.streamingAssetsPath, "GBK.TTF"));
BaseFont heiBaseFont = BaseFont.CreateFont(Path.Combine(Application.streamingAssetsPath, "GBK.TTF"), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
AcroFields af = stamper.AcroFields;
af.AddSubstitutionFont (heiBaseFont);

itextsharp生成pdf的更多相关文章

  1. itextsharp生成pdf后的直接打印问题

    原文 itextsharp生成pdf后的直接打印问题 小弟这两天用itextsharp生成pdf文档,生成的pdf可以直接保存在指定路径的文件夹下,可是user不想保存,想要点一下button,就可以 ...

  2. iTextSharp生成PDF文件

    这是一篇简单的教程,所以只涉及一些iTextSharp生成pdf的简单应用,详细教程请搜索iTextSharp进入官网看官方文档(英文版). iTextSharp官方文档:https://itextp ...

  3. iTextSharp生成pdf的一个简单例子

    效果图: 参考:http://www.cnblogs.com/CareySon/archive/2011/11/09/2243496.html http://www.cnblogs.com/julyl ...

  4. C#使用itextsharp生成PDF文件

    项目需求需要生成一个PDF文档,使用的是VS2010,ASP.NET. 网络上多次搜索没有自己想要的,于是硬着头皮到itextpdf官网看英文文档,按时完成任务,以实用为主,共享一下: 使用HTML文 ...

  5. iTextSharp生成pdf含模板(二)---C#代码部分

    参考地址:https://www.cnblogs.com/ibeisha/p/itextsharp-pdf.html 一.先在程序中使用Nuget安装iTextSharp(我是创建的控制台程序) 二. ...

  6. iTextSharp生成pdf文档案例

    1.using iTextSharp.text;using iTextSharp.text.pdf; 2.设置页面大小 iTextSharp.text.Rectangle pageSize = new ...

  7. 利用ItextSharp 生成PDF文档改进版

    导入的ItextSharp.dll一定要是较高的版本 数据库表结构 生成的PDF样式 代码: namespace WebPDF { public partial class _Default : Sy ...

  8. .Net iTextSharp 生成pdf

    拿别人例子 public ActionResult index() { var ms = new MemoryStream(); #region CreatePDF Document document ...

  9. C# iTextSharp 生成 PDF

    使用iTextSharp在Asp.Net中操作PDF系列文章 目录 http://www.cnblogs.com/CareySon/category/332146.html 实战 iTextSharp ...

随机推荐

  1. Centos7搭建Scrapy爬虫环境

    写在前面 因为之前的爬虫环境一直是部署在我自己本地的电脑上的,最近,写了一个监控别人空间的爬虫,需要一直线上24小时运行,所有就打算云服务器上部署环境,也捣鼓了好一会才弄好,还是有一些坑,这里先记录一 ...

  2. 基于操作系统原理的Linux的内存管理

    一.实验目的 1.理解虚拟内存.磁盘缓存的概念. 2.掌握基本的内存管理知识. 3.掌握查看实时查看内存.内存回收的方法 二.实验内容 1. 监控内存使用情况 2. 检查和回收内容 三.实验平台 1. ...

  3. Sentinel Cluster流程分析

     前面介绍了sentinel-core的流程,提到在进行流控判断时,会判断当前是本地限流,还是集群限流,若是集群模式,则会走另一个分支,这节便对集群模式做分析. 一.基本概念  namespace:限 ...

  4. Kotlin学习系列(二)

    IF表达式 if在kotlin可以当做表达式使用跟java的三元操作符类似: var max = if( a > b ) a else b if分支可以使用代码块,最后一个表达式是返回值: va ...

  5. 给定一个公式字符串用java进行拆解并计算结果

    需求很简单,给定一个字符串形式的公式规则,用java代码进行拆解,并能计算出结果. ♦考虑字符串中数字格式[整数.小数点] ♦考虑字符串中运算符[+-*/()] ♦考虑空格.运算规则[被0除] 以下是 ...

  6. .Net Core 商城微服务项目系列(三):Ocelot网关接入Grafana监控

    使用网关之后我们面临的一个问题就是监控,我们需要知道网关的实时状态,比如当前的请求吞吐量.请求耗费的时间.请求峰值甚至需要知道具体哪个服务的哪个方法花费了多少时间.网关作为请求的中转点是监控品牌的要塞 ...

  7. jQuery返回顶部和在线客服网站侧边栏

    效果图: 全部代码: <!DOCTYPE html> <html> <head> <title></title> <style typ ...

  8. CSS技巧 (2) · 多列等高布局

    前言  最近,面试的时候都碰到一些关于利用CSS实现多列等高布局或者一侧宽度固定,另一侧宽度自适应的问题,下面稍微总结一下: 先看一道题目 巧妙的多列等高布局 规定下面的布局,实现多列等高布局,要求两 ...

  9. Java 爬虫遇到需要登录的网站,该怎么办?

    这是 Java 网络爬虫系列博文的第二篇,在上一篇 Java 网络爬虫,就是这么的简单 中,我们简单的学习了一下如何利用 Java 进行网络爬虫.在这一篇中我们将简单的聊一聊在网络爬虫时,遇到需要登录 ...

  10. [WP8.1]RSA 使用BouncyCastle 公钥解密

    写应用的时候遇到个服务器返回私钥加密过的数据 ,然后要在客户端用公钥解密的需求 ,一直没找到方法,应用搁置了一个学期,多方搜索,结论就是.net没有实现公钥解密的方法,要自己实现,于是硬着头皮开始看  ...