Skip to main content
Inspiring
January 21, 2026
Answered

Delete only EMPTY Trailing Paragraphs

@frameexpert - I'm not sure if it was supplied by you or someone else, but I have a script that deletes empty paragraphs at the end of a document, but it deletes the paragraph if it doesn't contain text (i.e. it will delete paragraphs with an anchor for an anchored frame or with a table anchor also.) I need to revise the script to ONLY delete empty paragraphs.

As a workaround, I've told our writers to add a couple of spaces before or after the marker, which solves the problem, but it's not a permanent solution.
This is the script:

var doc = app.ActiveDoc;
DeleteBlankParagraphs(doc);
function DeleteBlankParagraphs(doc)
{

    var pgf;

    // Get the last paragraph in the main flow.
    pgf = GetLastParagraph(doc);
    // Loop backwards through the empty paragraphs.
    while (pgf.ObjectValid () === 1)
    {
        // If the paragraph is empty, delete it.
        if (getPgfText (pgf) === "")
        {
            pgf.Delete ();
        }
        else
        { // Otherwise, exit the loop.
            break;
        }
//        pgf =  GetLastParagraph(doc);
    }
}
function GetLastParagraph(doc)
{
    var pgf
    // Loop through the paragraphs in the document's main flow.
    pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
    while (pgf.ObjectValid() === 1)
    {
        //            text = getPgfText(pgf, doc);
        //            Console (text);
        if (pgf.NextPgfInFlow.ObjectValid()===0)
        {
            return pgf;
        }
        else
        {
            pgf=pgf.NextPgfInFlow;
        }
    }
} // End GetLastParagraph

function getPgfText(pgf)
{
    var text, textList, count, i;
    textList = pgf.GetText (Constants.FTI_String);
    text = "";
    count = textList.length;
    for (i = 0; i < count; i += 1)
    {
        text += (textList[i].sdata);
    }
    return text;
} //end getPgfText

Thank you in advance!!!

frameexpert
frameexpertCorrect answer
Community Expert
January 21, 2026

You can plug this into your script. This is more reliable than testing for no text in a paragraph. You should be able to figure out how to call it with your code.

function pgfIsEmpty (pgf) {
    
    var textItems;

    textItems = pgf.GetText (Constants.FTI_PgfEnd);
    if (textItems[0].offset === 0) {
        if (pgf.PgfNumber === "") {
            if (pgf.BottomSeparator === "") {
                if (pgf.TopSeparator === "") {
                    return true;
                }
            }
        }
    }
    return false;
}
www.frameexpert.com

7 replies

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
January 21, 2026

You can plug this into your script. This is more reliable than testing for no text in a paragraph. You should be able to figure out how to call it with your code.

function pgfIsEmpty (pgf) {
    
    var textItems;

    textItems = pgf.GetText (Constants.FTI_PgfEnd);
    if (textItems[0].offset === 0) {
        if (pgf.PgfNumber === "") {
            if (pgf.BottomSeparator === "") {
                if (pgf.TopSeparator === "") {
                    return true;
                }
            }
        }
    }
    return false;
}
Translate
www.frameexpert.com
Inspiring
January 21, 2026

@frameexpert - thanks - I'll try that out, but I'm sure it works!!!

Also - something weird is going on with the forum - I posted my own solution, but it is here: https://community.adobe.com/t5/framemaker-discussions/delete-only-empty-trailing-paragraphs/m-p/15671943#M88958 - I didn't double-post, but I got a lot of "Something went wrong" errors. Maybe a moderator can merge these?

 

Translate
frameexpert
Community Expert
Community Expert
January 21, 2026

It might be the forum. I got one of those earlier today.

Translate
www.frameexpert.com
Inspiring
January 21, 2026

@frameexpert - I'm not sure if it was supplied by you or someone else, but I have a script that deletes empty paragraphs at the end of a document, but it deletes the paragraph if it doesn't contain text (i.e. it will delete paragraphs with an anchor for an anchored frame or with a table anchor also.) I need to revise the script to ONLY delete empty paragraphs.

As a workaround, I've told our writers to add a couple of spaces before or after the marker, which solves the problem, but it's not a permanent solution.
This is the script:

var doc = app.ActiveDoc;
DeleteBlankParagraphs(doc);
function DeleteBlankParagraphs(doc)
{

    var pgf;

    // Get the last paragraph in the main flow.
    pgf = GetLastParagraph(doc);
    // Loop backwards through the empty paragraphs.
    while (pgf.ObjectValid () === 1)
    {
        // If the paragraph is empty, delete it.
        if (getPgfText (pgf) === "")
        {
            pgf.Delete ();
        }
        else
        { // Otherwise, exit the loop.
            break;
        }
//        pgf =  GetLastParagraph(doc);
    }
}
function GetLastParagraph(doc)
{
    var pgf
    // Loop through the paragraphs in the document's main flow.
    pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
    while (pgf.ObjectValid() === 1)
    {
        //            text = getPgfText(pgf, doc);
        //            Console (text);
        if (pgf.NextPgfInFlow.ObjectValid()===0)
        {
            return pgf;
        }
        else
        {
            pgf=pgf.NextPgfInFlow;
        }
    }
} // End GetLastParagraph

function getPgfText(pgf)
{
    var text, textList, count, i;
    textList = pgf.GetText (Constants.FTI_String);
    text = "";
    count = textList.length;
    for (i = 0; i < count; i += 1)
    {
        text += (textList[i].sdata);
    }
    return text;
} //end getPgfText

Thank you in advance!!!

Translate
Inspiring
January 21, 2026

Solved - Rather crude, but ... it works!!!

var doc = app.ActiveDoc;
DeleteBlankParagraphs(doc);
function DeleteBlankParagraphs(doc)
{

    var pgf;

    // Get the last paragraph in the main flow.
    pgf = GetLastParagraph(doc);
    // Loop backwards through the empty paragraphs.
    while (pgf.ObjectValid () === 1)
    {
        // If the paragraph is empty, delete it.
        if (getPgfText (pgf) === "" && getPgfTblAnchor (pgf)=== false && getPgfAFrame (pgf)===false)
        {
            pgf.Delete ();
        }
        else
        { // Otherwise, exit the loop.
            break;
        }
//        pgf =  GetLastParagraph(doc);
    }
}

function GetLastParagraph(doc)
{
    var pgf
    // Loop through the paragraphs in the document's main flow.
    pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
    while (pgf.ObjectValid() === 1)
    {
        //            text = getPgfText(pgf, doc);
        //            Console (text);
        if (pgf.NextPgfInFlow.ObjectValid()===0)
        {
            return pgf;
        }
        else
        {
            pgf=pgf.NextPgfInFlow;
        }
    }
} // End GetLastParagraph

function getPgfText(pgf)
{
    var text, textList, count, i;
    textList = pgf.GetText (Constants.FTI_String);
    text = "";
    count = textList.length;
    for (i = 0; i < count; i += 1)
    {
        text += (textList[i].sdata);
    }
    return text;
} //end getPgfText

function getPgfTblAnchor(pgf)
{
    var text, textList, count;
    textList = pgf.GetText (Constants.FTI_TblAnchor);
    text = false;
    count = textList.length;
    if (count>0)
    {
        text = true
     }
    return text;
} //end getPgfTblAnchor

function getPgfAFrame(pgf)
{
    var text, textList, count;
    textList = pgf.GetText (Constants.FTI_FrameAnchor);
    text = false;
    count = textList.length;
    if (count>0)
    {
        text = true
     }
    return text;
} //end getPgfAFrame
Translate