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 8513565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:36:24+00:00 2026-06-11T04:36:24+00:00

I am trying to convert image file (jpeg) in to pdf file in my

  • 0

I am trying to convert image file (jpeg) in to pdf file in my Adroid application. I have used itextpdf jar and droidtext jar. Neither work for me. Below is the code while using itextpdf.

Document document = new Document();

String directoryPath = Environment.getExternalStorageDirectory().toString();

File newPdfFile = new File(directoryPath, "textview8.pdf");

FileOutputStream fileOutputStream = null;

try {
    fileOutputStream = new FileOutputStream(newPdfFile);
} catch (FileNotFoundException fnfe) {
    Log.w(TAG, "# Exception caz of fileOutputStream : " + fnfe);
}

BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

try {
    PdfWriter.getInstance(document, bufferedOutputStream);
} catch (DocumentException de) {
    Log.w(TAG, "# Exception caz of PdfWriter.getInstance : " + de);
}

document.open(); 

Image image = null;

try {
    image = Image.getInstance(directoryPath + File.separator + "textview1.JPEG");
} catch (BadElementException bee) {
    Log.w(TAG, "# First exception caz of image : " + bee);
} catch (MalformedURLException mue) {
    Log.w(TAG, "# Second exception caz of image : " + mue);
} catch (IOException ioe) {
    Log.w(TAG, "# Third exception caz of image : " + ioe);
}

try {
    document.add(image);
} catch (DocumentException de) {
    Log.w(TAG, "# Exception caz of document.add : " + de);
}

try {
    bufferedOutputStream.flush();
    bufferedOutputStream.close();

    fileOutputStream.flush();
    fileOutputStream.close();

} catch (IOException ioe) {
    Log.w(TAG, "# Exception caz of bufferedOutputStream.flush : " + ioe);
}

document.close();

This gives me an error with a NullPointerException because of the code line document.close();

When I comment that line and run the program, it gives me the following error.

Could not find class 'com.itextpdf.awt.PdfPrinterGraphics2D', referenced from method com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes

But the class they are telling can’t find is already in the jar file, which means com.itextpdf.awt.PdfPrinterGraphics2D is existing in the project.

I have added the itextpdf-5.1.3.jar to the build path also. I tried this with a emulator as well as with a real device.

Can’t figure out what I have done wrong. Please help…

  • 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-06-11T04:36:26+00:00Added an answer on June 11, 2026 at 4:36 am

    Found a way to tackle with this problem. I have to do two changes in my code.

    1. Implement DocListener interface in my Activity
    2. Close streams after closing the document

    Here are the changes of the second point

    try {
        bufferedOutputStream.flush();
    
        fileOutputStream.flush();
    
        } catch (IOException ioe) {
            Log.w(TAG, "# Exception caz of flush : " + ioe);
        }
    
        document.close();
    
    try {
        bufferedOutputStream.close();
    
        fileOutputStream.close();
    
        } catch (IOException ioe) {
            Log.w(TAG, "# Exception caz of close : " + ioe);
    }     
    

    Still I cannot think the relationship with the Error given in the log and the working code :-/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to do some image file work, specifically convert one image file format
I'm trying to convert a grayscale image to a binary. The code I have
So I was trying to convert a Bitmap image to JPEG in code and
I'm trying to convert the following CURL: curl -X POST \ -H Content-Type: image/jpeg
I'm trying to convert dicom .dcm file to .jpeg using Imebra in C++ app
I'm trying to convert wmf image file into png format with c#.net. But, saved
I'm trying to convert a bitmap image into an uncompressed tif file for use
I am trying to convert a PDF to JPEG: $ convert pdf-test.pdf pdf-test.pdf.jpg However,
I'm trying to set a JPEG file's image orientation in ImageMagick, but not seeing
I am trying to convert the output of pyBarcode to a PIL Image file

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.