none
How to remove last page in document if it only contains whitespace characters RRS feed

  • Question

  • Hi,

    I have a C# application that creates a document with a lot of Tables and Graphics.
    It works perfectly fine, except that I often end up with a blank page at the end of my document.

    Sometimes my documents start a new section at each page (when the document shifts from Portrait to Landscape mode at each page) and sometimes the document has only one section when it only uses Portrait mode or only uses Landscape mode.

    I need a solution that removes the last page of the document if it does not contain any visual character (except for the header and footer of course).

    So, if the last document page contains only blanks, carriage returns and/or line feeds, I'd like to programmatically find out that and programmatically remove the last page.

    I searched for a good while, but can't find a good solution.

    Could you help and provide some snippet C# code for doing that?

    Thanks.

    Eric

    Wednesday, March 23, 2011 4:49 PM

Answers

  • Hi Bruce,

    Thanks very much.
    I will look at the links you sent.
    FYI, in the meantime, I have finally written some code that works for me:

        public void RemoveLastPageIfBlank()
        {
          Word.Paragraph paragraph;
          Word.Paragraphs paragraphs = oDoc.Paragraphs;
          for (int i = paragraphs.Count; i > 0; i--)
          {
            paragraph = paragraphs[i];
            if (paragraph.Range.Text.Trim() == "")
            {
              paragraph.Range.Select();
              word.Selection.Delete();
            }
            else
              break;
          }
        }
    
    

    Eric

    • Marked as answer by Bruce Song Wednesday, March 30, 2011 10:59 AM
    Thursday, March 24, 2011 12:15 PM

All replies