ASPOSE.Word 开发资料整理

时间:2023-03-09 05:12:21
ASPOSE.Word 开发资料整理

1.总体说明:操作主要涉及两个对象Document及DocumentBuilder

Document主要用来获取文档中的节点,DocumentBuilder主要用于实现文档内容的写入

            doc_Operate = new Document(blankTemplatePth);
doc_template = new Document(ToCopytemplatePth);
builder_template = new DocumentBuilder(doc_template);
builder_operate = new DocumentBuilder(doc_Operate);

2.内容写入,样式设置

            builder_operate.ParagraphFormat.Style = doc_Operate.Styles["标题"];
builder_operate.Writeln("XXX报告");
builder_operate.ParagraphFormat.Style = doc_Operate.Styles["正文"];
builder_operate.Writeln("(征求意见稿)");

3.关于分节,分节之后,需要将当前插入光标移动至新插入的Section中,否则容易出错

            builder_operate.InsertBreak(BreakType.SectionBreakNewPage);
Section lstSection2 = doc_Operate.LastSection;
int idx2 = doc_Operate.Sections.IndexOf(lstSection2);
builder_operate.MoveToSection(idx2);
builder_operate.MoveToDocumentEnd();

4.复制模板文档中的表格,插入到当前文档中

        //获取Table对象
        Table tbN=doc.ImportNode(tb, true, ImportFormatMode.KeepSourceFormatting) as Table;
doc.LastSection.Body.AppendChild(tbN);
Paragraph p = new Paragraph(doc);
tbN.ParentNode.AppendChild(p); //添加paragraph,以打断表格

5.当前表格插入新行

Row rN = table_N.Rows[].Clone(true) as Row;
table_N.InsertAfter(rN, table_N.LastRow);

6.单元格插入图片

builder_operate.MoveToCell(tableindex, , , );
Aspose.Words.Drawing.Shape spa = builder_operate.InsertImage(jietuA);

此处插入图片时,容易出现异常,提示tableindex超出索引,TableIndex是通过indexOf对象获取到的.

查阅大量资料发现,如果一个word文档中出现了多个Section,需要采用本篇第3小节的内容,移动当前的光标

7.设置单元格内容

public static void setCellText(this Cell cell, string txt)
{
Run run;
if (cell.FirstParagraph.Runs.Count == )
{
run = new Run(cell.Document);
}
else
{
run = (Run)cell.FirstParagraph.Runs[].Clone(true);
}
run.Text = txt;
for (int i = ; i < cell.Paragraphs.Count; i++)
{
Node np=cell.GetChild(NodeType.Paragraph, i, false);
cell.RemoveChild(np);
} cell.FirstParagraph.RemoveAllChildren();
cell.EnsureMinimum();
if(cell.Paragraphs.Count!=)
cell.Paragraphs[].AppendChild(run);
}

8.合并单元格

public static void HorizontallyMergeCells(Cell c1, Cell c2,bool SaveAllVal=false)
{
c1.CellFormat.HorizontalMerge = CellMerge.First; //Move all content from next cell to previous
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
} c2.CellFormat.HorizontalMerge = CellMerge.Previous;
} public static void VerticallyMergeCells(Cell c1, Cell c2,bool SaveAllVal)
{
c1.CellFormat.VerticalMerge = CellMerge.First; //Move all content from bottom cell to top
if (SaveAllVal)
{
foreach (Node child in c2.ChildNodes)
c1.AppendChild(child);
} c2.CellFormat.VerticalMerge = CellMerge.Previous;
} public static void MergeCell(this Table tb, int startrowid, int endrowid, int startColId, int endColId)
{
for (int i = startrowid; i <= endrowid; i++)
{
for (int j = startColId+; j <= endColId; j++)
{
//每行进行横向合并
HorizontallyMergeCells(tb.Rows[i].Cells[startColId], tb.Rows[i].Cells[j]);
}
} //首行进行纵向合并
for (int i = startrowid+; i <= endrowid; i++)
{
VerticallyMergeCells(tb.Rows[startrowid].Cells[startColId], tb.Rows[i].Cells[startColId], false);
}
}

9.插入另一个Word文档

Document docShuoMing = new Document(summaryTemplatePth);
//docShuoMing.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
//docShuoMing.FirstSection.PageSetup.RestartPageNumbering = true;
doc_Operate.LastSection.AppendContent(docShuoMing.FirstSection);
//doc_Operate.AppendDocument(docShuoMing, ImportFormatMode.KeepSourceFormatting);
builder_operate.MoveToDocumentEnd();

10.插入页码

public static void InsertHeaderFooter(Section sect, HeaderFooterType headerType)
{
HeaderFooter header = sect.HeadersFooters[headerType]; if (header == null)
{
header = new HeaderFooter(sect.Document, headerType);
sect.HeadersFooters.Add(header);
}
} public static void CancelHeaderFotter(Section sect)
{
for (int i = sect.HeadersFooters.Count-; i >=; i--)
{
sect.HeadersFooters.RemoveAt(i);
}
}
/// <summary>
/// 插入页码
/// </summary>
/// <param name="builder_operate"></param>
/// <param name="sec"></param>
/// <param name="startNumber"></param>
public static void InsertYeMa(DocumentBuilder builder_operate,Section sec,NumberStyle ns=NumberStyle.Arabic,int startNumber=)
{
//添加页码
ASPWHelper.InsertHeaderFooter(sec, HeaderFooterType.HeaderPrimary);
builder_operate.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
//设置开始页码
builder_operate.PageSetup.PageStartingNumber = startNumber;
builder_operate.PageSetup.PageNumberStyle = ns;
//页码在每个section会重新开始
builder_operate.PageSetup.RestartPageNumbering = true;
//页码位置
builder_operate.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder_operate.InsertField("PAGE", "");
builder_operate.MoveToDocumentEnd();
}

11.移除最后的分页符

public static void RemoveLastPageBreak(Document doc)
{
NodeCollection runs = doc.LastSection.GetChildNodes(NodeType.Run, true); for (int i = runs.Count - ; i >= ; i--)
{
Run run = (Run)runs[i];
if (run.Text.IndexOf(ControlChar.PageBreakChar) >= )
{
run.Text = run.Text.Remove(run.Text.IndexOf(ControlChar.PageBreakChar), );
break;
}
}
}

12.插入目录

/// <summary>
/// 插入目录
/// </summary>
/// <param name="bulider_blank"></param>
public static void InsertTOC(DocumentBuilder bulider_blank)
{
//设置"目录"格式
bulider_blank.ParagraphFormat.Alignment = ParagraphAlignment.Center;
bulider_blank.Bold = true;
bulider_blank.Font.Name = "SONG";
bulider_blank.Writeln("目录");
bulider_blank.ParagraphFormat.ClearFormatting();//清除所有样式
bulider_blank.InsertTableOfContents("\\o\"1-3\"\\h\\z\\u");
bulider_blank.InsertBreak(BreakType.SectionBreakNewPage);
}

获取某一Section的页数

public static int getLastSectionPageCount(Document doc)
{
int idx = doc.Sections.IndexOf(doc.LastSection);
Document tmp = doc.Clone();
for (int i = tmp.Sections.Count - ; i >= ; i--)
{
if (i != idx)
{
tmp.Sections.RemoveAt(i);
}
}
return tmp.PageCount;
}