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. 如何將模板拆分爲Meteor中的單獨文件
- 4. 如何在python中將文件拆分爲單獨的行?
- 5. 如何將單個Node.js文件拆分爲單獨的模塊
- 6. 如何分頁Word文檔從C#
- 7. 如何將模塊的代碼拆分爲單獨的文件
- 8. 如何在contenteditable中將文本分成單獨的頁面(如Google文檔)?
- 9. Ruby +如何將pdf拆分爲單獨的頁面?
- 10. 用VBA拆分Word文檔
- 11. 拆分HTML頁面以便打印機將其拆分爲單獨的頁面
- 12. 如何將此Coffeescript拆分爲單獨的文件?
- 13. 如何將CA證書包拆分爲單獨的文件?
- 14. 如何將快速路由器拆分爲單獨的文件?
- 15. Word 2007中的拆分文檔
- 16. Elasticsearch - 將文檔拆分爲單獨索引的含義
- 17. 將asp.net頁面導出爲word文檔
- 18. 將Word文檔拆分成更小的文檔
- 19. 如何將文本文件拆分爲C中的部分?
- 20. MS Word將每個頁面另存爲單獨的PDF文檔,並以文檔內的特定文本命名
- 21. 如何獲得word文檔的分頁?
- 22. 將文件拆分爲C++
- 23. 將部分類拆分爲單獨的文件
- 24. Python - 如何將文本輸入拆分爲單獨的元素
- 25. 將單獨文檔中的頁眉預先添加到頁面
- 26. 將Word文檔保存爲「Word文檔」
- 27. CSV文件將我的行數據拆分爲單獨的行
- 28. asp.net將代碼拆分爲aspx頁面上的單獨行
- 29. 如何將word文檔/ pdf /圖像的部分(每頁多頁)作爲單獨的圖像/ word文檔/ pdf進行提取?
- 30. 如何將word文檔轉換爲perl中的pdf文件?
感謝親愛@ZevSpitz – Iman 2012-08-03 08:11:23
這是一個完美的出發點,以創造一些有用的。 – 2012-10-16 15:12:45