Title: Adding a text in the bottom of a PDF and generate another one PDF.

Good afternoon.

When I use iText to add a text in the bottom all pages of a PDF doc and generate second PDF it works fine if the original doc has more than 1 page, but when it only has one page the PDF generated has a frame and colors that are not in the original.

Could you help me please?

import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class ejemplo2
{
    private String pdf_inicial="";
    private String pdf_final="";
   
    public ejemplo2(){}
    public ejemplo2(String pdf_inicial, String pdf_final){
        this.pdf_inicial= pdf_inicial;
        this.pdf_final= pdf_final;
    }
    public void marcar(String texto){
        try {
            PdfReader reader = new PdfReader(pdf_inicial);
            int n = reader.getNumberOfPages();
            Rectangle psize = reader.getPageSize(1);
            float width = psize.width();
            float height = psize.height();
            Document document = new Document(psize, 0, 0, 0, 0);
            document.setPageSize(psize);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdf_final));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            int i = 0;
            while (i < n) {
                document.newPage();
                i++;
                PdfImportedPage page1 = writer.getImportedPage(reader, i);
                cb.addTemplate(page1,0,0);

                BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                cb.beginText();
                cb.setFontAndSize(bf, 8);
                cb.showTextAligned(PdfContentByte.ALIGN_CENTER, texto, width / 2, 20, 0);
                cb.endText();
            }
            document.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
    public static void main(String args[])
    {
       ejemplo2 ej2= new ejemplo2("c:\\temp\\Pdfmark.pdf","c:\\temp\\acrobat2.pdf");
       ej2.marcar("Este es el texto que quiero a�adir");
    }
}

I send the original PDF (acrobat.pdf) and generated PDF (acrobat2.pdf) when the original has only one page:
<<ACROBAT.PDF>> <<acrobat2.pdf>>

Thanks in advance.

Attachment: ACROBAT.PDF
Description: Binary data

Attachment: acrobat2.pdf
Description: Binary data

Reply via email to