LoginSignup
0
0

More than 5 years have passed since last update.

C#は、セクション区切りとページ区切りに基づいてWord文書を分割します

Last updated at Posted at 2018-09-14

Word文書を編集するときは、Word文書を分割する必要がありますが、一般的には、元の文書の内容をいくつかの部分に分割して別の文書または元の文書にコピーして貼り付けるのが最も簡単です。 複数のコピーにコピーしたら、各文書の不要な部分を削除します。 しかし、多くのドキュメントコンテンツに遭遇した場合、これらの2つの方法を使用すると、プロセスがより煩雑になり、ドキュメントを分割しやすくなります。
この記事ではSpire.Docを使用して[C#]コードを使用してWord文書を分割するセクション区切りとページ区切りを実装する方法を詳しく説明します。
使用する必要のあるツール:Spire.Doc for .NET;Visual Studio;
セクション区切りに従ってWord文書を分割する
【C#】

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;

namespace Split_by_Sectionbreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Documentオブジェクトをインスタンス化する
            Document document = new Document();
            //分割するWord文書を読み込む
            document.LoadFromFile("テスト文書.docx");
            //セクション区切りを最初のセクションの最初の段落の最後に挿入します
            document.Sections[0].Paragraphs[0].InsertSectionBreak(SectionBreakType.NoBreak);
            //セクション区切りを2番目のセクションの2番目の段落の末尾に挿入します
            document.Sections[1].Paragraphs[1].InsertSectionBreak(SectionBreakType.NoBreak);
            Document newWord;
            for (int i = 0; i < document.Sections.Count; i++)
            {
                //セクションごとに新しい文書を作成する
                newWord = new Document();
                //セクションの内容を新しい文書にコピーする
                newWord.Sections.Add(document.Sections[i].Clone());
                //ドキュメントを保存
                newWord.SaveToFile(String.Format("セグメンテーション結果ドキュメント_{0}.docx", i));
            }
        }
    }
}

コードのデバッグが実行されると、結果は次のようになります:
1.jpg

改ページに従ってWord文書を分割する
【C#】

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Doc;
using Spire.Doc.Documents;
namespace Split_by_pagebreak
{
    class Program
    {
        static void Main(string[] args)
        {
            //Documentオブジェクトをインスタンス化する
            Document original = new Document();
            //分割するWord文書を読み込む
            original.LoadFromFile("テスト文書.docx");
            //最初のセクションの4番目の段落の末尾に改ページを挿入します
            original.Sections[0].Paragraphs[3].AppendBreak(BreakType.PageBreak);
            //最初のセクションの6番目の段落の最後に改ページを挿入します
            original.Sections[0].Paragraphs[5].AppendBreak(BreakType.PageBreak);
            //新しい文書をインスタンス化し、新しい章を追加する
            Document newWord = new Document();
            Section section = newWord.AddSection();
            int index = 0;
            //この章によれば、段落の階層構造は文書要素を大小順に横断し、内容を新しい文書にコピーします
            foreach (Section sec in original.Sections)
            {
                foreach (DocumentObject obj in sec.Body.ChildObjects)
                {
                    if (obj is Paragraph)
                    {
                        Paragraph para = obj as Paragraph;
                        section.Body.ChildObjects.Add(para.Clone());

                        foreach (DocumentObject parobj in para.ChildObjects)
                        {
                            //段落のページ区切りを見つけて、新しい段落に保存します
                            if (parobj is Break && (parobj as Break).BreakType == BreakType.PageBreak)
                            { 
                                int i = para.ChildObjects.IndexOf(parobj);
                                section.Body.LastParagraph.ChildObjects.RemoveAt(i);
                                newWord.SaveToFile(String.Format("ページ分割結果ドキュメント_{0}.docx", index), FileFormat.Docx);
                                index++;
                                //文書が完成した後に新しい文書を作成する
                                newWord = new Document();
                                section = newWord.AddSection();
                                //前の改ページがある段落のすべての内容を新しい文書にコピーする
                                section.Body.ChildObjects.Add(para.Clone());
                                //新しい文書の最初の段落(つまり、コピーしたばかりの節)に子要素がない場合は、文書の最初の子を削除します                                
                                if (section.Paragraphs[0].ChildObjects.Count == 0)
                                {
                                    section.Body.ChildObjects.RemoveAt(0);
                                }
                                else
                                {
                                    //コンテンツがある場合は改ページの前にすべてを削除する
                                    while (i >= 0)
                                    {
                                        section.Paragraphs[0].ChildObjects.RemoveAt(i);
                                        i--;
                                    }
                                }
                            }
                        }
                    }
                    if (obj is Table)
                    {
                        section.Body.ChildObjects.Add(obj.Clone());
                    }
                }
            }
            newWord.SaveToFile(String.Format("ページ分割結果ドキュメント_{0}.docx", index), FileFormat.Docx);

        }
    }
}

コードのデバッグが実行されると、結果は次のようになります:
2.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0