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
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 =

    //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())
                using (ElementBuilder builder = new ElementBuilder())
                    //Démarrer "NUMERO PAGE"
                    Element elementNumPageStart =
builder.CreateTextBegin(pdfFont, policeSize);

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

                    //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 =
                        double footerLeftSize =
                        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 +
                            isTexteMultiLignes = true;

                    //Terminer "NUMERO PAGE"


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

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

                    //Terminer l'écrire du texte

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).