Exemplo n.º 1
0
        private void SaveErrorFile(string strFile, Bitmap img)
        {
            XWPFDocument m_Docx   = new XWPFDocument();
            CT_SectPr    m_SectPr = new CT_SectPr();

            //页面设置A4
            m_SectPr.pgSz.w = (ulong)16838;
            m_SectPr.pgSz.h = (ulong)11906;

            m_Docx.Document.body.sectPr = m_SectPr;

            // 创建段落
            CT_P m_p = m_Docx.Document.body.AddNewP();

            m_p.AddNewPPr().AddNewJc().val = ST_Jc.left;                //段落水平居左
            XWPFParagraph gp = new XWPFParagraph(m_p, m_Docx);          //创建XWPFParagraph

            CT_R m_r = m_p.AddNewR();

            m_r.AddNewT().Value = "记录时间:" + DateTime.Now.ToString("yyyy-MM-dd mm:dd:ss");
            m_r.AddNewCr();
            m_r.AddNewT().Value = "账套公司名称:" + mstrCompanyName;
            m_r.AddNewCr();
            m_r.AddNewT().Value = "按钮名称:" + mstrButtonAction;
            WriteText(m_r, txtMainInfo.Text);
            WriteText(m_r, mstrMoreInfo);

            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                using (Bitmap bmp = GetScreenImage())
                {
                    bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Gif);
                }

                stream.Position = 0;

                m_p = m_Docx.Document.body.AddNewP();
                m_p.AddNewPPr().AddNewJc().val = ST_Jc.center;                //段落水平居中
                gp = new XWPFParagraph(m_p, m_Docx);
                XWPFRun gr = gp.CreateRun();
                gr.AddPicture(stream, (int)NPOI.XWPF.UserModel.PictureType.GIF, "1.gif", 360000 * 23, 360000 * 16);
            }

            using (FileStream writer = new FileStream(strFile, FileMode.CreateNew))
            {
                m_Docx.Write(writer);
                writer.Close();
            }
        }
Exemplo n.º 2
0
        public void AddRow(int level, String title, int page, String bookmarkRef)
        {
            CT_SdtContentBlock contentBlock = this.block.sdtContent;
            CT_P p = contentBlock.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("TOC" + level);
            CT_Tabs    tabs        = pPr.AddNewTabs();
            CT_TabStop tab         = tabs.AddNewTab();

            tab.val    = (ST_TabJc.right);
            tab.leader = (ST_TabTlc.dot);
            tab.pos    = "8290"; //(new BigInteger("8290"));
            pPr.AddNewRPr().AddNewNoProof();
            CT_R Run = p.AddNewR();

            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewT().Value = (title);
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewTab();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.begin);
            // pageref run
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            CT_Text text = Run.AddNewInstrText();

            text.space = "preserve";// (Space.PRESERVE);
            // bookmark reference
            text.Value = (" PAGEREF _Toc" + bookmarkRef + " \\h ");
            p.AddNewR().AddNewRPr().AddNewNoProof();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.separate);
            // page number run
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewT().Value = page.ToString();
            Run = p.AddNewR();
            Run.AddNewRPr().AddNewNoProof();
            Run.AddNewFldChar().fldCharType = (ST_FldCharType.end);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 为XWPFDocument文档复制指定索引的表
        /// </summary>
        /// <param name="readDoc">模板文件</param>
        /// <param name="tableIndex">需要复制模板的table的索引</param>
        /// <param name="targetIndex">复制到目标位置的table索引(如果目标位置原来有表格,会被覆盖)</param>
        /// <param name="myDoc">新创建的文件</param>
        public static void CopyTable(XWPFDocument readDoc, int tableIndex, int targetIndex, XWPFDocument myDoc)
        {
            var    sourceTable = readDoc.Tables[tableIndex];
            CT_Tbl sourceCTTbl = readDoc.Document.body.GetTblArray(8);

            var targetTable = myDoc.CreateTable();

            myDoc.SetTable(targetIndex, targetTable);
            var targetCTTbl = myDoc.Document.body.GetTblArray()[myDoc.Document.body.GetTblArray().Length - 1];

            targetCTTbl.tblPr        = sourceCTTbl.tblPr;
            targetCTTbl.tblPr.jc.val = ST_Jc.left;//表格在页面水平位置
            //targetCTTbl.tblGrid = sourceCTTbl.tblGrid;

            for (int i = 0; i < sourceTable.Rows.Count; i++)
            {
                var tbRow     = targetTable.CreateRow();
                var targetRow = tbRow.GetCTRow();
                tbRow.RemoveCell(0);
                XWPFTableRow row = sourceTable.Rows[i];
                targetRow.trPr = row.GetCTRow().trPr;
                for (int c = 0; c < row.GetTableCells().Count; c++)
                {
                    var tbCell = tbRow.CreateCell();
                    tbCell.RemoveParagraph(0);
                    var targetCell = tbCell.GetCTTc();

                    XWPFTableCell cell = row.GetTableCells()[c];
                    targetCell.tcPr = cell.GetCTTc().tcPr;
                    for (int p = 0; p < cell.Paragraphs.Count; p++)
                    {
                        var           tbPhs     = tbCell.AddParagraph();
                        CT_P          targetPhs = tbPhs.GetCTP();
                        XWPFParagraph para      = cell.Paragraphs[p];
                        var           paraCTP   = para.GetCTP();
                        targetPhs.pPr          = paraCTP.pPr;
                        targetPhs.rsidR        = paraCTP.rsidR;
                        targetPhs.rsidRPr      = paraCTP.rsidRPr;
                        targetPhs.rsidRDefault = paraCTP.rsidRDefault;
                        targetPhs.rsidP        = paraCTP.rsidP;

                        for (int r = 0; r < para.Runs.Count; r++)
                        {
                            var  tbRun     = tbPhs.CreateRun();
                            CT_R targetRun = tbRun.GetCTR();

                            XWPFRun run    = para.Runs[r];
                            var     runCTR = run.GetCTR();
                            targetRun.rPr     = runCTR.rPr;
                            targetRun.rsidRPr = runCTR.rsidRPr;
                            targetRun.rsidR   = runCTR.rsidR;
                            CT_Text text = targetRun.AddNewT();
                            text.Value = run.Text;
                        }
                    }
                }
            }
            targetTable.RemoveRow(0);
        }
        /// <summary>
        /// Appends a text run containing a single space.  The run is
        /// space-preserved so that the space will be visible.
        /// </summary>
        /// <param name="paragraph">The parent paragraph to add the run to.</param>
        private void AppendSpace(XWPFParagraph paragraph)
        {
            XWPFRun run = paragraph.CreateRun();

            setRTL(run);
            CT_R    ctr  = run.GetCTR();
            CT_Text text = ctr.AddNewT();

            text.Value = " ";
            text.space = "preserve";
        }
Exemplo n.º 5
0
        public void TestSetGetText()
        {
            ctRun.AddNewT().Value = ("TEST STRING");
            ctRun.AddNewT().Value = ("TEST2 STRING");
            ctRun.AddNewT().Value = ("TEST3 STRING");

            Assert.AreEqual(3, ctRun.SizeOfTArray());
            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual("TEST2 STRING", run.GetText(1));

            run.SetText("NEW STRING", 0);
            Assert.AreEqual("NEW STRING", run.GetText(0));

            //Run.Text=("xxx",14);
            //Assert.Fail("Position wrong");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Sets the text of this text run.in the
        /// </summary>
        /// <param name="value">the literal text which shall be displayed in the document</param>
        /// <param name="pos">position in the text array (NB: 0 based)</param>
        public void SetText(String value, int pos)
        {
            int length = run.SizeOfTArray();

            if (pos > length)
            {
                throw new IndexOutOfRangeException("Value too large for the parameter position");
            }
            CT_Text t = (pos < length && pos >= 0) ? run.GetTArray(pos): run.AddNewT();

            t.Value = (value);
            preserveSpaces(t);
        }
Exemplo n.º 7
0
 private void WriteText(CT_R m_r, string text)
 {
     using (StringReader reader = new StringReader(text))
     {
         string strLine = reader.ReadLine();
         while (strLine != null)
         {
             m_r.AddNewT().Value = strLine;
             m_r.AddNewCr();
             strLine = reader.ReadLine();
         }
     }
 }
Exemplo n.º 8
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr         sdtPr = block.AddNewSdtPr();
            CT_DecimalNumber id    = sdtPr.AddNewId();

            id.val = ("4844945");
            sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of Contents");
            CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr();
            CT_RPr      rPr      = sdtEndPr.AddNewRPr();
            CT_Fonts    fonts    = rPr.AddNewRFonts();

            fonts.asciiTheme    = (ST_Theme.minorHAnsi);
            fonts.eastAsiaTheme = (ST_Theme.minorHAnsi);
            fonts.hAnsiTheme    = (ST_Theme.minorHAnsi);
            fonts.cstheme       = (ST_Theme.minorBidi);
            CT_SdtContentBlock content = block.AddNewSdtContent();
            CT_P p = content.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("TOCHeading");
            pPr.AddNewJc().val     = ST_Jc.center;
            CT_R run = p.AddNewR();

            run.AddNewRPr().AddNewSz().val = 48;
            run.AddNewT().Value            = ("Table of Contents");
            run.AddNewBr().type            = ST_BrType.textWrapping; // line break

            // TOC Field
            p   = content.AddNewP();
            pPr = p.AddNewPPr();
            pPr.AddNewPStyle().val = "TOC1";
            pPr.AddNewRPr().AddNewNoProof();

            run = p.AddNewR();
            run.AddNewFldChar().fldCharType = ST_FldCharType.begin;

            run = p.AddNewR();
            CT_Text text = run.AddNewInstrText();

            text.space = "preserve";
            text.Value = (" TOC \\h \\z ");

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
        }
Exemplo n.º 9
0
        public void AddRow(int level, string title, int page, string bookmarkRef)
        {
            CT_P ctP = this.block.sdtContent.AddNewP();

            byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24");
            ctP.rsidR        = bytes;
            ctP.rsidRDefault = bytes;
            CT_PPr ctPpr = ctP.AddNewPPr();

            ctPpr.AddNewPStyle().val = nameof(TOC) + (object)level;
            CT_TabStop ctTabStop     = ctPpr.AddNewTabs().AddNewTab();

            ctTabStop.val    = ST_TabJc.right;
            ctTabStop.leader = ST_TabTlc.dot;
            ctTabStop.pos    = "8290";
            ctPpr.AddNewRPr().AddNewNoProof();
            CT_R ctR1 = ctP.AddNewR();

            ctR1.AddNewRPr().AddNewNoProof();
            ctR1.AddNewT().Value = title;
            CT_R ctR2            = ctP.AddNewR();

            ctR2.AddNewRPr().AddNewNoProof();
            ctR2.AddNewTab();
            CT_R ctR3 = ctP.AddNewR();

            ctR3.AddNewRPr().AddNewNoProof();
            ctR3.AddNewFldChar().fldCharType = ST_FldCharType.begin;
            CT_R ctR4 = ctP.AddNewR();

            ctR4.AddNewRPr().AddNewNoProof();
            CT_Text ctText = ctR4.AddNewInstrText();

            ctText.space = "preserve";
            ctText.Value = " PAGEREF _Toc" + bookmarkRef + " \\h ";
            ctP.AddNewR().AddNewRPr().AddNewNoProof();
            CT_R ctR5 = ctP.AddNewR();

            ctR5.AddNewRPr().AddNewNoProof();
            ctR5.AddNewFldChar().fldCharType = ST_FldCharType.separate;
            CT_R ctR6 = ctP.AddNewR();

            ctR6.AddNewRPr().AddNewNoProof();
            ctR6.AddNewT().Value = page.ToString();
            CT_R ctR7            = ctP.AddNewR();

            ctR7.AddNewRPr().AddNewNoProof();
            ctR7.AddNewFldChar().fldCharType = ST_FldCharType.end;
        }
Exemplo n.º 10
0
        public void TestGetText()
        {
            XWPFDocument doc       = new XWPFDocument();
            CT_Tbl       table     = new CT_Tbl();
            CT_Row       row       = table.AddNewTr();
            CT_Tc        cell      = row.AddNewTc();
            CT_P         paragraph = cell.AddNewP();
            CT_R         run       = paragraph.AddNewR();
            CT_Text      text      = run.AddNewT();

            text.Value = ("finally I can Write!");

            XWPFTable xtab = new XWPFTable(table, doc);

            Assert.AreEqual("finally I can Write!\n", xtab.GetText());
        }
Exemplo n.º 11
0
        public void TestSetHeader()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            // no header is Set (yet)
            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            Assert.IsNull(policy.GetDefaultHeader());
            Assert.IsNull(policy.GetFirstPageHeader());
            Assert.IsNull(policy.GetDefaultFooter());

            CT_P    ctP1 = new CT_P();
            CT_R    ctR1 = ctP1.AddNewR();
            CT_Text t    = ctR1.AddNewT();

            t.Value = ("Paragraph in header");

            // Commented MB 23 May 2010
            //CTP ctP2 = CTP.Factory.NewInstance();
            //CTR ctR2 = ctP2.AddNewR();
            //CTText t2 = ctR2.AddNewT();
            //t2.StringValue=("Second paragraph.. for footer");

            // Create two paragraphs for insertion into the footer.
            // Previously only one was inserted MB 23 May 2010
            CT_P    ctP2 = new CT_P();
            CT_R    ctR2 = ctP2.AddNewR();
            CT_Text t2   = ctR2.AddNewT();

            t2.Value = ("First paragraph for the footer");

            CT_P    ctP3 = new CT_P();
            CT_R    ctR3 = ctP3.AddNewR();
            CT_Text t3   = ctR3.AddNewT();

            t3.Value = ("Second paragraph for the footer");

            XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);

            XWPFParagraph[] pars = new XWPFParagraph[1];
            pars[0] = p1;

            XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
            XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);

            XWPFParagraph[] pars2 = new XWPFParagraph[2];
            pars2[0] = p2;
            pars2[1] = p3;

            // Set headers
            policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
            policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST);
            // Set a default footer and capture the returned XWPFFooter object.
            XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);

            // Ensure the headers and footer were Set correctly....
            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNotNull(policy.GetDefaultFooter());
            // ....and that the footer object captured above Contains two
            // paragraphs of text.
            Assert.AreEqual(2, footer.Paragraphs.Count);

            // As an Additional Check, recover the defauls footer and
            // make sure that it Contains two paragraphs of text and that
            // both do hold what is expected.
            footer = policy.GetDefaultFooter();

            XWPFParagraph[] paras = new XWPFParagraph[footer.Paragraphs.Count];
            int             i     = 0;

            foreach (XWPFParagraph p in footer.Paragraphs)
            {
                paras[i++] = p;
            }

            Assert.AreEqual(2, paras.Length);
            Assert.AreEqual("First paragraph for the footer", paras[0].Text);
            Assert.AreEqual("Second paragraph for the footer", paras[1].Text);
        }
Exemplo n.º 12
0
        public void TestSetHeader()
        {
            XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            // no header is Set (yet)
            XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy();

            Assert.IsNull(policy.GetDefaultHeader());
            Assert.IsNull(policy.GetFirstPageHeader());
            Assert.IsNull(policy.GetDefaultFooter());

            CT_P    ctP1  = new CT_P();
            CT_R    ctR1  = ctP1.AddNewR();
            CT_Text t     = ctR1.AddNewT();
            String  tText = "Paragraph in header";

            t.Value = tText;

            // Commented MB 23 May 2010
            //CTP ctP2 = CTP.Factory.NewInstance();
            //CTR ctR2 = ctP2.AddNewR();
            //CTText t2 = ctR2.AddNewT();
            //t2.StringValue=("Second paragraph.. for footer");

            // Create two paragraphs for insertion into the footer.
            // Previously only one was inserted MB 23 May 2010
            CT_P    ctP2 = new CT_P();
            CT_R    ctR2 = ctP2.AddNewR();
            CT_Text t2   = ctR2.AddNewT();

            t2.Value = ("First paragraph for the footer");

            CT_P    ctP3 = new CT_P();
            CT_R    ctR3 = ctP3.AddNewR();
            CT_Text t3   = ctR3.AddNewT();

            t3.Value = ("Second paragraph for the footer");

            XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc);

            XWPFParagraph[] pars = new XWPFParagraph[1];
            pars[0] = p1;

            XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc);
            XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc);

            XWPFParagraph[] pars2 = new XWPFParagraph[2];
            pars2[0] = p2;
            pars2[1] = p3;

            // Set headers
            XWPFHeader headerD = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
            XWPFHeader headerF = policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST);
            // Set a default footer and capture the returned XWPFFooter object.
            XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2);

            // Ensure the headers and footer were Set correctly....
            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNotNull(policy.GetDefaultFooter());
            // ....and that the footer object captured above Contains two
            // paragraphs of text.
            Assert.AreEqual(2, footer.Paragraphs.Count);

            // Check the header created with the paragraph got them, and the one
            // created without got an empty one
            Assert.AreEqual(1, headerD.Paragraphs.Count);
            Assert.AreEqual(1, headerF.Paragraphs.Count);

            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual("", headerF.Paragraphs[0].Text);

            // As an Additional Check, recover the defauls footer and
            // make sure that it Contains two paragraphs of text and that
            // both do hold what is expected.
            footer = policy.GetDefaultFooter();

            XWPFParagraph[] paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray();

            Assert.AreEqual(2, paras.Length);
            Assert.AreEqual("First paragraph for the footer", paras[0].Text);
            Assert.AreEqual("Second paragraph for the footer", paras[1].Text);

            // Add some text to the empty header
            String fText1 = "New Text!";

            headerF.Paragraphs[0].InsertNewRun(0).SetText(fText1);
            // TODO Add another paragraph and check

            // Check it
            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual(fText1, headerF.Paragraphs[0].Text);

            // Save, re-open, ensure it's all still there
            XWPFDocument reopened = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc);

            policy = reopened.GetHeaderFooterPolicy();
            Assert.IsNotNull(policy.GetDefaultHeader());
            Assert.IsNotNull(policy.GetFirstPageHeader());
            Assert.IsNull(policy.GetEvenPageHeader());
            Assert.IsNotNull(policy.GetDefaultFooter());
            Assert.IsNull(policy.GetFirstPageFooter());
            Assert.IsNull(policy.GetEvenPageFooter());

            // Check the new headers still have their text
            headerD = policy.GetDefaultHeader();
            headerF = policy.GetFirstPageHeader();
            Assert.AreEqual(tText, headerD.Paragraphs[0].Text);
            Assert.AreEqual(fText1, headerF.Paragraphs[0].Text);

            // Check the new footers have their new text too
            footer = policy.GetDefaultFooter();
            paras  = new List <XWPFParagraph>(footer.Paragraphs).ToArray();

            Assert.AreEqual(2, paras.Length);
            Assert.AreEqual("First paragraph for the footer", paras[0].Text);
            Assert.AreEqual("Second paragraph for the footer", paras[1].Text);
        }
        /// <summary>
        /// Creates a new section with the given page header.  Must be
        /// called *after* the final paragraph of the section.  In DOCX, a
        /// section definition is a child of the final paragraph of the
        /// section, except for the final section of the document, which
        /// is a direct child of the body.
        /// </summary>
        /// <param name="bookname"> The name of the book to display, usually from the \h marker </param>
        public void createBookHeaders(string bookname)
        {
            // Create page heading content for book
            CT_Hdr header          = new CT_Hdr();
            CT_P   headerParagraph = header.AddNewP();
            CT_PPr ppr             = headerParagraph.AddNewPPr();
            CT_Jc  align           = ppr.AddNewJc();

            align.val = ST_Jc.center;
            CT_R run = headerParagraph.AddNewR();

            // Show page numbers if requested
            if (configDocx.showPageNumbers)
            {
                // Page number
                run.AddNewFldChar().fldCharType = ST_FldCharType.begin;
                run.AddNewInstrText().Value     = " PAGE ";
                run.AddNewFldChar().fldCharType = ST_FldCharType.separate;
                run.AddNewInstrText().Value     = "1";
                run.AddNewFldChar().fldCharType = ST_FldCharType.end;
                run.AddNewT().Value             = "  -  ";
            }

            // Book name
            run.AddNewT().Value = bookname == null ? "" : bookname;
            // Chapter name
            if (currentChapterLabel.Length > 0)
            {
                run.AddNewT().Value = "  -  ";
                run.AddNewT().Value = currentChapterLabel;
            }


            // Create page header
            XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount);

            documentHeader.SetHeaderFooter(header);

            // Create new section and set its header
            CT_SectPr newSection = newDoc.Document.body.AddNewP().AddNewPPr().createSectPr();

            newSection.type     = new CT_SectType();
            newSection.type.val = ST_SectionMark.continuous;
            CT_HdrFtrRef headerRef = newSection.AddNewHeaderReference();

            headerRef.type = ST_HdrFtr.@default;
            headerRef.id   = documentHeader.GetPackageRelationship().Id;

            // Set number of columns
            newSection.cols.num = configDocx.columnCount.ToString();

            // Set page numbers
            CT_PageNumber pageNum = new CT_PageNumber
            {
                fmt = ST_NumberFormat.@decimal
            };

            newSection.pgNumType = pageNum;

            // Increment page header count so each one gets a unique ID
            pageHeaderCount++;
        }
Exemplo n.º 14
0
        /// <summary>
        /// 创建页眉页脚
        /// </summary>
        /// <param name="doc"></param>
        private void CreatePageHeaderFooter(XWPFDocument doc, CT_SectPr m_Sectpr)
        {
            XWPFParagraph p1 = doc.CreateParagraph();

            p1.CreateRun().AddBreak();//新建页

            //创建页眉

            CT_Hdr m_hdr = new CT_Hdr();

            m_hdr.Items = new System.Collections.ArrayList();


            CT_P   m_p    = m_hdr.AddNewP();
            CT_PPr cT_PPr = m_p.AddNewPPr();

            cT_PPr.AddNewJc().val = ST_Jc.both;//两端对齐
            cT_PPr.AddNewSpacing().beforeLines = "370";


            CT_R cT_R = m_p.AddNewR();

            cT_R.AddNewT().Value = "110kV濂泉(沙河)送电线路工程                 施工图设计说明书                     S123456S-D0101-01";//页眉内容
            CT_RPr cT_RPr        = cT_R.AddNewRPr();

            cT_RPr.AddNewSz().val       = (ulong)18;
            cT_RPr.AddNewSzCs().val     = (ulong)18;
            cT_RPr.AddNewRFonts().ascii = "宋体";
            //cT_RPr.AddNewU().val=ST_Underline.single;//下划线

            //创建页眉关系(headern.xml)
            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   m_h       = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), 3);

            //doc.CreateFootnotes();
            //设置页眉
            m_h.SetHeaderFooter(m_hdr);
            CT_HdrFtrRef m_HdrFtr = m_Sectpr.AddNewHeaderReference();

            m_HdrFtr.type = ST_HdrFtr.@default;
            //m_h.GetRelationById(m_HdrFtr.id);
            m_HdrFtr.id = m_h.GetPackageRelationship().Id;



            //创建页脚
            CT_Ftr m_ftr = new CT_Ftr();

            m_ftr.Items = new System.Collections.ArrayList();
            CT_SdtBlock   m_Sdt          = new CT_SdtBlock();
            CT_SdtPr      m_SdtPr        = m_Sdt.AddNewSdtPr();
            CT_SdtDocPart m_SdDocPartObj = m_SdtPr.AddNewDocPartObj();

            m_SdDocPartObj.AddNewDocPartGallery().val = "PageNumbers (Bottom of Page)";
            m_SdDocPartObj.docPartUnique = new CT_OnOff();
            CT_SdtContentBlock m_SdtContent = m_Sdt.AddNewSdtContent();
            CT_P   m_SdtContentP            = m_SdtContent.AddNewP();
            CT_PPr m_SdtContentPpr          = m_SdtContentP.AddNewPPr();

            m_SdtContentPpr.AddNewJc().val = ST_Jc.center;
            m_SdtContentP.Items = new System.Collections.ArrayList();
            CT_SimpleField m_fldSimple = new CT_SimpleField();

            m_fldSimple.instr = " PAGE   \\*MERGEFORMAT";

            //页码字体大小
            CT_R   m_r   = new CT_R();
            CT_RPr m_Rpr = m_r.AddNewRPr();

            m_Rpr.AddNewRFonts().ascii = "宋体";
            m_Rpr.AddNewSz().val       = (ulong)18;
            m_Rpr.AddNewSzCs().val     = (ulong)18;
            m_r.AddNewT().Value        = "1";//页数
            m_fldSimple.Items.Add(m_r);

            m_SdtContentP.Items.Add(m_fldSimple);
            m_ftr.Items.Add(m_Sdt);

            //m_ftr.AddNewP().AddNewR().AddNewT().Value = "fff";//页脚内容
            //m_ftr.AddNewP().AddNewPPr().AddNewJc().val = ST_Jc.center;
            //创建页脚关系(footern.xml)
            XWPFRelation Frelation = XWPFRelation.FOOTER;
            XWPFFooter   m_f       = (XWPFFooter)doc.CreateRelationship(Frelation, XWPFFactory.GetInstance(), doc.FooterList.Count + 1);

            //设置页脚
            m_f.SetHeaderFooter(m_ftr);
            m_HdrFtr      = m_Sectpr.AddNewFooterReference();
            m_HdrFtr.type = ST_HdrFtr.@default;
            m_HdrFtr.id   = m_f.GetPackageRelationship().Id;
        }