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).
@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;
}
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.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.