zoukankan      html  css  js  c++  java
  • pdfContentByte 类 图像和文本的绝对位置

    在PDF中当涉及到布局问题时(不再是自动地添加文本、图片等等,对元素在页面上的位置有一定的要求),有时我们希望将一些图像或者文本放置在某页的指定位置,为实现该功能,我们将使用PdfContentByte类。

    PdfContent对象可以通过在使用Writer对象中使用getDirectContent()方法来得到该对象。例:

    PdfWriter writer= PdfWriter.GetInstance(document, new FileStream("D:\\hellowrold.pdf", FileMode.OpenOrCreate));

    PdfContentByte cb=writer.DirectContent;

    简单图形的绘制:

    我们可以使用诸如moveTo和lineTo方法移动到页面上当前位置然后画一条直线到其他位置。例如

    cb.LineWidth=10f;

    cb.moveTo(100,700);

    cb.lineTo(200,800);

    cb.stroke();

     模板(Form xObjects)

    在页眉和页脚的解决方案时,我们可以选择定义一小块添加到每一页的信息,实际上,该小块信息写到了文件的每一个新页上。这并不是最经济的解决方案,更好的方法是将该信息作为一个Form Xobject 仅在文档中添加一次,在其可见位置重复出现。

    1.创建一个PdfTemplate

    2.创建PdfTemplate的最好方法是调用pdfContentByte对象中的createTemplate方法:PdfTemplate template=cb.createTemplate(500,200);

    这样,该模板的宽度为500,高度为200。通过该模板我们可以做像PdfContentByte同样的事情

    template.moveTo(0,200);

    template.lineTo(500,0);

    template.stroke();

    添加一个文档到模板,通过下面一样在绝对位置添加一个模板

    cb.addTemplate(template,0,400);

  • 相关阅读:
    解决“已禁用对分布式事务管理器(MSDTC)的网络访问”错误
    C# Freely convert between IList<T> and IEnumerable<T>
    Json MaxJsonLength Error
    [转]Introducing the IE8 Developer Tools JScript Profiler
    页面调试心得
    ASP.NET中Label控件特殊之处
    模板模式
    Android中通过typeface设置字体
    Android 新浪微博授权
    【转】android:网络图片转为bitmap 保存至SD卡中
  • 原文地址:https://www.cnblogs.com/xuezhi/p/2793917.html
Copyright © 2011-2022 走看看