2015-03-11 33 views
5

Próbuję uzyskać wszystkie strony dokumentu MSWord przez Microsoft.Office.Interop.Word (używam C# w VS2012). To, co chciałbym uzyskać, to List < Ciąg> Strony, gdzie index to liczba stron. Rozumiem (przynajmniej tak uważam), że nie ma na to bezpośredniego sposobu. Więc wymyśliłem coś takiego:Pobierz strony dokumentu Word

 List<String> Pages = new List<String>(); 
     int NumberOfPreviousPage = -1; 
     int NumberOfPage = -1; 
     string InnerText = ""; 
     for (int i = 0; i < Doc.Paragraphs.Count; i++) 
     { 
      Paragraph CurrentParagraph = Doc.Paragraphs[i + 1]; 
      InnerText = CurrentParagraph.Range.Text; 
      NumberOfPage = CurrentParagraph.Range.get_Information(WdInformation.wdActiveEndPageNumber); 
      if (NumberOfPage == NumberOfPreviousPage) 
       Pages[Pages.Count - 1] += String.Format("\r\n{0}", InnerText); 
      else 
      { 
       Pages.Add(InnerText); 
       NumberOfPreviousPage = NumberOfPage; 
      } 
     } 

ale gdy dostaje się do ust algorytm, który rozpoczyna się na jednej stronie, a kończy się na innym, uzna, że ​​akapit powinien być na następnej stronie. Chcę podzielić ten akapit między strony, ale nie wiem, jak wykryć, gdzie muszę dokonać podziału.

+0

Zobacz również tutaj http://stackoverflow.com/a/12339771/74585 –

Odpowiedz

5

Ostatecznie skończyłem z tym, i to działa (jest kulawy, to brzydkie, ale robi to, co powinien):

public string[] GetPagesDoc(object Path) 
    { 
     List<string> Pages = new List<string>(); 

     // Get application object 
     Microsoft.Office.Interop.Word.Application WordApplication = new Microsoft.Office.Interop.Word.Application(); 

     // Get document object 
     object Miss = System.Reflection.Missing.Value; 
     object ReadOnly = false; 
     object Visible = false; 
     Document Doc = WordApplication.Documents.Open(ref Path, ref Miss, ref ReadOnly, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Miss, ref Visible, ref Miss, ref Miss, ref Miss, ref Miss); 

     // Get pages count 
     Microsoft.Office.Interop.Word.WdStatistic PagesCountStat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages; 
     int PagesCount = Doc.ComputeStatistics(PagesCountStat, ref Miss); 

     //Get pages 
     object What = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage; 
     object Which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute; 
     object Start; 
     object End; 
     object CurrentPageNumber; 
     object NextPageNumber; 

     for (int Index = 1; Index < PagesCount + 1; Index++) 
     { 
      CurrentPageNumber = (Convert.ToInt32(Index.ToString())); 
      NextPageNumber = (Convert.ToInt32((Index+1).ToString())); 

      // Get start position of current page 
      Start = WordApplication.Selection.GoTo(ref What, ref Which, ref CurrentPageNumber, ref Miss).Start; 

      // Get end position of current page         
      End = WordApplication.Selection.GoTo(ref What, ref Which, ref NextPageNumber, ref Miss).End; 

      // Get text 
      if (Convert.ToInt32(Start.ToString()) != Convert.ToInt32(End.ToString())) 
       Pages.Add(Doc.Range(ref Start, ref End).Text); 
      else 
       Pages.Add(Doc.Range(ref Start).Text); 
     } 
      return Pages.ToArray<string>(); 
    } 
Powiązane problemy