• 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

  • 相关阅读:
    接口与实现分离
    C++的explicit关键字
    C++的类型转换
    使用catch做单元测试简介
    C++可调用对象与函数表
    在sublime中使用cppcheck
    你需要的代码静态检查
    构造析构与拷贝赋值那些事
    c++的关联容器入门(map and set)
    【致敬程序猿】
  • 原文地址:https://www.cnblogs.com/dongh/p/15175043.html
Copyright © 2020-2023  润新知