iTextSharp 页面设置

页面设置

页面尺寸

  1. new Document()
    默认A4尺寸 默认页边距 上下左右是36磅
  2. new Document(Rectangle rect);
    指定页面大小 默认页边距 上下左右 是36磅
  3. new Document(Rectangle rect,页边距左,右,上,下)
    指定页面尺寸 和 页边距
    4.SetPageSize(PageSize.A4)方法
    设置页面尺寸为A4
    注:PageSize有多个参数 A0-A10等等
  4. 默认页面是竖的 可以旋转页面为横向
    new Document(PageSize.A4.Rotate());
    注意:若要设置首页尺寸 需要在文档打开前设置 ;
    一个页面内 只有首个 SetMargins 起作用
    下一个页面 受到上一个页面中 最后一个 SetMargins 的影响

页边距

  1. 页边距 使用磅做为单位 1英寸=72磅
  2. SetMargins(float marginLeft, float marginRight, float marginTop, float marginBottom);
    设置左右 上下页边距
  3. SetMarginMirroring(true);
    水平方向镜像页边距 左右页边距交换 上下页边距不变
  4. SetMarginMirroringTopBottom(true);
    竖直方向页边距 上下页边距交互 左右页边距不变
    注意:如果要设置第一页的pdf页边距 需要在文档打开前设置

新页面

NewPage();打开新的页面

页面初始化

Opne方法会进行 页面初始化操作
因此若要设置 首页页面尺寸、页边距等,需要在Open方法之前进行操作;若在Open方法之后设置页面尺寸, 设置的页面尺寸只对首页之后的页面起作用。

实例

//1.页面尺寸 默认A4 页边距 36磅
Document document = new Document(PageSize.A4.Rotate());//横向

//2.页边距 影响所有页面 默认36磅
//一个页面内 只有首个 SetMargins 起作用   
//下一个页面 受到上一个页面中 最后一个 SetMargins 的影响             
document.SetMargins(36, 0, 36, 0);//单位磅 1英寸=72磅

PdfWriter.GetInstance(document, new FileStream("Chapter01.pdf", FileMode.Create));
document.Open();

document.Add(new Paragraph("*****************************************************"));

//3.镜像 水平页边距 
document.SetMarginMirroring(true);//镜像 水平页边距   交换 左右页边距
document.SetMarginMirroringTopBottom(true);//镜像 竖直页边距   交换 顶部和底部页边距

//4.换页 
//SetPageSize 要在换页前使用    才可以作用于新的页面
document.SetPageSize(PageSize.A2);      
document.NewPage();
document.Add(new Paragraph("*****************************************************"));
document.Close();

猜你喜欢

转载自blog.csdn.net/weixin_43796392/article/details/124344905