Ejemplo n.º 1
0
        /// <summary>
        /// 设置段落对齐方式
        /// </summary>
        /// <param name="p"></param>
        /// <param name="align"></param>
        public static void SetAlign(this CT_P p, ST_Jc align)
        {
            CT_PPr ppr = p.pPr == null?p.AddNewPPr() : p.pPr;

            CT_Jc jc = ppr.IsSetJc() ? ppr.jc : ppr.AddNewJc();

            jc.val = align;
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 3
0
        public void TestSetAlignment()
        {
            //new clean instance of paragraph
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            Assert.AreEqual(ParagraphAlignment.LEFT, p.Alignment);

            CT_P   ctp = p.GetCTP();
            CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr;

            CT_Jc align = ppr.AddNewJc();

            align.val = (ST_Jc.center);
            Assert.AreEqual(ParagraphAlignment.CENTER, p.Alignment);

            p.Alignment = (ParagraphAlignment.BOTH);
            Assert.AreEqual((int)ST_Jc.both, (int)ppr.jc.val);
        }
Ejemplo n.º 4
0
 public CT_Lvl()
 {
     this.rPrField = new CT_RPr();
     this.pPrField = new CT_PPr();
     this.lvlJcField = new CT_Jc();
     //this.legacyField = new CT_LvlLegacy();
     //this.lvlPicBulletIdField = new CT_DecimalNumber();
     this.lvlTextField = new CT_LevelText();
     //this.suffField = new CT_LevelSuffix();
     //this.isLglField = new CT_OnOff();
     //this.pStyleField = new CT_String();
     //this.lvlRestartField = new CT_DecimalNumber();
     this.numFmtField = new CT_NumFmt();
     this.startField = new CT_DecimalNumber();
 }
        /// <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++;
        }