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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM