Convert Figma logo to code with AI

TomRoush logoPdfBox-Android

The Apache PdfBox project ported to work on Android

1,028
262
1,028
119

Top Related Projects

2,702

Mirror of Apache PDFBox

iText for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText can be a boon to nearly every workflow.

[DEPRECATED] A fast PDF reader component for Android development

Android view for displaying PDFs rendered with PdfiumAndroid

Quick Overview

PdfBox-Android is an open-source port of Apache PDFBox for Android devices. It provides a library for working with PDF documents on Android platforms, allowing developers to create, manipulate, and extract content from PDF files within their Android applications.

Pros

  • Native Android support, optimized for mobile devices
  • Comprehensive PDF manipulation capabilities, including creation, editing, and content extraction
  • Active development and community support
  • Compatible with a wide range of Android versions

Cons

  • May have performance limitations on low-end devices due to the complexity of PDF processing
  • Some features of the original Apache PDFBox may not be fully implemented or optimized for Android
  • Limited documentation specifically for the Android port
  • Potential compatibility issues with certain PDF features or complex documents

Code Examples

  1. Loading a PDF file:
PDDocument document = PDDocument.load(new File("path/to/file.pdf"));
  1. Extracting text from a PDF:
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
  1. Creating a new PDF document:
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
  1. Adding text to a PDF:
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello, PdfBox-Android!");
contentStream.endText();
contentStream.close();

Getting Started

To use PdfBox-Android in your project, follow these steps:

  1. Add the following dependency to your app's build.gradle file:
dependencies {
    implementation 'com.tom_roush:pdfbox-android:2.0.27.0'
}
  1. Initialize PdfBox in your application's onCreate() method:
public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        PDFBoxResourceLoader.init(getApplicationContext());
    }
}
  1. Use PdfBox-Android classes in your code as shown in the examples above.

Competitor Comparisons

2,702

Mirror of Apache PDFBox

Pros of PDFBox

  • Larger community and more active development
  • Broader feature set for PDF manipulation and creation
  • Better documentation and official Apache support

Cons of PDFBox

  • Not optimized for Android, may require additional configuration
  • Larger library size, potentially impacting app performance
  • May include unnecessary features for mobile applications

Code Comparison

PDFBox (Java):

PDDocument document = PDDocument.load(new File("example.pdf"));
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
document.close();

PdfBox-Android:

PDDocument document = PDDocument.load(new File("example.pdf"));
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
document.close();

Key Differences

  • PdfBox-Android is specifically tailored for Android development
  • PDFBox offers a wider range of PDF manipulation features
  • PdfBox-Android may have better performance on mobile devices
  • PDFBox has more frequent updates and bug fixes
  • PdfBox-Android may lack some advanced features present in PDFBox

Use Cases

PDFBox

  • Desktop applications
  • Server-side PDF processing
  • Complex PDF manipulation tasks

PdfBox-Android

  • Android app development
  • Mobile PDF viewing and basic editing
  • Lightweight PDF processing on Android devices

iText for Java represents the next level of SDKs for developers that want to take advantage of the benefits PDF can bring. Equipped with a better document engine, high and low-level programming capabilities and the ability to create, edit and enhance PDF documents, iText can be a boon to nearly every workflow.

Pros of itext-java

  • More comprehensive PDF manipulation capabilities, including advanced features like digital signatures and PDF/A conversion
  • Better documentation and extensive API reference
  • Larger community and more frequent updates

Cons of itext-java

  • Commercial license required for many use cases, which can be costly
  • Steeper learning curve due to its extensive feature set
  • Larger library size, potentially increasing app size

Code Comparison

itext-java:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf);
document.add(new Paragraph("Hello World!"));
document.close();

PdfBox-Android:

PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.beginText();
contentStream.showText("Hello World!");
contentStream.endText();
contentStream.close();
document.save(dest);
document.close();

The code comparison shows that itext-java offers a more concise syntax for basic PDF creation, while PdfBox-Android requires more explicit steps. However, PdfBox-Android provides a more granular control over the PDF structure, which can be beneficial for certain use cases.

[DEPRECATED] A fast PDF reader component for Android development

Pros of android-pdfview

  • Lightweight and focused specifically on PDF rendering for Android
  • Simpler API for basic PDF viewing tasks
  • Smoother zooming and scrolling performance

Cons of android-pdfview

  • Less feature-rich compared to PdfBox-Android
  • Limited PDF manipulation capabilities
  • Less active development and community support

Code Comparison

android-pdfview:

PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset("example.pdf")
    .defaultPage(0)
    .onLoad(this)
    .load();

PdfBox-Android:

PDDocument document = PDDocument.load(new File("example.pdf"));
PDFRenderer renderer = new PDFRenderer(document);
Bitmap bitmap = renderer.renderImage(0, 1.0f);
imageView.setImageBitmap(bitmap);
document.close();

android-pdfview offers a more straightforward API for basic PDF viewing, while PdfBox-Android provides more low-level control and additional features for PDF manipulation. The choice between the two depends on the specific requirements of your Android application and the level of PDF functionality needed.

Android view for displaying PDFs rendered with PdfiumAndroid

Pros of AndroidPdfViewer

  • Simpler implementation for basic PDF viewing functionality
  • Faster rendering and scrolling performance for large PDF files
  • Built-in zooming and page navigation features

Cons of AndroidPdfViewer

  • Limited PDF manipulation capabilities compared to PdfBox-Android
  • Less flexibility for advanced PDF processing tasks
  • Smaller community and fewer updates

Code Comparison

AndroidPdfViewer:

PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset("sample.pdf")
    .defaultPage(0)
    .enableSwipe(true)
    .load();

PdfBox-Android:

PDDocument document = PDDocument.load(new File("sample.pdf"));
PDFRenderer renderer = new PDFRenderer(document);
Bitmap bitmap = renderer.renderImage(0, 1.0f);
imageView.setImageBitmap(bitmap);
document.close();

AndroidPdfViewer provides a more straightforward API for basic PDF viewing, while PdfBox-Android offers more control over PDF processing and rendering. AndroidPdfViewer is better suited for simple PDF display tasks, whereas PdfBox-Android is more appropriate for complex PDF manipulation and analysis projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

PdfBox-Android

Maven Central Build Status

A port of Apache's PdfBox library to be usable on Android. Most features should be implemented by now. Feature requests can be added to the issue tracker. Stable releases can be added as a Gradle dependency from Maven Central.

The main code of this project is licensed under the Apache 2.0 License, found at http://www.apache.org/licenses/LICENSE-2.0.html

Usage

Add the following to dependency to build.gradle:

dependencies {
    implementation 'com.tom-roush:pdfbox-android:2.0.27.0'
}

Before calls to PDFBox are made it is required to initialize the library's resource loader. Add the following line before calling PDFBox methods:

PDFBoxResourceLoader.init(getApplicationContext());

An example app is located in the sample directory and includes examples of common tasks.

Optional Dependencies

PdfBox-Android can optionally make use of additional features provided by third-party libraries. These libraries are not included by default to reduce the size of the PdfBox-Android. See the dependencies section in thebuild.gradle of the Sample project for examples of including the optional dependencies.

Reading JPX Images

Android does not come with native support for handling JPX images. These images can be read using the JP2Android library. As JPX is not a common image format, this library is not included with PdfBox-Android by default. If the JP2Android library is not on the classpath of your application, JPX images will be ignored and a warning will be logged.

To include the JP2Android library, add the following to your project's Gradle dependencies section. Note that this library is available in JCenter only, so you will need to add jcenter() to your repository list.

dependencies {
    implementation 'com.gemalto.jp2:jp2-android:1.0.3'
}

Important notes

  • Currently based on PDFBox v2.0.27

  • Requires API 19 or greater for full functionality