Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6528609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:33:01+00:00 2026-05-25T09:33:01+00:00

I am currently using PDFBox and reading from within a.pdf which is found in

  • 0

I am currently using PDFBox and reading from within a.pdf which is found in folder 1

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.

I have arrived till getting list of files and the number of pages.

What command do i use to go to the end of the last page and write there.

Should i transform the .pdf file into text or
Should i be able to use PDPageContentStream

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.

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


    }

}

Thanks for your attention


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.

Does anyone know what I should do if i’m using a version 1.2, since I can’t really upgrade it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T09:33:02+00:00Added an answer on May 25, 2026 at 9:33 am

    you can look at the examples supplied with the library.
    there are two files that are of interest to you:

    1- AddImageToPDF.java AddImageToPDF.java on google code search

    2- AddMessageToEachPage.java AddMessageToEachPage.java on google code search

    the second one adds a message to every page but you can modify it to work with the last page only. according to the PDFBox user guide document, they should be found under the folder: src/main/java/org/apache/pdfbox/examples
    I have added links on google code search in case you have trouble locating the files.

    I have not worked with the library or tried the examples and I am quite sure you will need to modify the code a little to suit your needs for the location of the added line/image.
    In any case, if this helps you and you get a working solution, you can add the solution so that others can benefit from it.

    EDIT:
    After seeing the code posted by the question author, I add a modification to make it work.
    I allowed myself also to make few changes for clarity.

    import java.io.File;
    import java.io.FileFilter;
    import java.util.List;
    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 final FileFilter pdfFileFilter = new FileFilter() {
    
            public boolean accept(File file) {
                return file.isFile() && file.getName().toLowerCase().endsWith(".pdf");
            }
        };
    
        public static void closeQuietly(PDDocument doc) {
            if (doc != null) {
                try {
                    doc.close();
                } catch (Exception exception) {
                    //do something here if you wish like logging 
                }
            }
        }
    
        public static void CheckPages(File[] sourcePdfFiles,String textToInsert, String prefix) {
    
            for (File sourcePdfFile : sourcePdfFiles) {
                PDFont font = PDType1Font.HELVETICA_BOLD;
                float fontSize = 12.0f;
                PDDocument pdoc = null;
                try {
    
                    pdoc = PDDocument.load(sourcePdfFile);
                    List allPages = pdoc.getDocumentCatalog().getAllPages();
                    PDPage lastPage = (PDPage) allPages.get(allPages.size() - 1);
                    PDRectangle pageSize = lastPage.findMediaBox();
                    float stringWidth = font.getStringWidth(textToInsert);
                    float centeredPosition = (pageSize.getWidth() - (stringWidth * fontSize) / 1000f) / 2f;
    
                    PDPageContentStream contentStream = new PDPageContentStream(pdoc, lastPage, true, true);
    
                    contentStream.beginText();
                    contentStream.setFont(font, fontSize);
                    contentStream.moveTextPositionByAmount(centeredPosition, 30);
                    contentStream.drawString(textToInsert);
                    contentStream.endText();
                    contentStream.close();
    
                    File resultFile = new File(sourcePdfFile.getParentFile(), prefix + sourcePdfFile.getName());
                    pdoc.save(resultFile.getAbsolutePath());
    
    
                } catch (Exception e) {
                    System.err.println("An exception occured in parsing the PDF Document." + e.getMessage());
                } finally {
                    closeQuietly(pdoc);
                }
            }
        }
    
        public static void main(String[] args) {
            File pdfFilesFolder = new File("C:\\1");
            File[] pdfFiles = pdfFilesFolder.listFiles(pdfFileFilter);
            //when a file is processed, the result will be saved in a new file having the location of the source file 
            //and the same name of source file prefixed with this
            String modifiedFilePrefix = "modified-";
            CheckPages(pdfFiles,"AAA", modifiedFilePrefix);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently using the following command in bash to list the info from an
I'm writing a java app that creates a pdf from scratch using the pdfbox
I'm currently using DPack as this adds a Collapse All Projects option to the
Our team is currently using some ported code from an old architecture to a
I'm currently using exif_read_data to load the information from JPG, JPEG, TIFF and TIF
Currently using Telerik ASP .NET MVC Controls version 2011.2.712 Hello all, I am trying
Im currently using the Barcode Scanner from the open source library Zxing in my
Im currently using PHP to fetch results from a mysql db. Im also displaying
Im currently using jquery to link all td's to the edit link but i
Hi all I am currently using FLEX version 3.0 It just take a long

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.