NPOI生成通知书



public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //创建document对象
            XWPFDocument doc = new XWPFDocument();

            //纸张横向 A4
            CT_SectPr m_SectPr = new CT_SectPr();
            m_SectPr.pgSz.w = (ulong)16838;
            m_SectPr.pgSz.h = (ulong)11906;
            doc.Document.body.sectPr = m_SectPr;

            //创建段落对象 - 标题
            XWPFParagraph title = doc.CreateParagraph();
            title.Alignment = ParagraphAlignment.CENTER;  //段落对其方式为居中  
            XWPFRun title_run = title.CreateRun();//段落中添加文字
            title_run.FontSize = 20;//设置大小  
            title_run.FontFamily = "宋体"; //设置字体  
            title_run.SetText("完税通知书");

            doc.CreateParagraph();

            //称呼
            XWPFParagraph address = doc.CreateParagraph();
            //段落对其方式为居中  
            XWPFRun address_run = address.CreateRun();//向该段落中添加文字  
            address_run.FontSize = 15;//设置大小  
            address_run.FontFamily = "宋体"; //设置字体  
            address_run.SetText("宝宝拉绿色大便:");

            //内容
            XWPFParagraph content = doc.CreateParagraph();
            content.IndentationFirstLine = this.Indentation("宋体", 21, 2, FontStyle.Regular);
            XWPFRun content_run = content.CreateRun();
            content_run.FontSize = 15;     //设置大小  
            content_run.FontFamily = "宋体"; //设置字体  
            content_run.SetText("许多细心的新妈妈会发现,宝宝的大便有时会呈现出绿色。" +
                "因此很多妈妈非常担心,是不是宝宝的消化出现了问题?那么,宝宝拉绿便是正常的现象么?到底是什么原因导致的呢?");

            XWPFParagraph qrcode = doc.CreateParagraph();
            XWPFRun qrcode_run = qrcode.CreateRun();
            CT_WrapSquare wrapSquare = new CT_WrapSquare();
            wrapSquare.wrapText = ST_WrapText.bothSides;
            FileStream fs = new FileStream("d:\\1490801014.jpg", FileMode.Open, FileAccess.Read);
            qrcode_run.AddPicture(fs, (int)PictureType.JPEG, "1.jpg", 1000000, 1000000);

            XWPFParagraph date = doc.CreateParagraph();
            date.IndentationRight = this.Indentation("宋体", 21, 4, FontStyle.Regular);
            date.Alignment = ParagraphAlignment.RIGHT;
            XWPFRun date_run = date.CreateRun();
            date_run.FontSize = 15;//设置大小  
            date_run.FontFamily = "宋体"; //设置字体  
            date_run.SetText("2017年3月29日");

            string path = "d://simpleTable.docx"; ;
            FileStream out1 = new FileStream(path, FileMode.Create);
            doc.Write(out1);
            out1.Close();

            System.Diagnostics.Process.Start(path);
        }

        //一个字符的宽度
        private int Indentation(String fontname, int fontsize, int Indentationfonts, FontStyle fs)
        {
            //字显示宽度,用于段首行缩进
            //字号与fontsize关系
            //初号(0号)=84,小初=72,1号=52,2号=44,小2=36,3号=32,小3=30,4号=28,
            //小4=24,5号=21,小5=18,6号=15,小6=13,7号=11,8号=10
            Graphics m_tmpGr = this.CreateGraphics();
            m_tmpGr.PageUnit = GraphicsUnit.Point;
            SizeF size = m_tmpGr.MeasureString("好", new Font(fontname, fontsize * 0.75F, fs));
            return (int)size.Width * Indentationfonts * 10;
        }
    }


效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值