Pregunta

Hi Please help to extracting pages from docx file according to page range like 2 - 4 or 10 - 15. i am using mentioned but it is not extracting correctly, please correct me where i need to change something code.

public void docx( string path,int pageStart,int pageend)
 {
var app = new Application();
  app.Visible = true;
  var doc = app.Documents.Open(path);
  //This Range object will contain each page.
      var page = doc.Range(pageStart, pageend);
      if (pageStart < pageend)
      {

          page.End = page.GoTo(What: WdGoToItem.wdGoToPage, Which: WdGoToDirection.wdGoToAbsolute, Count: pageStart + pageend).Start - pageStart;


      }
      else
      {
          page.End = doc.Range().End;
      }
      //Copy and paste the contents of the Range into a new document
      page.Copy();
      var doc2 = app.Documents.Add();
      doc2.Range().Paste();
} 
¿Fue útil?

Solución

This works for me

var range = doc.Range();
range.Start = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, pageStart).Start;

if (pageend < doc.ComputeStatistics(WdStatistic.wdStatisticPages, false))
{
    range.End = doc.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToAbsolute, pageend + 1).End - 1;
}

range.Copy();

The new range select the entire document, so its End is already the document's end. The start is set according to the start of the start page you need. The end is set as the beginning of page (pageend + 1), minus 1 character (to get back). This will bring us to the end of page pageend. This is only done if pageend is not the last page.

We could fit it all inside the range initialization, but that will make the code unreadable.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top