Esempio n. 1
0
        /// <summary>
        /// 创建标题段
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="text">标题文字</param>
        /// <param name="level">标题级别</param>
        /// <returns></returns>
        public static XWPFRun CreateHeading(this XWPFDocument doc, string text, int level)
        {
            XWPFRun run = doc.CreateParagraph(text);

            run.Paragraph.Style = GetHeadingId(level);
            return(run);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            XWPFDocument  doc  = new XWPFDocument();
            XWPFParagraph para = doc.CreateParagraph();
            XWPFRun       r0   = para.CreateRun();

            r0.SetText("Title1");
            para.BorderTop           = Borders.Thick;
            para.FillBackgroundColor = "EEEEEE";
            para.FillPattern         = NPOI.OpenXmlFormats.Wordprocessing.ST_Shd.diagStripe;

            XWPFTable table = doc.CreateTable(3, 3);

            table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE");

            XWPFTableCell c1 = table.GetRow(0).GetCell(0);
            XWPFParagraph p1 = c1.AddParagraph();   //don't use doc.CreateParagraph
            XWPFRun       r1 = p1.CreateRun();

            r1.SetText("The quick brown fox");
            r1.IsBold = true;

            r1.FontFamily = "Courier";
            r1.SetUnderline(UnderlinePatterns.DotDotDash);
            r1.SetTextPosition(100);
            c1.SetColor("FF0000");


            table.GetRow(2).GetCell(2).SetText("only text");

            FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);

            doc.Write(out1);
            out1.Close();
        }
Esempio n. 3
0
        public byte[] CreateWord(Dictionary <string, string> paragraphs)
        {
            if (paragraphs == null)
            {
                throw new ArgumentNullException(nameof(paragraphs));
            }

            var doc = new XWPFDocument();

            try
            {
                //创建段落对象
                foreach (var key in paragraphs.Keys)
                {
                    XWPFParagraph paragraph = doc.CreateParagraph();
                    XWPFRun       run       = paragraph.CreateRun();
                    run.IsBold = true;
                    run.SetText(ConvertHelper.GetString(key));
                    run.SetText(ConvertHelper.GetString(paragraphs[key]));
                }

                using (var stream = new MemoryStream())
                {
                    doc.Write(stream);
                    var bs = stream.ToArray();
                    return(bs);
                }
            }
            finally
            {
                doc.Close();
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            XWPFDocument doc = new XWPFDocument();

            XWPFTable table = doc.CreateTable(3, 3);

            table.GetRow(1).GetCell(1).SetText("EXAMPLE OF TABLE");


            XWPFParagraph p1 = doc.CreateParagraph();

            XWPFRun r1 = p1.CreateRun();

            r1.SetBold(true);
            r1.SetText("The quick brown fox");
            r1.SetBold(true);
            r1.SetFontFamily("Courier");
            r1.SetUnderline(UnderlinePatterns.DotDotDash);
            r1.SetTextPosition(100);

            table.GetRow(0).GetCell(0).SetParagraph(p1);


            table.GetRow(2).GetCell(2).SetText("only text");

            FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);

            doc.Write(out1);
            out1.Close();
        }
 public static void CreatePictureCxCy(this XWPFDocument document, string blipId, int id, long cx, long cy)
 {
     CT_Inline inline = document.CreateParagraph().CreateRun().GetCTR().AddNewDrawing().AddNewInline();
     string    picXml = "" +
                        "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
                        "   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                        "      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
                        "         <pic:nvPicPr>" +
                        "            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
                        "            <pic:cNvPicPr/>" +
                        "         </pic:nvPicPr>" +
                        "         <pic:blipFill>" +
                        "            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
                        "            <a:stretch>" +
                        "               <a:fillRect/>" +
                        "            </a:stretch>" +
                        "         </pic:blipFill>" +
                        "         <pic:spPr>" +
                        "            <a:xfrm>" +
                        "               <a:off x=\"0\" y=\"0\"/>" +
                        "               <a:ext cx=\"" + cx + "\" cy=\"" + cy + "\"/>" +
                        "            </a:xfrm>" +
                        "            <a:prstGeom prst=\"rect\">" +
                        "               <a:avLst/>" +
                        "            </a:prstGeom>" +
                        "         </pic:spPr>" +
                        "      </pic:pic>" +
                        "   </a:graphicData>" +
                        "</a:graphic>";
 }
        /// <summary>
        /// 创建word文档中的段落对象和设置段落文本的基本样式(字体大小,字体,字体颜色,字体对齐位置)
        /// </summary>
        /// <param name="document">document文档对象</param>
        /// <param name="fillContent">段落第一个文本对象填充的内容</param>
        /// <param name="isBold">是否加粗</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="fontFamily">字体</param>
        /// <param name="paragraphAlign">段落排列(左对齐,居中,右对齐)</param>
        /// <param name="isStatement">是否在同一段落创建第二个文本对象(解决同一段落里面需要填充两个或者多个文本值的情况,多个文本需要自己拓展,现在最多支持两个)</param>
        /// <param name="secondFillContent">第二次声明的文本对象填充的内容,样式与第一次的一致</param>
        /// <returns></returns>
        private static XWPFParagraph ParagraphInstanceSetting(XWPFDocument document, string fillContent, bool isBold, int fontSize, string fontFamily, ParagraphAlignment paragraphAlign, bool isStatement = false, string secondFillContent = "")
        {
            XWPFParagraph paragraph = document.CreateParagraph();  //创建段落对象

            paragraph.Alignment = paragraphAlign;                  //文字显示位置,段落排列(左对齐,居中,右对齐)

            XWPFRun xwpfRun = paragraph.CreateRun();               //创建段落文本对象

            xwpfRun.IsBold = isBold;                               //文字加粗
            xwpfRun.SetText(fillContent);                          //填充内容
            xwpfRun.FontSize = fontSize;                           //设置文字大小
            xwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定

            if (isStatement)
            {
                XWPFRun secondxwpfRun = paragraph.CreateRun();               //创建段落文本对象
                secondxwpfRun.IsBold = isBold;                               //文字加粗
                secondxwpfRun.SetText(secondFillContent);                    //填充内容
                secondxwpfRun.FontSize = fontSize;                           //设置文字大小
                secondxwpfRun.SetFontFamily(fontFamily, FontCharRange.None); //设置标题样式如:(微软雅黑,隶书,楷体)根据自己的需求而定
            }


            return(paragraph);
        }
Esempio n. 7
0
        public string Test()
        {
            string       unique_file_name = Guid.NewGuid().ToString() + ".docx";
            string       file_path        = Path.Combine("wwwroot", "awards", unique_file_name);
            XWPFDocument doc = new XWPFDocument();
            // 添加段落
            XWPFParagraph gp = doc.CreateParagraph();

            gp.Alignment = ParagraphAlignment.CENTER;//水平居中
            XWPFRun gr = gp.CreateRun();

            gr.GetCTR().AddNewRPr().AddNewRFonts().ascii    = "黑体";
            gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = "黑体";
            //gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia;
            gr.GetCTR().AddNewRPr().AddNewSz().val    = (ulong)44; //2号字体
            gr.GetCTR().AddNewRPr().AddNewSzCs().val  = (ulong)44;
            gr.GetCTR().AddNewRPr().AddNewB().val     = true;      //加粗
            gr.GetCTR().AddNewRPr().AddNewColor().val = "red";     //字体颜色
            gr.SetText("NPOI创建Word2007Docx");

            FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write);

            doc.Write(fs);
            Console.WriteLine("生成word成功");

            return("ok admin/test");
        }
Esempio n. 8
0
        public static byte[] CreateWord(Dictionary <string, string> paragraphs)
        {
            var doc = new XWPFDocument();

            try
            {            //创建段落对象
                paragraphs = paragraphs ?? new Dictionary <string, string>()
                {
                };
                paragraphs.Keys.ToList().ForEach(key =>
                {
                    XWPFParagraph paragraph = doc.CreateParagraph();
                    XWPFRun run             = paragraph.CreateRun();
                    run.IsBold = true;
                    run.SetText(ConvertHelper.GetString(key));
                    run.SetText(ConvertHelper.GetString(paragraphs[key]));
                });
                using (var stream = new MemoryStream())
                {
                    doc.Write(stream);
                    var bs = stream.ToArray();
                    return(bs);
                }
            }
            finally
            {
                doc.Close();
            }
        }
Esempio n. 9
0
        public void TestSetParagraphStyle()
        {
            //new clean instance of paragraph
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("heading123.docx");
            XWPFParagraph p   = doc.CreateParagraph();
            XWPFRun       run = p.CreateRun();

            run.SetText("Heading 1");

            CT_SdtBlock block = doc.Document.body.AddNewSdt();

            Assert.IsNull(p.Style);
            p.Style = HEADING1;
            Assert.AreEqual(HEADING1, p.GetCTP().pPr.pStyle.val);

            doc.CreateTOC();

            /*
             * // TODO - finish this test
             * if (false) {
             *  CTStyles styles = doc.Style;
             *  CTStyle style = styles.AddNewStyle();
             *  style.Type=(STStyleType.PARAGRAPH);
             *  style.StyleId=("Heading1");
             * }
             *
             * if (false) {
             *  File file = TempFile.CreateTempFile("testHeaders", ".docx");
             *  OutputStream out1 = new FileOutputStream(file);
             *  doc.Write(out1);
             *  out1.Close();
             * }
             */
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            XWPFDocument doc  = new XWPFDocument();
            var          p    = doc.CreateParagraph();
            var          math = p.CreateOMath();

            var acc = math.CreateAcc();

            acc.AccPr = "¯";
            acc.Element.CreateRun().SetText("X");
            math.CreateRun().SetText("=");
            var f = math.CreateF();

            f.FractionType = ST_FType.bar;
            f.Denominator.CreateRun().SetText("n");
            var nary = f.Numerator.CreateNary().SetSumm();

            nary.Superscript.CreateRun().SetText("n");
            nary.Subscript.CreateRun().SetText("i=1");
            var ssub = nary.Element.CreateSSub();

            ssub.Element.CreateRun().SetText("X");
            ssub.Subscript.CreateRun().SetText("i");

            FileStream out1 = new FileStream("Xavg.docx", FileMode.Create);

            doc.Write(out1);
            out1.Close();
        }
Esempio n. 11
0
        public void Bug55802()
        {
            String blabla =
                "Bir, iki, \u00fc\u00e7, d\u00f6rt, be\u015f,\n" +
                "\nalt\u0131, yedi, sekiz, dokuz, on.\n" +
                "\nK\u0131rm\u0131z\u0131 don,\n" +
                "\ngel bizim bah\u00e7eye kon,\n" +
                "\nsar\u0131 limon";
            XWPFDocument doc = new XWPFDocument();
            XWPFRun      run = doc.CreateParagraph().CreateRun();

            foreach (String str in blabla.Split("\n".ToCharArray()))
            {
                run.SetText(str);
                run.AddBreak();
            }

            run.FontFamily = (/*setter*/ "Times New Roman");
            run.FontSize   = (/*setter*/ 20);
            Assert.AreEqual(run.FontFamily, "Times New Roman");
            Assert.AreEqual(run.GetFontFamily(FontCharRange.CS), "Times New Roman");
            Assert.AreEqual(run.GetFontFamily(FontCharRange.EastAsia), "Times New Roman");
            Assert.AreEqual(run.GetFontFamily(FontCharRange.HAnsi), "Times New Roman");
            run.SetFontFamily("Arial", FontCharRange.HAnsi);
            Assert.AreEqual(run.GetFontFamily(FontCharRange.HAnsi), "Arial");
        }
Esempio n. 12
0
        private static void CreateDoc()
        {
            var widthEmus  = (int)(30 * 9525);
            var heightEmus = (int)(20 * 9525);

            XWPFDocument doc = new XWPFDocument();//创建文档

            using (FileStream picData = new FileStream("headerImage.png", FileMode.Open, FileAccess.Read))
            {
                doc.CreateParagraph().CreateRun().AddPicture(picData, (int)PictureType.PNG, "headerImage", widthEmus, heightEmus);
            }

            V5XWPFHeader v5XWPFHeader = new V5XWPFHeader(doc);
            var          p            = v5XWPFHeader.CreateParagraph();

            p.CreateRun().SetText("wurunjie");


            using (FileStream picData = new FileStream("HumpbackWhale.jpg", FileMode.Open, FileAccess.Read))
            {
                //v5XWPFHeader.XWPFHeader.AddPictureData(picData, (int)PictureType.PNG);
                //v5XWPFHeader.XWPFHeader.
                //p.CreateRun().AddPicture(picData, (int)PictureType.PNG, "HumpbackWhale", widthEmus, heightEmus);
                v5XWPFHeader.AddPicture(picData, (int)PictureType.PNG, "HumpbackWhale", widthEmus, heightEmus, p.CreateRun());
            }
            WriteFile(doc);
        }
        public static XWPFParagraph CreateParagraph(this XWPFDocument doc, StyleConfig styles)
        {
            XWPFParagraph para = doc.CreateParagraph();

            para.Alignment = (styles.isAlignRight ? ParagraphAlignment.RIGHT : ParagraphAlignment.LEFT);
            return(para);
        }
Esempio n. 14
0
        protected XWPFPicture GetPicture(XWPFDocument document, Bitmap bmp)
        {
            var para = document.CreateParagraph();
            var run  = para.CreateRun();

            return(GetPicture(run, bmp));
        }
Esempio n. 15
0
        protected void InsertSectionPageBreak(XWPFDocument document)
        {
            var pageBreak    = document.CreateParagraph();
            var pageBreakRun = pageBreak.CreateRun();

            pageBreakRun.AddBreak(BreakType.PAGE);
        }
Esempio n. 16
0
        public void TestIntegration()
        {
            XWPFDocument doc = new XWPFDocument();

            XWPFParagraph p1 = doc.CreateParagraph();

            XWPFRun r1 = p1.CreateRun();

            r1.SetText("Lorem ipsum dolor sit amet.");
            doc.EnforceCommentsProtection();

            FileInfo tempFile = TempFile.CreateTempFile("documentProtectionFile", ".docx");

            if (File.Exists(tempFile.FullName))
            {
                File.Delete(tempFile.FullName);
            }
            Stream out1 = new FileStream(tempFile.FullName, FileMode.CreateNew);

            doc.Write(out1);
            out1.Dispose();

            FileStream   inputStream = new FileStream(tempFile.FullName, FileMode.Open);
            XWPFDocument document    = new XWPFDocument(inputStream);

            inputStream.Dispose();

            Assert.IsTrue(document.IsEnforcedCommentsProtection());
        }
Esempio n. 17
0
        public static void WriteContent(this XWPFDocument doc, string content)
        {
            var p = doc.CreateParagraph();
            var r = p.CreateRun();

            r.FontSize = 16;
            r.SetText("\t\t" + content);
        }
Esempio n. 18
0
        public void TestGetSetILvl()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            p.SetNumILvl("1");
            Assert.AreEqual("1", p.GetNumIlvl());
        }
        public static XWPFParagraph CreateParagraph(this XWPFDocument doc, StyleConfig styles, DocxConfig config)
        {
            XWPFParagraph para = doc.CreateParagraph(styles);

            para.IndentationLeft  = config.marginLeft * UnitValue.TWIP_PER_CM;
            para.IndentationRight = config.marginRight * UnitValue.TWIP_PER_CM;
            return(para);
        }
Esempio n. 20
0
        /// <summary>
        /// 创建普通文本段
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static XWPFRun CreateParagraph(this XWPFDocument doc, string text)
        {
            XWPFParagraph paragraph = doc.CreateParagraph();
            XWPFRun       run       = paragraph.CreateRun();

            run.SetText(text);
            return(run);
        }
Esempio n. 21
0
        public void TestSearchTextNotFound()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            Assert.IsNull(p.SearchText("test", new PositionInParagraph()));
            Assert.AreEqual("", p.Text);
        }
Esempio n. 22
0
        public void SetUp()
        {
            XWPFDocument doc = new XWPFDocument();

            p = doc.CreateParagraph();

            this.ctRun = new CT_R();
        }
Esempio n. 23
0
        /*
         *插入空行函数
         *@n 插入的行数
         *@m_Docx 根文档
         *@longth 空行高
         */

        private static void word_insert_space(int n, XWPFDocument m_Docx, int longth = 250)
        {
            for (int i = 0; i < n; i++)
            {
                XWPFParagraph gp_space = m_Docx.CreateParagraph(); //创建XWPFParagraph
                gp_space.SetAlignment(ParagraphAlignment.CENTER);
                gp_space.SetSpacingBefore(longth);
                gp_space.SpacingAfter = longth;
            }
        }
Esempio n. 24
0
        static void Main(string[] args)
        {
            XWPFDocument doc = new XWPFDocument();

            doc.CreateParagraph();
            using (FileStream sw = File.Create("blank.docx"))
            {
                doc.Write(sw);
            }
        }
Esempio n. 25
0
        protected void InsertHeader(XWPFDocument document, string htext, int headSize = 1)
        {
            var     h  = document.CreateParagraph();
            XWPFRun r0 = h.CreateRun();

            r0.FontSize = GetSize(headSize);

            //file data
            r0.SetText(htext ?? "");
        }
Esempio n. 26
0
        protected void InsertParagraph(XWPFDocument document, string ptext, int textFontSize = -1)
        {
            var     h  = document.CreateParagraph();
            XWPFRun r0 = h.CreateRun();

            //file data
            r0.SetText(ptext ?? "");

            r0.FontSize = textFontSize != -1 ? textFontSize : 10;
        }
Esempio n. 27
0
        public static void WriteTitle(this XWPFDocument doc, string title, string styleId = null, ParagraphAlignment alignment = ParagraphAlignment.CENTER)
        {
            var p = doc.CreateParagraph();

            p.Alignment = alignment;
            var r = p.CreateRun();

            r.SetText(title);
            p.Style = styleId;
        }
Esempio n. 28
0
        static void Main(string[] args)
        {
            XWPFDocument doc = new XWPFDocument();

            doc.CreateParagraph();
            FileStream sw = File.OpenWrite("blank.docx");

            doc.Write(sw);
            sw.Close();
        }
Esempio n. 29
0
        public void Main()
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            XWPFDocument doc = new XWPFDocument();

            doc.CreateParagraph();
            using (FileStream sw = File.Create("blank.docx"))
            {
                doc.Write(sw);
            }
        }
Esempio n. 30
0
        //插入一段文字
        private static void word_insert_text(XWPFDocument m_Docx, string Font, int size, string text, ParagraphAlignment position = ParagraphAlignment.CENTER)
        {
            XWPFParagraph gp = m_Docx.CreateParagraph();

            gp.SetAlignment(position);
            XWPFRun gr = gp.CreateRun();

            gr.SetFontFamily(Font);
            gr.SetFontSize(size);
            gr.SetText(text);
        }