zoukankan      html  css  js  c++  java
  • 生成PDF文档之iText

    • iTextSharp.text.Document:这是iText库中最常用的类,它代表了一个pdf实例。如果你需要从零开始生成一个PDF文件,你需要使用这个Document类。首先创建(new)该实例,然后打开(open)它,并添加(add)内容,最后关闭(close)该实例,即可生成一个pdf文件。
    • iTextSharp.text.Paragraph:表示一个缩进的文本段落,在段落中,你可以设置对齐方式,缩进,段落前后间隔等。
    • iTextSharp.text.Chapter:表示PDF的一个章节,他通过一个Paragraph类型的标题和整形章数创建。
    • iTextSharp.text.Font:这个类包含了所有规范好的字体,包括family of font,大小,样式和颜色,所有这些字体都被声明为静态常量。
    • iTextSharp.text.List:表示一个列表;
    • iTextSharp.text.List:表示一个列表;
    • iTextSharp.text.Anchor:表示一个锚,类似于HTML页面的链接。
    • iTextSharp.text.pdf.PdfWriter:当这个PdfWriter被添加到PdfDocument后,所有添加到Document的内容将会写入到与文件或网络关联的输出流中。
    • iTextSharp.text.pdf.PdfReader:用于读取PDF文件;

    image

    给PDF文件设置文件属性

                    //设置属性
                    //标题
                    document.AddTitle("this is a title");
                    //作者
                    document.AddAuthor("H__D");
                    //主题
                    document.AddSubject("this is subject");
                    //关键字
                    document.AddKeywords("Keywords");
                    //创建时间
                    document.AddCreationDate();
                    //应用程序
                    document.AddCreator("hd.com");

    PDF中添加图片

                    //输出图片到PDF文件
                    iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance("D:\work\YIQI\jiaju.jpg");
                    jpeg01.Alignment = Element.ALIGN_CENTER;
                    //设置边框
                    jpeg01.Border = iTextSharp.text.Image.BOX;
                    jpeg01.BorderWidth = 2;
                    jpeg01.BorderColor = BaseColor.RED;
                    //设置图片大小
                    /* ScaleToFit有两个参数,指的是长和宽的最大值,但是图片的长宽比还是不会变的
                     * ScaleAbsoluteHeight设置图片的高度,不管长宽比
                     * ScaleAbsoluteWidth设置图片的宽度,不管长宽比
                     * ScalePercent等比例缩放
                     */
                    // image.ScaleToFit(1000, 1000);
                    // image.ScaleAbsoluteHeight(100f);
                    // image.ScaleAbsoluteWidth(100f);
    
                    //设置图片位置的x轴和y周
                    //jpeg01.SetAbsolutePosition(100f, 550f);
                    //设置图片的宽度和高度
                    jpeg01.ScaleAbsolute(200, 200);
                    document.Add(jpeg01);

    输出段落文本

    //按设置的字体输出文本
                    var paragraph = new Paragraph(@"安装Adobe Acrobat 8 Pro,本人安装的是8.1.2版本,在你的安装目录下
    (例如我自己的:C:Program FilesAdobeAcrobat 8.0PDFMakerCommon)common目录中找到PDFMakerAPI.dll程序集,
    拷贝出到项目中放DLL的文件夹(此文件夹为用户保存DLL文件的文件夹,名称以自己项目为准),并在项目里对其添加引用。!!!", FontChinese);
                    paragraph.IndentationLeft = 30f;
                    paragraph.IndentationRight = 30f;
                    paragraph.Alignment = Element.ALIGN_LEFT;
                    paragraph.FirstLineIndent = 30f;
                    document.Add(paragraph);

    添加anchor

                    //设置一个坐标点,用于goTop定位
                    Anchor topLine = new iTextSharp.text.Anchor("This name is US");
                    topLine.Name = "US";
                    document.Add(topLine);
    
                    document.Add(Chunk.NEWLINE);
                    //链接到百度
                    Anchor anchor = new iTextSharp.text.Anchor("This is Anchor");
                    anchor.Reference = "http://www.baidu.com";
                    document.Add(anchor);

    添加表格

                    PdfPTable table = new PdfPTable(3);
    
                    for (int i = 0; i < 6; i++)
                    {   
                         table.AddCell(new Phrase("gogogo测试", FontBlue));
    
                    }
                    document.Add(table);

    添加List

                    List orderedList = new List(List.ORDERED);
                    orderedList.Add(new ListItem("Item one"));
                    orderedList.Add(new ListItem("Item two"));
                    orderedList.Add(new ListItem("Item three"));
                    document.Add(orderedList);

    生成的效果:

  • 相关阅读:
    通过Spring使用远程访问和web服务
    mongoDB id 导出,dump,sed,count,mysql import等用法示例
    Spring属性占位符 PropertyPlaceholderConfigurer
    关于Spring管理的类如何创建对象
    spring中反射机制和注入的使用
    Spring 反射注入+全注解注入
    Spring 注解@Component,@Service,@Controller,@Repository
    @Transactional spring 配置事务 注意事项
    Spring 注解Autowired自动注入bean异常解决
    Spring事务的传播行为 @Transactional
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/10133066.html
Copyright © 2011-2022 走看看