Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c语言中将Word文档的页面拆分为单独的文件#_C#_Ms Word - Fatal编程技术网

C# 如何在c语言中将Word文档的页面拆分为单独的文件#

C# 如何在c语言中将Word文档的页面拆分为单独的文件#,c#,ms-word,C#,Ms Word,我有一个OCR程序,可以将图像转换为word文档。word文档包含所有图像的文本,我想将其拆分为单独的文件 在c#中有什么方法可以做到这一点吗 谢谢在Word文档结尾不太容易,尽管Word使用w:lastrendedPageBreak创建文档 最好让OCR程序在每个转换文本块之间的文档中插入一些标记 然后,根据Word文档的类型,使用适当的工具处理该文件。如果安装了Word,可以使用Word对象模型从C#操作Word文档 首先,添加对Word对象模型的引用。右键单击项目,然后addreferen

我有一个OCR程序,可以将图像转换为word文档。word文档包含所有图像的文本,我想将其拆分为单独的文件

在c#中有什么方法可以做到这一点吗


谢谢

在Word文档结尾不太容易,尽管Word使用w:lastrendedPageBreak创建文档

最好让OCR程序在每个转换文本块之间的文档中插入一些标记


然后,根据Word文档的类型,使用适当的工具处理该文件。

如果安装了Word,可以使用Word对象模型从C#操作Word文档

首先,添加对Word对象模型的引用。右键单击项目,然后
addreference…->COM->Microsoft Word 14.0对象模型
(或类似内容,取决于您的Word版本)

然后,您可以使用以下代码:

using Microsoft.Office.Interop.Word;
//for older versions of Word use:
//using Word;

namespace WordSplitter {
    class Program {
        static void Main(string[] args) {
            //Create a new instance of Word
            var app = new Application();

            //Show the Word instance.
            //If the code runs too slowly, you can show the application at the end of the program
            //Make sure it works properly first; otherwise, you'll get an error in a hidden window
            //(If it still runs too slowly, there are a few other ways to reduce screen updating)
            app.Visible = true;

            //We need a reference to the source document
            //It should be possible to get a reference to an open Word document, but I haven't tried it
            var doc = app.Documents.Open(@"path\to\file.doc");
            //(Can also use .docx)

            int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];

            //We'll hold the start position of each page here
            int pageStart = 0;

            for (int currentPageIndex = 1; currentPageIndex <= pageCount; currentPageIndex++) {
                //This Range object will contain each page.
                var page = doc.Range(pageStart);

                //Generally, the end of the current page is 1 character before the start of the next.
                //However, we need to handle the last page -- since there is no next page, the 
                //GoTo method will move to the *start* of the last page.
                if (currentPageIndex < pageCount) {
                    //page.GoTo returns a new Range object, leaving the page object unaffected
                    page.End = page.GoTo(
                        What: WdGoToItem.wdGoToPage,
                        Which: WdGoToDirection.wdGoToAbsolute,
                        Count: currentPageIndex + 1
                    ).Start - 1;
                } else {
                    page.End = doc.Range().End;
                }
                pageStart = page.End + 1;

                //Copy and paste the contents of the Range into a new document
                page.Copy();
                var doc2 = app.Documents.Add();
                doc2.Range().Paste();
            }
        }
    }
}
使用Microsoft.Office.Interop.Word;
//对于旧版本的Word使用:
//用词;
名称空间字拆分器{
班级计划{
静态void Main(字符串[]参数){
//创建Word的新实例
var app=新应用程序();
//显示单词实例。
//如果代码运行太慢,可以在程序末尾显示应用程序
//首先确保它工作正常;否则,将在隐藏窗口中出现错误
//(如果运行速度仍然太慢,有几种其他方法可以减少屏幕更新)
app.Visible=true;
//我们需要对源文档的引用
//应该可以获取对open Word文档的引用,但我还没有尝试过
var doc=app.Documents.Open(@“path\to\file.doc”);
//(也可以使用.docx)
int pageCount=doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];
//我们将保留每页的起始位置
int pageStart=0;
for(int currentPageIndex=1;currentPageIndex与相同,但具有IEnumerator和文档的扩展方法

static class PagesExtension {
    public static IEnumerable<Range> Pages(this Document doc) {
        int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];
        int pageStart = 0;
        for (int currentPageIndex = 1; currentPageIndex <= pageCount; currentPageIndex++) {
            var page = doc.Range(
                pageStart
            );
            if (currentPageIndex < pageCount) {
                //page.GoTo returns a new Range object, leaving the page object unaffected
                page.End = page.GoTo(
                    What: WdGoToItem.wdGoToPage,
                    Which: WdGoToDirection.wdGoToAbsolute,
                    Count: currentPageIndex+1
                ).Start-1;
            } else {
                page.End = doc.Range().End;
            }
            pageStart = page.End + 1;
            yield return page;
        }
        yield break;
    }
}

这是创建有用内容的完美起点。
static void Main(string[] args) {
    var app = new Application();
    app.Visible = true;
    var doc = app.Documents.Open(@"path\to\source\document");
    foreach (var page in doc.Pages()) {
        page.Copy();
        var doc2 = app.Documents.Add();
        doc2.Range().Paste();
    }
}