/// <summary>
        /// Creates an empty header for front pages.
        /// The returned paragraph should be inserted in front of document
        /// </summary>
        /// <example>xwpfDoc.Document.body.Items.Insert(1, CreateFrontHeader());</example>
        /// <returns>CT_P paragraph that contains a blank header</returns>
        private CT_P CreateFrontHeader()
        {
            CT_Hdr header          = new CT_Hdr();
            CT_P   headerParagraph = header.AddNewP();

            headerParagraph.AddNewPPr();

            XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount);

            documentHeader.SetHeaderFooter(header);

            // Create new section and set its header
            CT_P      p          = new CT_P();
            CT_SectPr newSection = p.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;

            // Increment page header count so each one gets a unique ID
            pageHeaderCount++;

            return(p);
        }
Exemple #2
0
        private static void CreateHeader(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_Hdr header = new CT_Hdr();
            //header.AddNewP().AddNewR().AddNewT().Value = headerText;
            var hp = header.AddNewP();

            //byte[] data = Encoding.ASCII.GetBytes("00B80FAA");
            hp.rsidR        = new byte[] { 0, 184, 15, 170 };
            hp.rsidRDefault = new byte[] { 0, 146, 87, 240 };
            hp.rsidP        = new byte[] { 0, 184, 15, 170 };

            var ppr  = hp.AddNewPPr();
            var ctjc = new CT_Jc();

            ctjc.val = ST_Jc.right;
            ppr.jc   = ctjc;

            var hr = hp.AddNewR().AddNewT().Value = headerText;

            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);

            docHeader.SetXWPFDocument(doc);
            XWPFFooter footer = new XWPFFooter();

            //设置页眉
            docHeader.SetHeaderFooter(header);
            if (!string.IsNullOrEmpty(imageUrl) && File.Exists(imageUrl))
            {
                var pictureData = new FileStream(imageUrl, FileMode.Open, FileAccess.Read);
                docHeader.AddPictureData(pictureData, (int)PictureType.PNG);
            }
            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = docHeader.GetPackageRelationship().Id;
        }
Exemple #3
0
        public XWPFParagraph CreateParagraph()
        {
            XWPFParagraph p = new XWPFParagraph(header.AddNewP(), XWPFHeader);

            return(p);
        }
Exemple #4
0
        private static void CreateWaterMark(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_Hdr header = new CT_Hdr();
            //header.AddNewP().AddNewR().AddNewT().Value = headerText;
            var paragraph = header.AddNewP();

            var ppr = paragraph.AddNewPPr();

            ppr.AddNewPStyle().val = "a3";
            ppr.AddNewJc().val     = ST_Jc.right;

            var r = paragraph.AddNewR();

            r.AddNewRPr().AddNewNoProof();

            CT_Picture pict = r.AddNewPict();

            CT_Shapetype shapetype = new CT_Shapetype();

            shapetype.id        = ("_x0000_t136");
            shapetype.coordsize = ("21600,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.OpenXmlFormats.Vml.ST_TrueFalse.t);
            path.connecttype   = (NPOI.OpenXmlFormats.Vml.Office.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.OpenXmlFormats.Vml.ST_TrueFalse.t);
            shapeTypeTextPath.fitshape = (NPOI.OpenXmlFormats.Vml.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);
            lock1.text      = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t;
            lock1.shapetype = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t;

            CT_Shape shape = new CT_Shape();

            shape.id    = ("PowerPlusWaterMarkObject" + "1109904797");
            shape.spid  = ("_x0000_s206" + (4 + "1"));
            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 = ("#ed7d31 [3205]");
            shape.stroked   = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.@false);

            shape.AddNewFill().opacity = ".5";
            CT_TextPath shapeTextPath  = shape.AddNewTextpath();

            shapeTextPath.style   = ("font-family:&quotYu Gothic UI&quot");
            shapeTextPath.@string = ("吴润杰水印");

            // pict.Items.Add(shapetype);
            //pict.Set(shapetype);
            pict.Set(shape);
            //pict.Set(group);


            XWPFRelation Hrelation = XWPFRelation.HEADER;
            XWPFHeader   docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);

            docHeader.SetXWPFDocument(doc);
            //设置页眉
            docHeader.SetHeaderFooter(header);
            CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();

            m_HdrFtr2.type = ST_HdrFtr.@default;
            m_HdrFtr2.id   = docHeader.GetPackageRelationship().Id;
        }
        /// <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 #6
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;
        }