0
A
回答
0
不容易在Word文档中结束,虽然Word创建以w文件:lastRenderedPageBreak。
最好让您的OCR程序在每个已转换文本块之间的文档中插入一些标记。
然后,根据它是什么类型的Word文档,使用适当的工具处理该文件。
3
如果您安装了Word,则可以使用Word对象模型从C#处理Word文档。
首先,添加对Word对象模型的引用。右键点击该项目,然后Add Reference... -> COM -> Microsoft Word 14.0 Object Model
(或类似的,取决于您的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();
}
}
}
}
4
同other answer,但有一个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();
}
}
相关问题
- 1. 如何将文本拆分为单独的UITextView页面?
- 2. 使用PHP(或可能perl)将PDF文档拆分为单独的页面
- 3. 如何在python中将文件拆分为单独的行?
- 4. 如何将模块的代码拆分为单独的文件
- 5. 如何在contenteditable中将文本分成单独的页面(如Google文档)?
- 6. 如何分页Word文档从C#
- 7. Ruby +如何将pdf拆分为单独的页面?
- 8. 用VBA拆分Word文档
- 9. 如何将快速路由器拆分为单独的文件?
- 10. 将asp.net页面导出为word文档
- 11. 将Word文档拆分成更小的文档
- 12. 如何将文本文件拆分为C中的部分?
- 13. 将文件拆分为C++
- 14. Python - 如何将文本输入拆分为单独的元素
- 15. 如何将word文档/ pdf /图像的部分(每页多页)作为单独的图像/ word文档/ pdf进行提取?
- 16. 根据两列将此csv/xls拆分为单独的文件?
- 17. 将解析逻辑拆分为单独的文件Node.js
- 18. VB.Net将单词文档分解为单独的文档(分页符作为分隔符)
- 19. 如何在单独的文件中拆分AngularJS代码?
- 20. 将单独的文本文件的段落拆分为不同的字符串
- 21. 如何将MS Word 2003文档转换为C#中的HTML?
- 22. 将文本文件拆分为单独的文件,并将子字符串保存为文件名;蟒蛇
- 23. 将pdf文件转换为word文档
- 24. 将Django模型拆分为单独文件时遇到问题
- 25. 将网页保存为Word文档? (VB.NET)
- 26. 如何将HTML文档分解为电子书的页面?
- 27. 将Excel文件中的文本插入Word文档(Word VBA)
- 28. Interop Word - 从文档中删除页面
- 29. 在ASP.Net中显示Word文档页面
- 30. 在Word文档中计数页面
感谢亲爱@ZevSpitz – Iman 2012-08-03 08:11:23
这是一个完美的出发点,以创造一些有用的。 – 2012-10-16 15:12:45