Free Support Forum - aspose.com

Look back method / what is previous node?

Hi,

Can you please direct me as to an easy way to determine what the last node the builder added to a document? For example, I am trying to determine if the last thing added using the builder is a new page break. My code has several paths it takes. Some times a new page break is added, other times not. I need to make sure that I do not insert two page breaks right next to each other.

Thank you.

Hi

Thanks for your request. You can try using a code like the following:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a page break.

builder.InsertBreak(BreakType.PageBreak);

// Check whether the previously inserted node is a page break.

// Insert an empty run to get the previously inserted node.

Run emptyRun = new Run(doc);

builder.InsertNode(emptyRun);

// Get the previouse node.

Node prevNode = emptyRun.PreviousSibling;

if (prevNode != null && prevNode.NodeType == NodeType.Run && ((Run)prevNode).Text.EndsWith("\f"))

{

builder.Writeln("Previously we inserted a page break.");

}

// Save output.

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,

Thank you!

I need to create two additional conditionals looking at the previous node. The first one is to see if the previous node BreakType.SectionBreakContinuous. The second conditional is to see if the previous node BreakType.SectionBreakNewPage. Could you please help me modify the conditional statement you provided in the above example to handle the above section breaks?

Is it possible to find in “Aspose.Words Help” guide that ‘/f’ is equal to a page break?

Thank you.

Hi

Thanks for your request. You can modify the code as shown below:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

// Insert a page break.

builder.InsertBreak(BreakType.SectionBreakNewPage);

// Check where there is an inline node previously inserted

Run emptyRun = new Run(doc);

builder.InsertNode(emptyRun);

// Get the previouse node.

Node prevNode = emptyRun.PreviousSibling;

if (prevNode != null && prevNode.NodeType == NodeType.Run && ((Run)prevNode).Text.EndsWith("\f"))

{

builder.Writeln("Previously we inserted a page break.");

}

else if (prevNode == null)

{

// Get section where the empty run is inserted.

Section currentSection = (Section)emptyRun.GetAncestor(NodeType.Section);

// If the section is not the first section and the parent paragraph of empty run is the first child of the section

// we can suppose there was section break inserted.

if (!currentSection.Equals(doc.FirstSection) && currentSection.Body.FirstChild.Equals(emptyRun.ParentParagraph))

{

builder.Writeln("Previously we inserted a section break of type " + currentSection.PageSetup.SectionStart);

}

}

// Save output.

doc.Save(@"Test001\out.doc");

Hope this helps.

Best regards,

Hi Rob,


Thanks for your inquiry.

You can find a list of the control characters and their corresponding enumerations in the documentation here: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.controlcharmembers.html

If we can help with anything else, please feel free to ask.

Thanks,