0

I have a word document which contains multiple pages and i want to copy some pages into new word document using OpenXml SDK. I did some web search and got below code which reads entire document and copies into new one

 string documentURL = filelocation;
 byte[] docAsArray = File.ReadAllBytes(documentURL);

 using (MemoryStream stream = new MemoryStream())
  {
    stream.Write(docAsArray, 0, docAsArray.Length);    // THIS performs doc copy
    using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true))
    {
                // perform content control substitution here, making sure to call .Save()
                // on any documents Part's changed.
    }

 File.WriteAllBytes(outputSplitDocpath, stream.ToArray());
 }

Now, in the above code how can i read just specific pages and copy into new one? Please help with suggestions. Thanks

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Lara
  • 2,821
  • 7
  • 39
  • 72

2 Answers2

0

Unless a manual page break has been used to generate every page in the document, what you want to do is not possible.

Automatic page breaks are generated by Word, at run-time, when the document is open in the Word application. The actual placement of a page break is completely dynamic, based on the editing being done and is recalculated "all the time" during editing.

This information is not reliably saved in the document when the document is closed. One reason for this is because the document could lay out differently when opened on a different machine, or when a different printer (driver) is selected.

So it's not possible to work with individual pages using the Office Open XML file format unless there's some way each page can be recognized, such as a manual page break.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
-2

Use Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); instead of OpenXML

//dummy value to satisfy params
object oMissing = System.Reflection.Missing.Value;

//copy specific page/s
object what = WdGoToItem.wdGoToPage;
object which = WdGoToDirection.wdGoToFirst;
object count1 = 1;
Range startRange = word.Selection.GoTo(ref what, ref which, ref count1, ref oMissing);
object count2 = (int)count + 1;
Range endRange = word.Selection.GoTo(ref what, ref which, ref count2, ref oMissing);
endRange.SetRange(startRange.Start, endRange.End - 1);
endRange.Select();
word.Selection.Copy();
//save...
Salomon Zhang
  • 1,553
  • 3
  • 23
  • 41
  • Any comments for downvoting? – Salomon Zhang Jul 10 '19 at 00:16
  • 2
    Probably because the OP is asking about OpenXML. Clearly you can use the interop to make this done, but that will very likely require a different architecture. OpenXML can be deploy to servers without Office installed, while Interop's not. – Steven.Xi Jan 19 '21 at 11:43