Kevin Wilson
Kevin Wilson

Reputation: 11

copy individual pages from word document to new document c#

I have a report that has hundreds of pages. I need to create extract each individual page from this document into a new document. I have found that this is possible using INTEROP, however I'm trying avoid installing MS Office on the server. I've been using ASPOSE for most of the operations, but this functionality is doesn't appear to be supported.

Is there a way to seperate pages of a document into individual files without having MS Office Installed?

Upvotes: 1

Views: 2908

Answers (1)

Saqib Razzaq
Saqib Razzaq

Reputation: 1430

Aspose.Words does not have layout information like pages or line numbers. It maintains DOM. But we have written some utility classes to achieve such behavior. They split the word document into multiple sections, such that each page becomes one separate section. After that, it is easy to copy individual pages.

String sourceDoc = dataDir + "source.docx";
String destinationtDoc = dataDir + "destination.docx";

// Initialize the Document instance with source and destination documents
Document doc = new Document(sourceDoc);
Document dstDoc = new Document();

// Remove the blank default page from new document
dstDoc.RemoveAllChildren();
PageNumberFinder finder = new PageNumberFinder(doc);
// Split nodes across pages
finder.SplitNodesAcrossPages(true);
// Get separate page sections
ArrayList pageSections = finder.RetrieveAllNodesOnPages(1, 5, NodeType.Section);
foreach (Section section in pageSections)
    dstDoc.AppendChild(dstDoc.ImportNode(section, true));
dstDoc.LastSection.Body.LastParagraph.Remove();
dstDoc.Save(destinationtDoc);

The PageNumberFinder class can be downloaded from here.

PS. I am a Developer Evangelist at Aspose.

Upvotes: 2

Related Questions