How to delete a page in a doc?

Hi there,

I have a template, and if for some condition (say it if the record count is zero i want to delete a certain page in the template document. I have another workaround but it is very inefficient that is to create many template docs as the combination can be so many (16 templates).

How to work around?

regards and thanks
TemplatedocMorePage.docx (68.0 KB)

@ibox There is no direct way to remove pages from MS Word document, since MS Word document are flow by their nature and there is no “page” concept. The consumer application reflows the document into pages on the fly. However, Aspose.Words provides a method for extracting page range from the document Document.ExtractPages. So to achieve what you need you can use code like the following:

Document doc = new Document("C:\\Temp\\in.docx");
Document doc1 = GetPagesExcept(doc, new int[] {0,2,8});
doc1.Save("C:\\Temp\\out.docx");
private static Document GetPagesExcept(Document doc, int[] excludePages)
{
    int startPageIndex = 0;
    int pageCount = doc.PageCount;

    if (excludePages[excludePages.Length - 1] >= pageCount)
        throw new IndexOutOfRangeException("Page index is out of range");

    Document mainDocument = (Document)doc.Clone(false);
    foreach (int page in excludePages)
    {
        if (page == startPageIndex)
        {
            startPageIndex++;
            continue;
        }

        int chunkCount = page - startPageIndex;

        Document chunk = doc.ExtractPages(startPageIndex, chunkCount);
        mainDocument.AppendDocument(chunk, ImportFormatMode.UseDestinationStyles);

        startPageIndex = page + 1;
    }

    return mainDocument;
}

Thank You, Sir

Is it possible to delete some part of the document? Here I attach a picture, suppose the “Threat Assessment, [btreatementtable], and Table: Threat assessment” is deleted such that the “Hazard Assesment, [bhazardtable], and table: Hazard Assessment” will go up to fill the above deleted.

Best Regards

@ibox Sure, you can simply delete the nodes from the document tree and the rest of document’s content will be reflowed accordingly. Please see our documentation to learn more about Aspose.Words Document Object Model:
https://docs.aspose.com/words/net/aspose-words-document-object-model/

Thank You, Sir.

I’ll read it.
I see that there is a document explorer tool, how to install and use it?

regards

@ibox s DocumentExplorer is a demo application. You can get DocumentExplorer sources from our Github .

Thank u Sir,

How to download it?

regards

@ibox Just clone the repository and build DocumentExplorer demo.

Thank u Sir,
It works there is already the execution (application) in the folder Debug.

regards

But, I wonder how to identify a paragraph in the program?

I want to delete a paragraph that contains text: Threat assessment

But in the Document Explorer, there are two Run Nodes: Threat and assessment.

Do i need to check one by one, whether this paragraph contains Run Threat and Run assessment?

regards

@ibox You can get whole paragraph text and remove it is it passes the condition:

Document doc = new Document(@"C:\Temp\in.docx");

foreach (Paragraph p in doc.GetChildNodes(NodeType.Paragraph, true))
{
    string paraText = p.ToString(SaveFormat.Text).Trim();
    if(paraText == "Remove me")
        p.Remove();
}

doc.Save(@"C:\Temp\out.docx");

Thank you, Sir
It works
regards

1 Like