Esempio n. 1
0
        private void AppendParagraph(XWPFTableCell cell, String cellText)
        {
            XWPFParagraph paragraph = cell.AddParagraph();
            XWPFRun       run       = paragraph.CreateRun();

            run.FontFamily = "宋体";
            //设置字体大小
            run.FontSize = 11;
            //字体是否加粗,这里加粗了
            run.SetBold(true);
            run.SetText(cellText);
        }
Esempio n. 2
0
        void SetRedFontCell(XWPFTableCell cell, string value)
        {
            XWPFParagraph pIO = cell.AddParagraph();

            pIO.Alignment = ParagraphAlignment.CENTER;
            XWPFRun rIO = pIO.CreateRun();

            rIO.SetColor("red");
            rIO.SetText(value);
            SetAlign(cell);
            cell.RemoveParagraph(0);
        }
Esempio n. 3
0
        private static void AnalysisXWPFRun(XWPFRun run)
        {
            var picts = run.GetEmbeddedPictures();

            foreach (var pict in picts)
            {
                var ctp      = pict.GetCTPicture();
                var width    = ctp.spPr.xfrm.ext.cx;
                var height   = ctp.spPr.xfrm.ext.cy;
                var pictData = pict.GetPictureData();
            }
        }
        //private void SetText(XWPFTable tb, int row, int column, string text, ParagraphAlignment paragraphAlignment = ParagraphAlignment.LEFT)
        //{
        //    tb.GetRow(row).GetCell(column).SetText(text);
        //}

        private void SetText(XWPFTable tb, int row, int column, String cellText, ParagraphAlignment paragraphAlignment = ParagraphAlignment.LEFT)
        {
            var           cell = tb.GetRow(row).GetCell(column);
            XWPFParagraph p0   = new XWPFParagraph(new CT_P(), cell);

            p0.Alignment = paragraphAlignment;
            XWPFRun r0 = p0.CreateRun();

            r0.FontFamily = "宋体";
            r0.FontSize   = 11;
            r0.SetText(cellText);
            cell.SetParagraph(p0);
        }
Esempio n. 5
0
        /// <summary>
        /// 追加段落
        /// </summary>
        /// <param name="value">内容</param>
        /// <param name="fontFamily">字体</param>
        /// <param name="fontSize">字体大小</param>
        /// <param name="isBold">是否加粗</param>
        /// <param name="paragraphAlignmentEnum">段落对其</param>
        /// <returns></returns>
        public WordBuilder AppendParagraph(string value, string fontFamily, int fontSize, bool isBold, ParagraphAlignmentEnum paragraphAlignmentEnum)
        {
            XWPFParagraph p = doc.CreateParagraph();

            p.Alignment = (ParagraphAlignment)paragraphAlignmentEnum;
            XWPFRun r = p.CreateRun();

            r.FontFamily = fontFamily;
            r.FontSize   = fontSize;
            r.IsBold     = isBold;
            r.SetText(value);
            return(this);
        }
Esempio n. 6
0
        public WordBuilder(string title, FontStyle fontStyle)
        {
            doc = new XWPFDocument();
            XWPFParagraph p = doc.CreateParagraph();

            p.Alignment = (ParagraphAlignment)fontStyle.ParagraphAlignment;
            XWPFRun r = p.CreateRun();

            r.FontFamily = fontStyle.FontFamily;
            r.FontSize   = fontStyle.FontSize;
            r.IsBold     = fontStyle.IsBold;
            r.SetText(title);
        }
Esempio n. 7
0
        public WordBuilder(string title)
        {
            doc = new XWPFDocument();
            XWPFParagraph p = doc.CreateParagraph();

            p.Alignment = ParagraphAlignment.CENTER;
            XWPFRun r = p.CreateRun();

            r.FontFamily = DEFAULT_FONTFAMILY;
            r.FontSize   = 18;
            r.IsBold     = true;
            r.SetText(title);
        }
Esempio n. 8
0
        private static XWPFRun TryGetNextRun(XWPFRun run)
        {
            int index = run.Paragraph.Runs.IndexOf(run);

            if (index >= run.Paragraph.Runs.Count - 1)
            {
                return(null);
            }
            else
            {
                return(run.Paragraph.Runs[index + 1]);
            }
        }
Esempio n. 9
0
        public void TestSetGetItalic()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewI().val = true;

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(true, run.IsItalic);

            run.IsItalic = false;
            Assert.AreEqual(false, rpr.i.val);
        }
Esempio n. 10
0
        public void TestSetGetTextForegroundBackground()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewPosition().val = "4000";

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(4000, run.TextPosition);

            run.TextPosition = (2400);
            Assert.AreEqual(2400, int.Parse(rpr.position.val));
        }
Esempio n. 11
0
        public void TestSetGetVAlign()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewVertAlign().val = (ST_VerticalAlignRun.subscript);

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(VerticalAlign.SUBSCRIPT, run.Subscript);

            run.Subscript = (VerticalAlign.BASELINE);
            Assert.AreEqual(ST_VerticalAlignRun.baseline, rpr.vertAlign.val);
        }
Esempio n. 12
0
        public void TestSetGetFontFamily()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewRFonts().ascii = ("Times New Roman");

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual("Times New Roman", run.FontFamily);

            run.FontFamily = ("Verdana");
            Assert.AreEqual("Verdana", rpr.rFonts.ascii);
        }
Esempio n. 13
0
        public void TestSetGetStrike()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewStrike().val = true;

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(true, run.IsStrike);

            run.SetStrike(false);
            Assert.AreEqual(false, rpr.strike.val);
        }
Esempio n. 14
0
        public void TestSetGetUnderline()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewU().val = (ST_Underline.dash);

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(UnderlinePatterns.Dash, run.Underline);

            run.SetUnderline(UnderlinePatterns.None);
            Assert.AreEqual(ST_Underline.none, rpr.u.val);
        }
Esempio n. 15
0
        public void TestSetGetFontSize()
        {
            CT_RPr rpr = ctRun.AddNewRPr();

            rpr.AddNewSz().val = 14;

            XWPFRun run = new XWPFRun(ctRun, p);

            Assert.AreEqual(7, run.FontSize);

            run.FontSize = (24);
            Assert.AreEqual(48, (int)rpr.sz.val);
        }
Esempio n. 16
0
        /// <summary>
        /// 将旧文本域的样式拷贝至新文本域
        /// </summary>
        /// <param name="fromRun"></param>
        /// <param name="toRun"></param>
        private static void CopyRunStyle(XWPFRun fromRun, XWPFRun toRun)
        {
            Type type = typeof(XWPFRun);

            PropertyInfo[] props = type.GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (prop.CanWrite)
                {
                    prop.SetValue(toRun, prop.GetValue(fromRun));
                }
            }
        }
Esempio n. 17
0
        public static void WriteWord()
        {
            XWPFDocument  doc  = new XWPFDocument();
            XWPFParagraph para = doc.CreateParagraph();

            XWPFRun run = para.CreateRun();

            run.SetText("aaaa");
            FileStream sw = new FileStream(@"D:\工作夹\刘涛\新建文件夹\1111.docx", FileMode.Create);

            doc.Write(sw);
            sw.Close();
        }
        private static void WriteMedia(XWPFDocument doc, MediaItem media)
        {
            var p2 = doc.CreateParagraph();

            p2.Style = "Heading2";  // 专辑名
            XWPFRun r2 = p2.CreateRun();

            // r2.FontSize = 14;
            // r2.IsBold = true;
            r2.SetText($"{media.Title}");
            // doc.CreateParagraph().CreateRun().SetText(media.Url);
            doc.CreateParagraph().CreateRun().SetText(media.Description);
            doc.CreateParagraph().CreateRun().SetText("");

            foreach (var mp3Item in media.Mp3Items)
            {
                var lyricTitlePara = doc.CreateParagraph();
                lyricTitlePara.Style = "LyricTitle";
                var title = lyricTitlePara.CreateRun();
                title.SetText(mp3Item.Title);
                //title.SetColor("#FA6922");
                //title.FontSize = 12;
                //title.IsBold = true;

                if (!string.IsNullOrEmpty(mp3Item.LyricTitle))
                {
                    // 绿色歌词标题
                    var p4 = doc.CreateParagraph();
                    p4.Style = "LyricInfo";
                    var lyricTitle = p4.CreateRun();
                    lyricTitle.SetText(mp3Item.LyricTitle);
                    //lyricTitle.SetColor("#006400");
                    //lyricTitle.IsBold = true;
                }

                if (!string.IsNullOrEmpty(mp3Item.Lyric))
                {
                    var lyricPara = doc.CreateParagraph();
                    lyricPara.Style = "Lyric";
                    lyricPara.CreateRun().SetText(mp3Item.Lyric);
                }

                // if (!string.IsNullOrEmpty(mp3Item.Url))
                // {
                //     doc.CreateParagraph().CreateRun();
                //     doc.CreateParagraph().CreateRun().SetText($"({mp3Item.Url})");
                // }

                doc.CreateParagraph().CreateRun();
            }
        }
Esempio n. 19
0
        public void testSetGetHighlight()
        {
            XWPFRun run = p.CreateRun();

            Assert.AreEqual(false, run.IsHighlighted);

            // TODO Do this using XWPFRun methods
            run.GetCTR().AddNewRPr().AddNewHighlight().val = (ST_HighlightColor.none);
            Assert.AreEqual(false, run.IsHighlighted);
            run.GetCTR().rPr.highlight.val = (ST_HighlightColor.cyan);
            Assert.AreEqual(true, run.IsHighlighted);
            run.GetCTR().rPr.highlight.val = (ST_HighlightColor.none);
            Assert.AreEqual(false, run.IsHighlighted);
        }
Esempio n. 20
0
        protected XWPFPicture GetPicture(XWPFRun run, Bitmap bmp)
        {
            using (var ms = new MemoryStream())
            {
                bmp.Save(ms, ImageFormat.Png);

                ms.Seek(0, 0);

                // Add an image into the document.
                var picture = run.AddPicture(ms, PICTURE_TYPE_PNG, "", Units.ToEMU(bmp.Width * PICTURE_SCALING), Units.ToEMU(bmp.Height * PICTURE_SCALING));

                return(picture);
            }
        }
Esempio n. 21
0
        public void TestAddPicture()
        {
            XWPFDocument  doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx");
            XWPFParagraph p   = doc.GetParagraphArray(2);
            XWPFRun       r   = p.Runs[0];

            Assert.AreEqual(0, doc.AllPictures.Count);
            Assert.AreEqual(0, r.GetEmbeddedPictures().Count);

            r.AddPicture(new MemoryStream(new byte[0]), (int)PictureType.JPEG, "test.jpg", 21, 32);

            Assert.AreEqual(1, doc.AllPictures.Count);
            Assert.AreEqual(1, r.GetEmbeddedPictures().Count);
        }
Esempio n. 22
0
        public void Test58618()
        {
            XWPFDocument  doc  = XWPFTestDataSamples.OpenSampleDocument("58618.docx");
            XWPFParagraph para = (XWPFParagraph)doc.BodyElements[0];

            Assert.IsNotNull(para);
            Assert.AreEqual("Some text  some hyper links link link and some text.....", para.Text);
            XWPFRun run = para.InsertNewRun(para.Runs.Count);

            run.SetText("New Text");
            Assert.AreEqual("Some text  some hyper links link link and some text.....New Text", para.Text);
            para.RemoveRun(para.Runs.Count - 2);
            Assert.AreEqual("Some text  some hyper links link linkNew Text", para.Text);
        }
Esempio n. 23
0
        public void TestRuns()
        {
            XWPFDocument  doc = new XWPFDocument();
            XWPFParagraph p   = doc.CreateParagraph();

            CT_R    run = new CT_R();
            XWPFRun r   = new XWPFRun(run, doc.CreateParagraph());

            p.AddRun(r);
            p.AddRun(r);

            Assert.IsNotNull(p.GetRun(run));
            Assert.IsNull(p.GetRun(null));
        }
        public static XWPFRun GetRun(XWPFTableRow row, int line, XWPFTableRow modelrow)
        {
            var cell = GetCell(row, line, modelrow);

            if (cell.Paragraphs.Count == 0)
            {
                cell.AddParagraph();
            }
            var     para = cell.Paragraphs[0];
            XWPFRun run  = para.CreateRun();

            run.FontFamily = "仿宋";
            return(run);
        }
Esempio n. 25
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();
            }
        }
Esempio n. 26
0
        /// <summary>
        /// 文本对象设置
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="paragraph">段落对象</param>
        /// <param name="textOptions">文本对象属性选项</param>
        private void SetText(string content, XWPFParagraph paragraph, TextOptions textOptions)
        {
            XWPFRun txt = paragraph.CreateRun();    // 创建文本对象

            txt.SetText(content);

            if (textOptions == null)
            {
                return;
            }

            txt.IsBold   = textOptions.FontStyle.IsBold;
            txt.FontSize = textOptions.FontStyle.FontSize;
            txt.SetColor(textOptions.FontStyle.ForeColor);
        }
Esempio n. 27
0
        public static XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos)
        {
            CT_P          para  = new CT_P();
            XWPFParagraph pCell = new XWPFParagraph(para, table.Body);

            //pCell.Alignment = ParagraphAlignment.LEFT;//字体
            pCell.Alignment = align;
            XWPFRun r1c1 = pCell.CreateRun();

            r1c1.SetText(setText);
            //r1c1.FontSize = 12;
            //r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
            //r1c1.SetTextPosition(textPos);//设置高度
            return(pCell);
        }
Esempio n. 28
0
        private void ReplaceText_Word(string path)
        {
            XWPFDocument doc = WordRead.Read(path);
            Dictionary <string, IList <RunCustomer> > dic = WordRead.GetDocxDic(doc);

            foreach (RunCustomer c in dic.Values)
            {
                XWPFRun run = c.Run;
                if ("宗".Equals(run.GetText(0)))
                {
                    run.SetBold(true);
                }
            }
            WordWrite.SaveToFile(doc, path);
        }
Esempio n. 29
0
        public AdminAwardsResp Awards(int smid)
        {
            AdminAwardsResp resp = new AdminAwardsResp();

            try
            {
                var result     = Fun.GetSqlConn().Query($"select sid, mid, awards from student_match where  id = {smid}");
                var sm_obj     = result.Single();
                var m_result   = Fun.GetSqlConn().Query($"select title from `match` where id = {sm_obj.mid}");
                var m_obj      = m_result.Single();
                var stu_result = Fun.GetSqlConn().Query($"select name from student where id = {sm_obj.sid}");
                var stu_obj    = stu_result.Single();

                // 生成证书
                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("荣誉证书");
                gr.SetText("——————");
                gr.SetText($"恭喜{stu_obj.name}同学在{m_obj.title}比赛中获得{sm_obj.awards},特发此证,予以鼓励!");
                FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate, FileAccess.Write);
                doc.Write(fs);
                doc.Close();

                resp.status = 0;
                resp.msg    = "ok";
                resp.data   = Path.Combine("awards", unique_file_name);
            }
            catch (Exception ex)
            {
                resp.msg    = "未知错误" + ex.ToString();
                resp.status = -2;
                Console.WriteLine(resp.msg);
            }

            return(resp);
        }
Esempio n. 30
0
        public void TestAddParagraphsToFootnote()
        {
            // Add a run to the first paragraph:

            XWPFParagraph p1 = footnote.CreateParagraph();

            p1.CreateRun().SetText(p1Text);

            // Create a second paragraph:

            XWPFParagraph p = footnote.CreateParagraph();

            Assert.IsNotNull(p, "Paragraph is null");
            p.CreateRun().SetText(p2Text);

            XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut);

            XWPFFootnote testFootnote = docIn.GetFootnoteByID(footnoteId);

            Assert.IsNotNull(testFootnote);

            Assert.AreEqual(2, testFootnote.GetParagraphs().Count);
            XWPFParagraph testP1 = testFootnote.GetParagraphs()[0];

            Assert.AreEqual(p1Text, testP1.Text);

            XWPFParagraph testP2 = testFootnote.GetParagraphs()[1];

            Assert.AreEqual(p2Text, testP2.Text);

            // The first paragraph added using CreateParagraph() should
            // have the required footnote reference added to the first
            // run.

            // Verify that we have a footnote reference in the first paragraph and not
            // in the second paragraph.

            XWPFRun r1 = testP1.Runs[0];

            Assert.IsNotNull(r1);
            Assert.IsTrue(r1.GetCTR().GetFootnoteRefList().Count > 0, "No footnote reference in testP1");
            Assert.IsNotNull(r1.GetCTR().GetFootnoteRefArray(0), "No footnote reference in testP1");

            XWPFRun r2 = testP2.Runs[0];

            Assert.IsNotNull(r2, "Expected a run in testP2");
            Assert.IsTrue(r2.GetCTR().GetFootnoteRefList().Count == 0, "Found a footnote reference in testP2");
        }