title: I want to add a Line into a PDF document using java
body: <p>I am currently using PDFBox and reading from within a.pdf  which is found in folder 1</p>
  
  <p>I first list all the Pdf files found within the folder.
  Then I check the number of pages that each file has.
  Now i want to go to the very end of the file below the footer to add an image that can be recognised by the printer to staple the pages since it will realise it has reached end of file.</p>
  
  <p>I have arrived till getting list of files and the number of pages.</p>
  
  <p>What command do i use to go to the end of the last page and write there.</p>
  
  <p>Should i transform the .pdf file into text or
  Should i be able to use PDPageContentStream</p>
  
  <p>This is the code I am currently using I am trying to test and see if a AAA string will be insterted into my last page of the pdf file. the project is executing with no errors but for some reason it is not being inserted into the pdf.</p>
  
  <pre><code>package pdfviewer;
  
  import java.io.*;
  import java.util.*;
  import java.util.List;
  import java.io.IOException;
  
  import org.apache.pdfbox.PDFReader;
  import org.apache.pdfbox.pdmodel.PDDocument;
  import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
  import org.apache.pdfbox.pdmodel.PDPage;
  import org.apache.pdfbox.pdmodel.common.PDRectangle;
  import org.apache.pdfbox.pdmodel.font.PDFont;
  import org.apache.pdfbox.pdmodel.font.PDType1Font;
  
  
  
  
  
  public class Main {
  
      /**
       * @param args the command line arguments
       */
  
           public static List flist()
         {
          List listfile = new ArrayList();
          String path = "C:/1";
          String files;
          File folder = new File(path);
          File[] listOfFiles = folder.listFiles();
  
          for (int i = 0; i &lt; listOfFiles.length; i++)
          {
              if (listOfFiles[i].isFile())
              {
                  files = listOfFiles[i].getName();
                  if (files.endsWith(".pdf") || files.endsWith(".PDF"))
                  {
                     listfile.add(listOfFiles[i]);
  
  
                  }
              }
          }
          System.out.println(listfile);
          return listfile;
  
      }
  
  public static void CheckPages(List a)
      {
          String dir = null;
  
          Object[] arraydir = a.toArray(new Object[0]);
  
          for (int i=0; i&lt; arraydir.length; i++)
          {
              int pages = 0;
              PDFont font = PDType1Font.HELVETICA_BOLD;
              float fontSize = 12.0f;
              dir = arraydir[i].toString();
              System.out.println(dir);
  
              try {
  
                      PDDocument pdoc = PDDocument.load(dir);
                      List allPages = pdoc.getDocumentCatalog().getAllPages();
  
                      pages = pdoc.getNumberOfPages();
                      System.out.println(allPages);
                      int f = pages;
                      System.out.println(pages);
  
                      PDPage page = (PDPage) allPages.get(i);
                      //System.out.println(page);
                      PDRectangle pageSize = page.findMediaBox();
                      float stringWidth = font.getStringWidth( "AAA" );
                      float centeredPosition = (pageSize.getWidth() - (stringWidth*fontSize)/1000f)/2f;
  
                      PDPageContentStream contentStream = new PDPageContentStream(pdoc,page,true,true);
                      //System.out.println(contentStream);
  
                      contentStream.beginText();
                      contentStream.setFont( font, fontSize );
                      contentStream.moveTextPositionByAmount( centeredPosition, 30 );
                      contentStream.drawString( "AAA" );
                      contentStream.endText();
                      contentStream.close();
  
  
                      pdoc.close();
  
                    }
              catch (Exception e)
                      {
                          System.err.println("An exception occured in parsing the PDF Document."+ e.getMessage());
                      }
          }
  
  }
      public static void main(String[] args)
      {
          List l = new ArrayList();
          l = pdfviewer.Main.flist();
          pdfviewer.Main.CheckPages(l);
  
  
      }
  
  }
  </code></pre>
  
  <p>Thanks for your attention</p>
  
  <hr>
  
  <p>The code I was using above is correct. 
  The problem is that the PDF files being generated are version 1.2, that is the reason why I am not being allowed to Edit the pdf document.</p>
  
  <p>Does anyone know what I should do if i'm using a version 1.2, since I can't really upgrade it.</p>