Android and Apache PDFBox
To create pdf using your app,
You can use PdfDocument if your app is for users who have android devices having api 19 or above.
Otherwise you can use open-sourced library named Android PDF Writer
I hope it will be helpful !!
How to add PDFBox to an Android project or suggest alternative
PDFBox uses java awt and swing, even for non UI tasks, I've tried to remove references but there are a lot of files, and I was removing too much stuff
I've just tested PDFjet http://pdfjet.com/os/edition.html it's bsd licensed (plus commercial version with more features), with this sample code (ripped from Example_03.java) I was able to convert a jpeg to a pdf
FileOutputStream fos = null;
try
{
fos = new FileOutputStream("/sdcard/sample.pdf");
PDF pdf = new PDF(fos);
InputStream f = getApplicationContext().getAssets().open("img0.jpg");
Image image = new Image(pdf, f, ImageType.JPEG);
Page page = new Page(pdf, A4.PORTRAIT);
image.setPosition(0, 0);
image.drawOn(page);
pdf.flush();
fos.close();
} catch (Exception e)
{
e.printStackTrace();
}
I found the link here http://java-source.net/open-source/pdf-libraries
PDFBox in Android or other means to extract text from PDF on device?
I needed similar functionality for my app so I've tried solution suggested by Mike M. in comments under your question and it worked great for me (so this is really his answer – I just confirmed that it works and supplied the code).
Hope it helps.
The “magic” is actually in these two lines:
InputStream inputStream = this.getContentResolver().openInputStream(fileUri);
document = PDDocument.load(inputStream);
But for some context (and for those who will search an answer for this problem on another occasion) here is whole example code:
public class MainActivity extends AppCompatActivity {
private static final int OPEN_FILE_REQUEST_CODE = 1;
Intent intentOpenfile;
Uri fileUri;
TextView tvTextDisplay;
Button bOpenFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTextDisplay = findViewById(R.id.tv_text_display);
PDFBoxResourceLoader.init(getApplicationContext());
bOpenFile = findViewById(R.id.b_open_file);
bOpenFile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentOpenfile = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intentOpenfile.setType("application/pdf");
startActivityForResult(intentOpenfile, OPEN_FILE_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OPEN_FILE_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
fileUri = data.getData();
PDDocument document = null;
String parsedText = null;
try {
InputStream inputStream = this.getContentResolver().openInputStream(fileUri);
document = PDDocument.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
try {
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(1);
parsedText = "Parsed text: " + pdfStripper.getText(document);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (document != null) document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
tvTextDisplay.setText(parsedText);
}
}
}
}
PDF size too large generating through Android PDFDocument. And while using pdfbox it is cutting image in output
In your android pdf document library code you set the page size to the image height and width values
PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
and draw the image at the origin:
canvas.drawBitmap(scaledBitmap, 0f, 0f, null);
You can do the same in your PDFBox code:
PDDocument document = new PDDocument();
PDImageXObject ximage = JPEGFactory.createFromStream(document,imageResource);
PDPage page = new PDPage(new PDRectangle(ximage.getWidth(), ximage.getHeight()));
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(ximage, 0, 0);
contentStream.close();
(DrawImage test testDrawImageToFitPage
)
Alternatively, as discussed in comments, you can set the current transformation matrix before drawing the image to scale it down to fit the page.
Create PDF that Android devices could read
If you generate a non-XFA PDF you'll probably have more luck with it. The XFA spec is large, complicated and not well supported. Adobe Reader supports it but not many other readers (on Android or on desktop).
Related Topics
How to Add Background Image to Activity
How to Convert Time to " Time Ago " in Android
On Android, How to Switch Activities Programmatically
Set a Consistent Style to All Edittext (For E.G.)
How to Get Results from an Intentservice Back into an Activity
How to Make an Android View That Flips Between Views on Swipe/Fling
Listfragment Does Not Accept My Layout
Redirect Users to Itunes App Store or Google Play Store
How to Be Notified When a Snackbar Has Dismissed Itself
How to Detect Android Listview Scrolling Stopped
Android Active Link of Url in Textview
Move Snackbar Above The Bottom Bar
How to Store Object in Sqlite Database
How to Disable "Next" Button on a Edittext Software Keyboard (Replace with "Done" Button)
Android: How to Get a Modal Dialog or Similar Modal Behavior
Difference Between Getview & Getdropdownview in Spinneradapter