/// <summary>
        /// Appends a new run to this paragraph
        /// </summary>
        /// <returns>a new text run</returns>
        public XWPFRun CreateRun()
        {
            XWPFRun xwpfRun = new XWPFRun(paragraph.AddNewR(), this);

            runs.Add(xwpfRun);
            return(xwpfRun);
        }
Exemple #2
0
        /**
         * add a new run at the end of the position of
         * the content of parameter run
         * @param run
         */
        protected void AddRun(CT_R Run)
        {
            int pos;

            pos = paragraph.GetRList().Count;
            paragraph.AddNewR();
            paragraph.SetRArray(pos, Run);
        }
Exemple #3
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;
        }
Exemple #4
0
        public TOC(CT_SdtBlock block)
        {
            this.block = block;
            CT_SdtPr ctSdtPr = block.AddNewSdtPr();

            ctSdtPr.AddNewId().val = "4844945";
            ctSdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = "Table of contents";
            CT_RPr   ctRpr   = block.AddNewSdtEndPr().AddNewRPr();
            CT_Fonts ctFonts = ctRpr.AddNewRFonts();

            ctFonts.asciiTheme    = ST_Theme.minorHAnsi;
            ctFonts.eastAsiaTheme = ST_Theme.minorHAnsi;
            ctFonts.hAnsiTheme    = ST_Theme.minorHAnsi;
            ctFonts.cstheme       = ST_Theme.minorBidi;
            ctRpr.AddNewB().val     = ST_OnOff.off;
            ctRpr.AddNewBCs().val   = ST_OnOff.off;
            ctRpr.AddNewColor().val = "auto";
            ctRpr.AddNewSz().val    = 24UL;
            ctRpr.AddNewSzCs().val  = 24UL;
            CT_P ctP = block.AddNewSdtContent().AddNewP();

            byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24");
            ctP.rsidR        = bytes;
            ctP.rsidRDefault = bytes;
            ctP.AddNewPPr().AddNewPStyle().val = "TOCHeading";
            ctP.AddNewR().AddNewT().Value      = "Table of Contents";
        }
Exemple #5
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);
            rPr.AddNewB().val          = false;
            rPr.AddNewBCs().val        = false;
            rPr.AddNewColor().val      = ("auto");
            rPr.AddNewSz().val         = 24;
            rPr.AddNewSzCs().val       = 24;
            CT_SdtContentBlock content = block.AddNewSdtContent();
            CT_P p = content.AddNewP();

            byte[] b = Encoding.Unicode.GetBytes("00EF7E24");
            p.rsidR        = b;
            p.rsidRDefault = b;
            p.AddNewPPr().AddNewPStyle().val = ("TOCHeading");
            p.AddNewR().AddNewT().Value      = ("Table of Contents");
        }
Exemple #6
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;
        }
Exemple #7
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);
        }
Exemple #8
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();
            }
        }
Exemple #9
0
        private static void CreateFooter(XWPFDocument doc, string headerText, string imageUrl = null)
        {
            if (doc.Document.body.sectPr == null)
            {
                doc.Document.body.sectPr = new CT_SectPr();
            }
            CT_SectPr m_SectPr = doc.Document.body.sectPr;

            CT_Ftr footer = new CT_Ftr();

            CT_SdtBlock   sdtBlock   = new CT_SdtBlock();
            CT_SdtPr      sdtPr      = sdtBlock.AddNewSdtPr();
            CT_SdtDocPart sdtDocPart = sdtPr.AddNewDocPartObj();

            sdtDocPart.AddNewDocPartGallery().val = "Page Numbers (Bottom of Page)";
            sdtDocPart.docPartUnique = new CT_OnOff();

            CT_SdtContentBlock sdtContentBlock = sdtBlock.AddNewSdtContent();
            CT_P p = sdtContentBlock.AddNewP();

            p.AddNewPPr().AddNewJc().val = ST_Jc.center;

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
            p.AddNewR().AddNewInstrText().Value     = "PAGE";
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;

            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
            p.AddNewR().AddNewInstrText().Value     = "NUMPAGES";
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
            p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;


            footer.Items.Add(sdtBlock);

            XWPFRelation Hrelation  = XWPFRelation.FOOTER;
            XWPFFooter   xwpfFooter = (XWPFFooter)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.FooterList.Count);

            xwpfFooter.SetXWPFDocument(doc);
            xwpfFooter.SetHeaderFooter(footer);

            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewFooterReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = xwpfFooter.GetPackageRelationship().Id;
        }
Exemple #10
0
        public CT_SdtBlock Build()
        {
            // append end field char for TOC - only once
            if (!isBuilt)
            {
                CT_SdtContentBlock contentBlock = block.sdtContent;
                CT_P p   = contentBlock.AddNewP();
                CT_R run = p.AddNewR();
                run.AddNewRPr().AddNewNoProof();
                run.AddNewFldChar().fldCharType = ST_FldCharType.end;

                isBuilt = true;
            }

            return(block);
        }
Exemple #11
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());
        }
Exemple #12
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;
        }
Exemple #13
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);
        }
Exemple #14
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].GetText());
            Assert.AreEqual("Second paragraph for the footer", paras[1].GetText());
        }
        /// <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++;
        }
Exemple #16
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);
        }
        /*
         * This is the default Watermark paragraph; the only variable is the text message
         * TODO: manage all the other variables
         */
        private XWPFParagraph GetWatermarkParagraph(String text, int idx)
        {
            CT_P p = new CT_P();
            byte[] rsidr = doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
            p.rsidP = (rsidr);
            p.rsidRDefault = (rsidrdefault);
            CT_PPr pPr = p.AddNewPPr();
            pPr.AddNewPStyle().val = ("Header");
            // start watermark paragraph
            NPOI.OpenXmlFormats.Wordprocessing.CT_R r = p.AddNewR();
            CT_RPr rPr = r.AddNewRPr();
            rPr.AddNewNoProof();
            CT_Picture pict = r.AddNewPict();

            CT_Group group = new CT_Group();
            CT_Shapetype shapetype = group.AddNewShapetype();
            shapetype.id = ("_x0000_t136");
            shapetype.coordsize = ("1600,21600");
            shapetype.spt = (136);
            shapetype.adj = ("10800");
            shapetype.path2 = ("m@7,0l@8,0m@5,21600l@6,21600e");
            CT_Formulas formulas = shapetype.AddNewFormulas();
            formulas.AddNewF().eqn=("sum #0 0 10800");
            formulas.AddNewF().eqn = ("prod #0 2 1");
            formulas.AddNewF().eqn = ("sum 21600 0 @1");
            formulas.AddNewF().eqn = ("sum 0 0 @2");
            formulas.AddNewF().eqn = ("sum 21600 0 @3");
            formulas.AddNewF().eqn = ("if @0 @3 0");
            formulas.AddNewF().eqn = ("if @0 21600 @1");
            formulas.AddNewF().eqn = ("if @0 0 @2");
            formulas.AddNewF().eqn = ("if @0 @4 21600");
            formulas.AddNewF().eqn = ("mid @5 @6");
            formulas.AddNewF().eqn = ("mid @8 @5");
            formulas.AddNewF().eqn = ("mid @7 @8");
            formulas.AddNewF().eqn = ("mid @6 @7");
            formulas.AddNewF().eqn = ("sum @6 0 @5");
            CT_Path path = shapetype.AddNewPath();
            path.textpathok=(ST_TrueFalse.t);
            path.connecttype=(ST_ConnectType.custom);
            path.connectlocs=("@9,0;@10,10800;@11,21600;@12,10800");
            path.connectangles=("270,180,90,0");
            CT_TextPath shapeTypeTextPath = shapetype.AddNewTextpath();
            shapeTypeTextPath.on=(ST_TrueFalse.t);
            shapeTypeTextPath.fitshape=(ST_TrueFalse.t);
            CT_Handles handles = shapetype.AddNewHandles();
            CT_H h = handles.AddNewH();
            h.position=("#0,bottomRight");
            h.xrange=("6629,14971");
            NPOI.OpenXmlFormats.Vml.Office.CT_Lock lock1 = shapetype.AddNewLock();
            lock1.ext=(ST_Ext.edit);
            CT_Shape shape = group.AddNewShape();
            shape.id = ("PowerPlusWaterMarkObject" + idx);
            shape.spid = ("_x0000_s102" + (4 + idx));
            shape.type = ("#_x0000_t136");
            shape.style = ("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
            shape.wrapcoords = ("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068");
            shape.fillcolor = ("black");
            shape.stroked = (ST_TrueFalse.@false);
            CT_TextPath shapeTextPath = shape.AddNewTextpath();
            shapeTextPath.style=("font-family:&quot;Cambria&quot;;font-size:1pt");
            shapeTextPath.@string=(text);
            pict.Set(group);
            // end watermark paragraph
            return new XWPFParagraph(p, doc);
        }
        private void RenderMarker(Marker input, StyleConfig styles, XWPFParagraph parentParagraph = null)
        {
            // Keep track of the previous marker
            previousMarker = thisMarker;
            thisMarker     = input;

            StyleConfig markerStyle = (StyleConfig)styles.Clone();

            switch (input)
            {
            case PMarker _:

                XWPFParagraph paragraph = parentParagraph;
                // If the previous marker was a chapter marker, don't create a new paragraph.
                if (!(previousMarker is CMarker _))
                {
                    XWPFParagraph newParagraph = newDoc.CreateParagraph(markerStyle, configDocx);
                    newParagraph.SetBidi(configDocx.rightToLeft);
                    newParagraph.Alignment      = configDocx.textAlign;
                    newParagraph.SpacingBetween = configDocx.lineSpacing;
                    newParagraph.SpacingAfter   = 200;
                    paragraph = newParagraph;
                }

                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, paragraph);
                }
                break;

            case CLMarker clMarker:
                if (beforeFirstChapter)
                {
                    // A CL before the first chapter means that we should use
                    // this string instead of the word "Chapter".
                    chapterLabel = clMarker.Label;
                }
                break;

            case CMarker cMarker:

                if (beforeFirstChapter)
                {
                    // We found the first chapter, so set the flag to false.
                    beforeFirstChapter = false;
                }
                else
                {
                    if (configDocx.separateChapters)
                    {
                        // Add page break between chapters.
                        newDoc.CreateParagraph().CreateRun().AddBreak(BreakType.PAGE);
                    }
                }

                createBookHeaders(previousBookHeader);

                XWPFParagraph newChapter = newDoc.CreateParagraph(markerStyle, configDocx);
                newChapter.SetBidi(configDocx.rightToLeft);
                newChapter.Alignment      = configDocx.textAlign;
                newChapter.SpacingBetween = configDocx.lineSpacing;
                newChapter.SpacingBefore  = 200;
                newChapter.SpacingAfter   = 200;
                XWPFRun chapterMarker = newChapter.CreateRun(markerStyle);
                setRTL(chapterMarker);
                string simpleNumber = cMarker.Number.ToString();
                if (cMarker.CustomChapterLabel != simpleNumber)
                {
                    // Use the custom label for this section, e.g. "Psalm One" instead of "Chapter 1"
                    currentChapterLabel = cMarker.CustomChapterLabel;
                }
                else
                {
                    // Use the default chapter text for this section, e.g. "Chapter 1"
                    currentChapterLabel = chapterLabel + " " + simpleNumber;
                }
                chapterMarker.SetText(currentChapterLabel);
                chapterMarker.FontSize = (int)(configDocx.fontSize * 1.5);

                XWPFParagraph chapterVerses = newDoc.CreateParagraph(markerStyle, configDocx);
                chapterVerses.SetBidi(configDocx.rightToLeft);
                chapterVerses.Alignment      = configDocx.textAlign;
                chapterVerses.SpacingBetween = configDocx.lineSpacing;
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, chapterVerses);
                }

                RenderCrossReferences(markerStyle);

                break;

            case VMarker vMarker:

                // If there is no parent paragraph, then we're maybe
                // missing a chapter marker prior to this verse.  Let's
                // create a stub parent paragraph so we can keep rendering.
                if (parentParagraph == null)
                {
                    parentParagraph = newDoc.CreateParagraph(markerStyle, configDocx);
                    parentParagraph.SetBidi(configDocx.rightToLeft);
                    parentParagraph.Alignment      = configDocx.textAlign;
                    parentParagraph.SpacingBetween = configDocx.lineSpacing;
                    parentParagraph.SpacingAfter   = 200;
                }

                if (configDocx.separateVerses)
                {
                    XWPFRun newLine = parentParagraph.CreateRun();
                    newLine.AddBreak(BreakType.TEXTWRAPPING);
                }

                markerStyle.fontSize = configDocx.fontSize;
                XWPFRun verseMarker = parentParagraph.CreateRun(markerStyle);
                setRTL(verseMarker);
                verseMarker.SetText(vMarker.VerseCharacter);
                verseMarker.Subscript = VerticalAlign.SUPERSCRIPT;
                AppendSpace(parentParagraph);

                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                if (parentParagraph.Text.EndsWith(" ") == false)
                {
                    AppendSpace(parentParagraph);
                }
                break;

            case QMarker qMarker:
                markerStyle.fontSize = configDocx.fontSize;
                XWPFParagraph poetryParagraph = newDoc.CreateParagraph(markerStyle, configDocx);
                poetryParagraph.SetBidi(configDocx.rightToLeft);
                poetryParagraph.Alignment        = configDocx.textAlign;
                poetryParagraph.SpacingBetween   = configDocx.lineSpacing;
                poetryParagraph.IndentationLeft += qMarker.Depth * 500;
                poetryParagraph.SpacingAfter     = 200;

                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, poetryParagraph);
                }
                break;

            case MMarker mMarker:
                break;

            case TextBlock textBlock:
                markerStyle.fontSize = configDocx.fontSize;
                XWPFRun blockText = parentParagraph.CreateRun(markerStyle);
                setRTL(blockText);
                blockText.SetText(textBlock.Text);
                break;

            case BDMarker bdMarker:
                markerStyle.isBold = true;
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            case HMarker hMarker:

                // Add section header for previous book, if any
                // (section page headers are set at the final paragraph of the section)
                if (previousBookHeader != null)
                {
                    // Create new section and page header
                    createBookHeaders(previousBookHeader);
                    // Print page break
                    XWPFParagraph sectionParagraph = newDoc.CreateParagraph();
                    sectionParagraph.SetBidi(configDocx.rightToLeft);
                    sectionParagraph.Alignment = configDocx.textAlign;
                    sectionParagraph.CreateRun().AddBreak(BreakType.PAGE);
                }
                previousBookHeader = hMarker.HeaderText;

                // Write body header text
                markerStyle.fontSize = (configDocx.fontSize * 2);
                XWPFParagraph newHeader = newDoc.CreateParagraph(markerStyle, configDocx);
                newHeader.Style = "Heading1";     // for TOC pagination

                newHeader.SetBidi(configDocx.rightToLeft);
                newHeader.SpacingAfter = 200;
                XWPFRun headerTitle = newHeader.CreateRun(markerStyle);
                setRTL(headerTitle);
                headerTitle.SetText(hMarker.HeaderText);

                break;

            case FMarker fMarker:
                string footnoteId;
                footnoteId = nextFootnoteNum.ToString();
                nextFootnoteNum++;

                CT_FtnEdn footnote = new CT_FtnEdn();
                footnote.id   = footnoteId;
                footnote.type = ST_FtnEdn.normal;
                StyleConfig footnoteMarkerStyle = (StyleConfig)styles.Clone();
                footnoteMarkerStyle.fontSize = 12;
                CT_P          footnoteParagraph  = footnote.AddNewP();
                XWPFParagraph xFootnoteParagraph = new XWPFParagraph(footnoteParagraph, parentParagraph.Body);
                xFootnoteParagraph.SetBidi(configDocx.rightToLeft);
                footnoteParagraph.AddNewR().AddNewT().Value = "F" + footnoteId.ToString() + " ";
                foreach (Marker marker in fMarker.Contents)
                {
                    RenderMarker(marker, footnoteMarkerStyle, xFootnoteParagraph);
                }
                parentParagraph.Document.AddFootnote(footnote);

                XWPFRun footnoteReferenceRun = parentParagraph.CreateRun();
                setRTL(footnoteReferenceRun);
                CT_RPr rpr = footnoteReferenceRun.GetCTR().AddNewRPr();
                rpr.rStyle     = new CT_String();
                rpr.rStyle.val = "FootnoteReference";
                CT_FtnEdnRef footnoteReference = new CT_FtnEdnRef();
                footnoteReference.id        = footnoteId;
                footnoteReference.isEndnote = false;
                footnoteReferenceRun.SetUnderline(UnderlinePatterns.Single);
                footnoteReferenceRun.AppendText("F");
                footnoteReferenceRun.GetCTR().Items.Add(footnoteReference);
                break;

            case FPMarker fPMarker:
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            case FTMarker fTMarker:

                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            case FRMarker fRMarker:
                markerStyle.isBold = true;
                XWPFRun VerseReference = parentParagraph.CreateRun(markerStyle);
                setRTL(VerseReference);
                VerseReference.SetText(fRMarker.VerseReference);
                break;

            case FKMarker fKMarker:
                XWPFRun FootNoteKeyword = parentParagraph.CreateRun(markerStyle);
                setRTL(FootNoteKeyword);
                FootNoteKeyword.SetText($" {fKMarker.FootNoteKeyword.ToUpper()}: ");
                break;

            case FQMarker fQMarker:
            case FQAMarker fQAMarker:
                markerStyle.isItalics = true;
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            // Cross References
            case XMarker xMarker:
                string crossId;
                switch (xMarker.CrossRefCaller)
                {
                case "-":
                    crossId = "";
                    break;

                case "+":
                    crossId = $"{CrossRefMarkers.Count + 1}";
                    break;

                default:
                    crossId = xMarker.CrossRefCaller;
                    break;
                }
                XWPFRun crossRefMarker = parentParagraph.CreateRun(markerStyle);
                setRTL(crossRefMarker);
                crossRefMarker.SetText(crossId);
                crossRefMarker.Subscript = VerticalAlign.SUPERSCRIPT;

                CrossRefMarkers[crossId] = xMarker;
                break;

            case XOMarker xOMarker:
                markerStyle.isBold = true;
                XWPFRun CrossVerseReference = parentParagraph.CreateRun(markerStyle);
                setRTL(CrossVerseReference);
                CrossVerseReference.SetText($" {xOMarker.OriginRef} ");
                break;

            case XTMarker xTMarker:
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            case XQMarker xQMarker:
                markerStyle.isItalics = true;
                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, parentParagraph);
                }
                break;

            // Table Markers
            case TableBlock table:
                XWPFTable tableContainer = newDoc.CreateTable();

                // Clear Borders
                tableContainer.SetBottomBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");
                tableContainer.SetLeftBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");
                tableContainer.SetRightBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");
                tableContainer.SetTopBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");
                // Clear Inside Borders
                tableContainer.SetInsideHBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");
                tableContainer.SetInsideVBorder(XWPFTable.XWPFBorderType.NONE, 0, 0, "#FFFFFFF");

                foreach (Marker marker in input.Contents)
                {
                    getRenderedRows(marker, markerStyle, tableContainer);
                }
                break;

            case BMarker bMarker:
                XWPFRun newLineBreak = parentParagraph.CreateRun();
                setRTL(newLineBreak);
                newLineBreak.AddBreak(BreakType.TEXTWRAPPING);
                break;

            case IDMarker _:
                // This is the start of a new book.
                beforeFirstChapter  = true;
                chapterLabel        = chapterLabelDefault;
                currentChapterLabel = "";
                break;

            case IPMarker _:
                XWPFParagraph introParagraph = parentParagraph;
                // If the previous marker was a chapter marker, don't create a new paragraph.
                if (!(previousMarker is CMarker _))
                {
                    XWPFParagraph newParagraph = newDoc.CreateParagraph(markerStyle, configDocx);
                    newParagraph.SetBidi(configDocx.rightToLeft);
                    newParagraph.Alignment      = configDocx.textAlign;
                    newParagraph.SpacingBetween = configDocx.lineSpacing;
                    newParagraph.SpacingAfter   = 200;
                    introParagraph = newParagraph;
                }

                foreach (Marker marker in input.Contents)
                {
                    RenderMarker(marker, markerStyle, introParagraph);
                }
                break;

            case XEndMarker _:
            case FEndMarker _:
            case IDEMarker _:
            case VPMarker _:
            case VPEndMarker _:
                break;

            default:
                UnrenderableMarkers.Add(input.Identifier);
                break;
            }
        }
Exemple #19
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);
        }
        /*
         * This is the default Watermark paragraph; the only variable is the text message
         * TODO: manage all the other variables
         */
        private XWPFParagraph GetWatermarkParagraph(String text, int idx)
        {
            CT_P p = new CT_P();

            byte[] rsidr        = doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault;
            p.rsidP        = (rsidr);
            p.rsidRDefault = (rsidrdefault);
            CT_PPr pPr = p.AddNewPPr();

            pPr.AddNewPStyle().val = ("Header");
            // start watermark paragraph
            Npoi.Core.OpenXmlFormats.Wordprocessing.CT_R r = p.AddNewR();
            CT_RPr rPr = r.AddNewRPr();

            rPr.AddNewNoProof();
            CT_Picture pict = r.AddNewPict();

            CT_Group     group     = new CT_Group();
            CT_Shapetype shapetype = group.AddNewShapetype();

            shapetype.id        = ("_x0000_t136");
            shapetype.coordsize = ("1600,21600");
            shapetype.spt       = (136);
            shapetype.adj       = ("10800");
            shapetype.path2     = ("m@7,0l@8,0m@5,21600l@6,21600e");
            CT_Formulas formulas = shapetype.AddNewFormulas();

            formulas.AddNewF().eqn = ("sum #0 0 10800");
            formulas.AddNewF().eqn = ("prod #0 2 1");
            formulas.AddNewF().eqn = ("sum 21600 0 @1");
            formulas.AddNewF().eqn = ("sum 0 0 @2");
            formulas.AddNewF().eqn = ("sum 21600 0 @3");
            formulas.AddNewF().eqn = ("if @0 @3 0");
            formulas.AddNewF().eqn = ("if @0 21600 @1");
            formulas.AddNewF().eqn = ("if @0 0 @2");
            formulas.AddNewF().eqn = ("if @0 @4 21600");
            formulas.AddNewF().eqn = ("mid @5 @6");
            formulas.AddNewF().eqn = ("mid @8 @5");
            formulas.AddNewF().eqn = ("mid @7 @8");
            formulas.AddNewF().eqn = ("mid @6 @7");
            formulas.AddNewF().eqn = ("sum @6 0 @5");
            CT_Path path           = shapetype.AddNewPath();

            path.textpathok    = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            path.connecttype   = (ST_ConnectType.custom);
            path.connectlocs   = ("@9,0;@10,10800;@11,21600;@12,10800");
            path.connectangles = ("270,180,90,0");
            CT_TextPath shapeTypeTextPath = shapetype.AddNewTextpath();

            shapeTypeTextPath.on       = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            shapeTypeTextPath.fitshape = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.t);
            CT_Handles handles = shapetype.AddNewHandles();
            CT_H       h       = handles.AddNewH();

            h.position = ("#0,bottomRight");
            h.xrange   = ("6629,14971");
            Npoi.Core.OpenXmlFormats.Vml.Office.CT_Lock lock1 = shapetype.AddNewLock();
            lock1.ext = (ST_Ext.edit);
            CT_Shape shape = group.AddNewShape();

            shape.id         = ("PowerPlusWaterMarkObject" + idx);
            shape.spid       = ("_x0000_s102" + (4 + idx));
            shape.type       = ("#_x0000_t136");
            shape.style      = ("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin");
            shape.wrapcoords = ("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068");
            shape.fillcolor  = ("black");
            shape.stroked    = (Npoi.Core.OpenXmlFormats.Vml.ST_TrueFalse.@false);
            CT_TextPath shapeTextPath = shape.AddNewTextpath();

            shapeTextPath.style   = ("font-family:&quot;Cambria&quot;;font-size:1pt");
            shapeTextPath.@string = (text);
            pict.Set(group);
            // end watermark paragraph
            return(new XWPFParagraph(p, doc));
        }
        private XWPFParagraph GetWatermarkParagraph(string text, int idx)
        {
            CT_P prgrph = new CT_P();

            byte[] rsidR        = this.doc.Document.body.GetPArray(0).rsidR;
            byte[] rsidRdefault = this.doc.Document.body.GetPArray(0).rsidRDefault;
            prgrph.rsidP        = rsidR;
            prgrph.rsidRDefault = rsidRdefault;
            prgrph.AddNewPPr().AddNewPStyle().val       = "Header";
            NPOI.OpenXmlFormats.Wordprocessing.CT_R ctR = prgrph.AddNewR();
            ctR.AddNewRPr().AddNewNoProof();
            CT_Picture   ctPicture   = ctR.AddNewPict();
            CT_Group     ctGroup     = new CT_Group();
            CT_Shapetype ctShapetype = ctGroup.AddNewShapetype();

            ctShapetype.id        = "_x0000_t136";
            ctShapetype.coordsize = "1600,21600";
            ctShapetype.spt       = 136f;
            ctShapetype.adj       = "10800";
            ctShapetype.path2     = "m@7,0l@8,0m@5,21600l@6,21600e";
            CT_Formulas ctFormulas = ctShapetype.AddNewFormulas();

            ctFormulas.AddNewF().eqn = "sum #0 0 10800";
            ctFormulas.AddNewF().eqn = "prod #0 2 1";
            ctFormulas.AddNewF().eqn = "sum 21600 0 @1";
            ctFormulas.AddNewF().eqn = "sum 0 0 @2";
            ctFormulas.AddNewF().eqn = "sum 21600 0 @3";
            ctFormulas.AddNewF().eqn = "if @0 @3 0";
            ctFormulas.AddNewF().eqn = "if @0 21600 @1";
            ctFormulas.AddNewF().eqn = "if @0 0 @2";
            ctFormulas.AddNewF().eqn = "if @0 @4 21600";
            ctFormulas.AddNewF().eqn = "mid @5 @6";
            ctFormulas.AddNewF().eqn = "mid @8 @5";
            ctFormulas.AddNewF().eqn = "mid @7 @8";
            ctFormulas.AddNewF().eqn = "mid @6 @7";
            ctFormulas.AddNewF().eqn = "sum @6 0 @5";
            CT_Path ctPath           = ctShapetype.AddNewPath();

            ctPath.textpathok    = ST_TrueFalse.t;
            ctPath.connecttype   = ST_ConnectType.custom;
            ctPath.connectlocs   = "@9,0;@10,10800;@11,21600;@12,10800";
            ctPath.connectangles = "270,180,90,0";
            CT_TextPath ctTextPath1 = ctShapetype.AddNewTextpath();

            ctTextPath1.on       = ST_TrueFalse.t;
            ctTextPath1.fitshape = ST_TrueFalse.t;
            CT_H ctH = ctShapetype.AddNewHandles().AddNewH();

            ctH.position = "#0,bottomRight";
            ctH.xrange   = "6629,14971";
            ctShapetype.AddNewLock().ext = ST_Ext.edit;
            CT_Shape ctShape             = ctGroup.AddNewShape();

            ctShape.id         = "PowerPlusWaterMarkObject" + (object)idx;
            ctShape.spid       = "_x0000_s102" + (object)(4 + idx);
            ctShape.type       = "#_x0000_t136";
            ctShape.style      = "position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin";
            ctShape.wrapcoords = "616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068";
            ctShape.fillcolor  = "black";
            ctShape.stroked    = ST_TrueFalse.@false;
            CT_TextPath ctTextPath2 = ctShape.AddNewTextpath();

            ctTextPath2.style   = "font-family:&quot;Cambria&quot;;font-size:1pt";
            ctTextPath2.@string = text;
            ctPicture.Set((object)ctGroup);
            return(new XWPFParagraph(prgrph, (IBody)this.doc));
        }