1. Introduction
1. Class library for operating Word:
- NPOI: V2.5.1 rapid generation https://github.com/tonyqus/npoi NPOI Baidu Cloud download: https://pan.baidu.com/s/13wiYmZ2txCq79CR-kj9Cvg Extraction code: 5x1v
- DocX (free version of Xceed Words for .NET): V1.6.0 https://github.com/xceedsoftware/DocX
- Aspose.Words: Charges
- Spire.Doc: Charges
2. Simple use
1. Instantiation of XWPFDocument class
An instance of this class corresponds to a word document
XWPFDocument MyDoc = new XWPFDocument();
2. Set the page size
If you do not set the page size, the default is portrait A4 size. The page size of A4 in landscape orientation. If you want portrait orientation, just swap the width and height.
T_SectPr m_SectPr = new CT_SectPr(); //Instance of an instance of size class m_SectPr.pgSz.w = 16838; //Set the width (here is a ulong type) m_SectPr.pgSz.h = 11906; //Set the height (here is a ulong type) MyDoc.Document.body.sectPr = m_SectPr; //Set the page size
The unit here is rather special. Twip is used, which is a length unit that has nothing to do with the screen. The purpose is to maintain a consistent calculation method when application elements are output to different devices.
Conversion relationship:
- 1 inch = 1440 twips
- 1 centimeter = 567 twips
- 1 pound = 20 twips
- 1 pixel = 15 twips
Common page sizes: (unit Twip)
- A4 (vertical): W=11906 H=16838
- A4 (vertical): W=16838 H=11906
- A5: W=8390 H=11906
- A6: W=5953 H=8390
Example: Control word page margins
//Create document object var doc = new XWPFDocument(); doc.Document.body.sectPr = new CT_SectPr(); CT_SectPr m_SectPr = doc.Document.body.sectPr; m_SectPr.pgSz.h = (ulong)16838; m_SectPr.pgSz.w = (ulong)11906; //page margins m_SectPr.pgMar.left = (ulong)800;//Left margin m_SectPr.pgMar.right = (ulong)800;//right margin m_SectPr.pgMar.top = "850";//Top margin m_SectPr.pgMar.bottom = "850";//bottom margin
3. Paragraph processing
Create a paragraph
A paragraph is an instance of the XWPFParagraph type, and a paragraph is generated by using the CreateParagraph() method of an XWPFDocument instance.
XWPFParagraph MyParagraph = MyDoc.CreateParagraph();
The paragraph class has many properties for setting paragraph-related content. Mainly include the following aspects
-
- BorderXXXXX attribute: This value is used to set the style of the border. This property is used to get or set a value of the Borders enumeration type.
- FillBackgroundColor property: background fill color. This property gets or sets a variable of type string. The meaning of this variable is the RGB value of a certain color (all colors in NPOI are expressed in this form). For example, if set to black, the corresponding code is as follows:
MyParagraph.FillBackgroundColor = "#000000";
- Alignment property: the alignment of the paragraph. This property gets or sets a ParagraphAlignment enumeration value.
- VerticalAlignment property: The vertical alignment of the text. This property gets or sets a TextAlignment enumeration value.
- IndentationFirstLine property: used to set the indentation of the first line of a paragraph. This property gets or sets an int type variable.
This int value is not the number of words indented. Here you can use a function to calculate the indented distance.
//Paragraph indentation The return value is the corresponding indentation distance //(fontname: text type name fontsize: text size fontcount: indentation number fontstyle: text type (italic, bold...)) int Indentation(String fontname, int fontsize, int fontnum, FontStyle fontstyle) { Graphics gp = this.CreateGraphics(); gp.PageUnit = GraphicsUnit.Point; SizeF size = gp.MeasureString("字", new Font(fontname, fontsize * 0.75F, fontstyle)); return (int)size.Width * fontnum * 10; }
Example: Creating a paragraph
var paragraph = doc.CreateParagraph(); paragraph.Alignment = ParagraphAlignment.CENTER; //The font is centered var run = paragraph.CreateRun(); run.IsBold = true; run.SetText(contend); run.FontSize = 28; run.SetFontFamily("黑体", FontCharRange.None); //Set boldface //Control the top and bottom distance between the paragraph and other elements paragraph.SpacingBeforeLines = 20;//Distance above paragraph.SpacingAfterLines = 20;//Distance below
Page change (If this page is not full, just write the next page)
paragraph = doc.CreateParagraph(); paragraph.CreateRun().AddBreak(BreakType.PAGE);
Text processing
- doc.Paragraphs gets all the paragraph objects in the document;
- para.ParagraphText gets the text data of the paragraph;
- para.ReplaceText (text to be replaced, replacement text) Replace the text of the paragraph (the key to the template)
4. Form processing
doc.Tables gets all the table objects in the document; //doc.Tables gets only the outermost table in Word, not including the nested inner ones.
cell.Tables; //Get nested cells to use
row.Rows //Get all rows of the table;
row.GetTableICells();//Get all cells in the table row;
After obtaining the cell, you can obtain the text paragraphs (Paragraphs) in the cell and perform text replacement.
Create table
var table = doc.CreateTable(number of rows, number of columns); table.Width = 5000;
Control the column width in the table (It should be noted here that setting the column width of only one row will invalidate the set column width once text is inserted, so it must be set for every cell)
for (int r = 0; r < 8; r + + ) { SetCellWith(table.GetRow(r).GetCell(0), "600"); SetCellWith(table.GetRow(r).GetCell(1), "1500"); SetCellWith(table.GetRow(r).GetCell(2), "1500"); SetCellWith(table.GetRow(r).GetCell(3), "600"); }
Set the cells in the table to be vertically centered
var rowcell = table.GetRow(r).GetCell(c); rowcell.SetVerticalAlignment(XWPFTableCell.XWPPFvertAlign.CENTER);
Set cell content
//Text formatting in table var para = new CT_P(); var pCell = new XWPFParagraph(para, table.Body); pCell.Alignment = paragraphAlignment; //Font centered pCell.VerticalAlignment = TextAlignment.CENTER; //Font centered var r1c1 = pCell.CreateRun(); r1c1.SetText(setText); r1c1.FontSize = 11; r1c1.SetFontFamily("宋体", FontCharRange.None); //Set Yahei font pCell.SpacingAfterLines = 40; pCell.SpacingBeforeLines = 40; //Put into cell table.GetRow(4).GetCell(1).SetParagraph(pCell);
Line break in table
var run= paragraph.CreateRun(); run.SetText(contents[i]); run.FontSize = 11; run.SetFontFamily("宋体", FontCharRange.None); run.AddBreak(BreakType.TEXTWRAPPING);//Line break
Merge cells
table.GetRow(rowIndex).MergeCells(fromCol, toCol);//Merge columns
Merge row cells horizontally
CT_Tc cttcofRowThird = cell.GetCTTc(); CT_TcPr ctProfRowThird = cttcofRowThird.AddNewTcPr(); ctProfRowThird.gridSpan = new CT_DecimalNumber(); ctProfRowThird.gridSpan.val = num.ToString();//Merge num columns
Merge rows and vertically merge column cells
public void MYMergeRows(XWPFTable table, int fromRow, int toRow, int colIndex) { for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex + + ) { XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(colIndex); rowcell.SetVerticalAlignment(XWPFTableCell.XWPPFvertAlign.CENTER); CT_Tc cttc = rowcell.GetCTTc(); CT_TcPr ctTcPr = cttc.tcPr; if (ctTcPr == null) { ctTcPr = cttc.AddNewTcPr(); } if (rowIndex == fromRow) { // The first merged cell is set with RESTART merge value ctTcPr.AddNewVMerge().val = ST_Merge.restart; } else { // Cells which join (merge) the first one, are set with CONTINUE ctTcPr.AddNewVMerge().val = ST_Merge.@continue;//Continue merging rows } ctTcPr.AddNewVAlign().val = ST_VerticalJc.center;//Vertical } }
5. Header and footer processing
XWPFDocument doc = new XWPFDocument(); doc.Document.body.sectPr = new CT_SectPr(); CT_SectPr m_SectPr = doc.Document.body.sectPr; //Create header CT_Hdr m_Hdr = new CT_Hdr(); CT_P m_P = m_Hdr.AddNewP(); m_P.AddNewR().AddNewT().Value = "Header content value"; m_P.AddNewPPr().AddNewJc().val = ST_Jc.center;// Center the header //Create header relationship (headern.xml) XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader m_h = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count + 1); doc.CreateFootnotes(); //Set the header m_h.SetHeaderFooter(m_Hdr); CT_HdrFtrRef m_HdrFtr = m_SectPr.AddNewHeaderReference(); m_h.GetRelationById(m_HdrFtr.id); //Create footer CT_Ftr m_ftr = new CT_Ftr(); CT_P m_fP = m_ftr.AddNewP(); m_fP.AddNewR().AddNewT().Value = "footer content value"; m_fP.AddNewPPr().AddNewJc().val = ST_Jc.center;// Header is centered //Create footer relationship (footern.xml) XWPFRelation Frelation = XWPFRelation.FOOTER; XWPFFooter m_f = (XWPFFooter)doc.CreateRelationship(Frelation, XWPFFactory.GetInstance(), doc.FooterList.Count + 1); //Set the footer m_f.SetHeaderFooter(m_ftr); m_HdrFtr = m_SectPr.AddNewFooterReference(); m_HdrFtr.type = ST_HdrFtr.@default; m_f.GetRelationById(m_HdrFtr.id);
3. Comprehensive examples
CT_SectPr srcpr = new CT_SectPr(); //Set the A4 paper portrait orientation. If you want it to be landscape orientation, just swap the two values. srcpr.pgSz.w = (ulong)11906; srcpr.pgSz.h = (ulong)16838; XWPFDocument doc = new XWPFDocument(); doc.Document.body.sectPr = srcpr; //output title XWPFParagraph ptitle = doc.CreateParagraph(); ptitle.Alignment = ParagraphAlignment.CENTER; XWPFRun titlerun = ptitle.CreateRun(); //Add text to this paragraph titlerun.SetText("title"); titlerun.IsBold = true; titlerun.FontFamily = "Chinese regular script"; titlerun.FontSize = 30; titlerun.SetColor("blue"); //Output a paragraph XWPFParagraph p2 = doc.CreateParagraph(); p2.Alignment = ParagraphAlignment.CENTER; p2.IndentationFirstLine = (int)100; //First line indentation XWPFRun r2 = p2.CreateRun(); r2.SetText(string.Format("Generation time: {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm"))); r2.FontSize = 15; titlerun.FontFamily = "宋体"; StringBuilder sb = new StringBuilder(); using (FileStream stream = File.OpenRead("simple.docx")) { XWPFDocument docx = new XWPFDocument(stream); //Header foreach (XWPFHeader xwpfHeader in docx.HeaderList) { sb.AppendLine(string.Format("{0}", new string[] { xwpfHeader.Text })); } //footer foreach (XWPFFooter xwpfFooter in docx.FooterList) { sb.AppendLine(string.Format("{0}", new string[] { xwpfFooter.Text })); } //Read paragraph foreach (var para in docx.Paragraphs) { string text = para.ParagraphText; //Get text var runs = para.Runs; // string styleid = para.Style; for (int i = 0; i < runs.Count; i + + ) { var run = runs[i]; text = run.ToString(); //Get the text of run sb.Append(text + ","); } } //Read the table foreach (XWPFTable table in docx.Tables) { //Loop table rows foreach (XWPFTableRow row in table.Rows) { foreach (XWPFTableCell cell in row.GetTableCells()) { sb.Append(cell.GetText()); } } } //Read the picture foreach (XWPFPictureData pictureData in docx.AllPictures) { string picExtName = pictureData.SuggestFileExtension(); string picFileName = pictureData.FileName; byte[] picFileContent = pictureData.Data; string picTempName = string.Format(Guid.NewGuid().ToString() + "_" + picFileName + "." + picExtName); using (FileStream fs = new FileStream(picTempName, FileMode.Create, FileAccess.Write)) { fs.Write(picFileContent, 0, picFileContent.Length); fs.Close(); } sb.AppendLine(picTempName); } Console.WriteLine(sb.ToString()); } FileStream out1 = new FileStream("simple.docx", FileMode.Create); doc.Write(out1); out1.Close();
4. Reference
Word and Excel font size comparison table
Additional content: (Display XPS files, you can also convert DocX to XPS and then render)
System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog(); open.Filter = "XPS|*.xps"; if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var result = new XpsDocument(open.FileName, System.IO.FileAccess.Read); documentview.Document = result.GetFixedDocumentSequence();/*Use WPF’s built-in control DocumentViewer to display XPS files. You can convert the DOCX format into XPS and then display it*/ } }
Original address: https://www.cnblogs.com/springsnow/p/13125531.html