c# aspose操作word文档

背景

这个是一个操作word文档的插件

1.1插入图片

using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Rendering;

 

  Document doc = new Document(TempValue);//TempValue doc模板的路径


  DocumentBuilder builder = new DocumentBuilder(doc);

  Shape shape = new Shape(doc,ShapeType.Image);

  shape.ImageData.SetImage(Server.MapPath("Images/test.jpg"));

  shape.Width = 70;//设置宽和高
  shape.Height = 70;

  shape.WrapType = WrapType.None;
  shape.BehindText = true;

  //shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
  //shape.HorizontalAlignment = HorizontalAlignment.Center;
  //shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
  //shape.VerticalAlignment = VerticalAlignment.Center;
  //shape.HorizontalAlignment = HorizontalAlignment.Center;

  builder.MoveToBookmark("PO_MyImage");
  builder.InsertNode(shape);

  doc.Save("newword.doc",SaveFormat.Doc);

1.2删除指定段落

// 根据表格找到所在段落
var paragraph = (Paragraph) table.GetAncestor(NodeType.Paragraph);
// 清除段落前的分页符
if (paragraph.ParagraphFormat.PageBreakBefore)
paragraph.ParagraphFormat.PageBreakBefore = false;
// 清除段落中的分页符
foreach (Run run in paragraph.Runs)
{
if (run.Text.Contains(ControlChar.PageBreak))
run.Text = run.Text.Replace(ControlChar.PageBreak, string.Empty);
}

1.3合并文档

Document doc = new Document();
// We should call this method to clear this document of any existing content.
doc.RemoveAllChildren();
 
int recordCount = 5;
for (int i = 1; i <= recordCount; i++)
{
    // Open the document to join.
    Document srcDoc = new Document(@"C:DetailsList.doc");
 
    // Append the source document at the end of the destination document.
    doc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles);
 
    // In automation you were required to insert a new section break at this point, however in Aspose.Words we
    // don't need to do anything here as the appended document is imported as separate sectons already.
 
    // If this is the second document or above being appended then unlink all headers footers in this section
    // from the headers and footers of the previous section.
    if (i > 1)
        doc.Sections[i].HeadersFooters.LinkToPrevious(false);
}

1.4合并的时候在同一页显示

Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

// Make the document appear straight after the destination documents content.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;

// Append the source document using the original styles found in the source document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
dstDoc.Save(gDataDir + "TestFile.JoinContinuous Out.doc");

1.5合并的时候再另外一页开始

 Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc =  new Document(gDataDir + "TestFile.Source.doc");

// Set the appended document to start on a new page.
srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

// Append the source document using the original styles found in the source document.
dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
dstDoc.Save(gDataDir + "TestFile.JoinNewPage Out.doc");

1.6其他的一些信息

https://blog.csdn.net/ibigpig/article/details/8432245

1.7 Spire.Doc与Aspose.Words功能对比

http://www.cnblogs.com/dare/archive/2018/07/03/9259417.html