Resolving Acrobat Reader error after adding text block to an exiting PDF

Q: We have a c# method which adds a footer (multi line text) to a pdf
document.
For some documents, Adobe Reader raises an error.

Could you tell us what is wrong in this method and how to fix it.

static void Paginer(PDFDoc pdfDoc, int pageStart, string texteGauche,
pdftron.PDF.Font pdfFont, double policeSize)
{
    const double margeLeft = 20;
    const double margeRight = 20;
    const double margeBottom = 20;

    //Remplacer les caractères encodés
    string texteBasGauche = texteGauche;
    if (!string.IsNullOrEmpty(texteBasGauche))
        texteBasGauche =
texteBasGauche.Trim().UnWordify().RemoveDiacriticsOnMajuscules();

    //Ecrire le texte en pied de page de chaque page sauf la première
    PageIterator itr = pdfDoc.GetPageIterator();
    while (itr.HasNext())
    {
        Page currentPage = itr.Current();
        double pageWidth = currentPage.GetPageWidth();
        if (currentPage.GetIndex() >= pageStart)
        {
            using (ElementWriter writer = new ElementWriter())
            {
                writer.Begin(currentPage);
                using (ElementBuilder builder = new ElementBuilder())
                {
                    //Démarrer "NUMERO PAGE"
                    builder.Reset();
                    Element elementNumPageStart =
builder.CreateTextBegin(pdfFont, policeSize);
                    writer.WriteElement(elementNumPageStart);

                    //Placer le numéro de page en bas à droite
                    Element footerRight =
builder.CreateTextRun(string.Format("Page " +
currentPage.GetIndex()));
                    double footerRightSize =
footerRight.GetTextLength();
                    Matrix2D mtxRight = new Matrix2D();
                    mtxRight.Translate(currentPage.GetPageWidth() -
footerRightSize - margeRight, policeSize + margeBottom);
                    footerRight.SetTextMatrix(mtxRight);
                    writer.WriteElement(footerRight);

                    //En même temps, regarder si on pourrait écrire le
texte sur la même ligne
                    bool isTexteMultiLignes = false;
                    int nbLignes = 0;
                    double texteSizeMax = 0;

                    if (!string.IsNullOrEmpty(texteBasGauche))
                    {
                        //Calculer la taille du texte
                        Element footerLeft =
builder.CreateTextRun(texteBasGauche);
                        double footerLeftSize =
footerLeft.GetTextLength();
                        texteSizeMax = pageWidth - margeLeft -
margeRight - footerRightSize;

                        //Calculer le nombre de lignes nécessaires
                        nbLignes =
((int)Math.Truncate(footerLeftSize / texteSizeMax)) + 1;

                        //Ecrire le texte si ok
                        if (nbLignes == 1)
                        {
                            Matrix2D mtxLeft = new Matrix2D();
                            mtxLeft.Translate(margeLeft, policeSize +
margeBottom);
                            footerLeft.SetTextMatrix(mtxLeft);
                            writer.WriteElement(footerLeft);
                        }
                        else
                        {
                            isTexteMultiLignes = true;
                        }
                    }

                    //Terminer "NUMERO PAGE"

writer.WriteElement(builder.CreateTextEnd());

                    //Cas d'un texte qui tient sur plusieurs lignes
                    if (isTexteMultiLignes)
                    {
                        double interligne = policeSize;
                        double y1FirstLigne = (nbLignes*interligne) +
margeBottom;
                        double x2Max = margeLeft + texteSizeMax;

                        EcrireMultiLignes(writer, builder,
texteBasGauche, pdfFont, policeSize, interligne, margeLeft,
y1FirstLigne, x2Max);
                    }

                    //Terminer l'écrire du texte

writer.WriteElement(builder.CreateTextEnd());
                }
                writer.End();
            }
        }
        itr.Next();
    }
}
-----------------
A: The problem is that sometimes you are creating multiple 'Text end'
elements (writer.WriteElement(builder.CreateTextEnd())). Each 'text
begin' needs to be closed by exactly one 'text end' and text blocks
can't nest.

To simplify process of adding stamps we have recently added Stamper
utility class 'pdftron.PDF.Stamper' (it can be used as shown in
Stamper sample project).