简述
本文章中NPOI版本为2.5.3.0。
页面设置
void main()
{
XWPFDocument document = new XWPFDocument();
document.Document.body.sectPr = new CT_SectPr();
var sectPr = document.Document.body.sectPr;
SetPageSz(sectPr.pgSz,"A4", ST_PageOrientation.portrait);//A4纵向
SetPageMar(sectPr.pgMar, "Medium");//中等边框
}
void SetPageSz(CT_PageSz size, string pageSz, ST_PageOrientation orientation)
{
//这里的单位是缇,1英寸=1440缇,1英寸=2.54cm,1cm=1440÷2.54≈567缇
//A4纸的物理尺寸是210×297mm,
//210mm=210×144÷2.54≈11905.51181102362
//297mm=297×144÷2.54≈16837.79527559055
size.orient = orientation;
switch (pageSz)//默认A4
{
case "A3": size.w = 16838; size.h = 23811; break;//297x420mm
case "A4": size.w = 11906; size.h = 16838; break;//210x297mm
case "A5": size.w = 8391; size.h = 11906; break;//148x210mm
}
if (orientation != ST_PageOrientation.portrait)
{
(size.h, size.w) = (size.w, size.h);
}
}
void SetPageMar(CT_PageMar margin, string pageMar)
{
switch (type)
{
case "Normal": m.left = m.right = 1800; m.top = m.bottom = "1440"; break;
case "Narrow": m.left = m.right = 720; m.top = m.bottom = "720"; break;
case "Medium": m.left = m.right = 1080; m.top = m.bottom = "1440"; break;
case "Broad": m.left = m.right =