package com.chenyang.nse.bussiness.tools.xml.xmlclass;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TableOfContent;
import com.spire.doc.fields.TextRange;
import java.awt.Color;

public class AddToc {
   public static void AddToc(String path) {
      Document doc = new Document(path);
      Paragraph parainserted = new Paragraph(doc);
      TextRange tr = parainserted.appendText("目 录");
      tr.getCharacterFormat().setBold(true);
      tr.getCharacterFormat().setTextColor(Color.gray);
      doc.getSections().get(0).getParagraphs().insert(0, parainserted);
      parainserted.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
      new TableOfContent(doc, "{\\o \"1-3\" \\h \\z \\u}");
      doc.getSections().get(0).getParagraphs().get(0).appendTOC(1, 3);
      doc.updateTableOfContents();
      doc.saveToFile(path, FileFormat.Docx_2010);
   }
}
