이미지를 Word 문서로 변환하는 OCR 프로그램이 있습니다. 문서라는 단어는 모든 이미지의 텍스트를 포함하고 있으며이를 별도의 파일로 나누고 싶습니다.C에서 Word 문서의 페이지를 개별 파일로 분할하는 방법 #
C#에서이 작업을 수행 할 수있는 방법이 있습니까?
감사
이미지를 Word 문서로 변환하는 OCR 프로그램이 있습니다. 문서라는 단어는 모든 이미지의 텍스트를 포함하고 있으며이를 별도의 파일로 나누고 싶습니다.C에서 Word 문서의 페이지를 개별 파일로 분할하는 방법 #
C#에서이 작업을 수행 할 수있는 방법이 있습니까?
감사
하지 쉽게 Word 문서의 끝 부분에, 말씀이 w와 문서 생성 불구하고 : lastRenderedPageBreak합니다.
변환 된 텍스트의 각 블록 사이에 문서에 마커를 삽입하는 것이 가장 좋습니다.
그런 다음 Word 문서의 종류에 따라 적절한 도구로 파일을 처리하십시오.
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();
}
}
}
}
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();
}
}
감사합니다 사랑 @ZevSpitz – Iman
을이 뭔가 유용한을 만들 수있는 완벽한 시작점이다. –