How to set PDF file size using PdfDocument on Android

I need to convert text and a small image to a PDF document on Android, and for this I use PdfDocument added in API 19.

The fact is that the PDF generated by this is about 3 times larger (file size) than the one generated by iOS.

In addition, I tried to compress both Android and iOS PDF files using the free online compression tool, and the following results were obtained:

  • iOS : compression could not do anything because it was compressed enough

  • Android : the resulting PDF was the same as with iOS (about 3 times smaller)

Even if I delete the image from the document, the Android PDF will take about 2 times more space than the PDF from iOS, which also contains the image (as I said, the image is small).

As I could see, there is no way or property to set a quality change or compression for a PDF file. Does anyone have this problem?

Could you give me some advice? Maybe some java libraries that can just compress pdf?

Thanks!

PS: I do not want to use a paid library for PDF, I know that there is an iText library, but I would like to use my own.

UPDATE

Here you will find two PDF samples for Android and one for iOS.

The code used to create the PDF file in Android is as follows:

 // Create a shiny new (but blank) PDF document in memory PdfDocument document = new PdfDocument(); // crate a page description PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(612, 792, 1).create(); // create a new page from the PageInfo PdfDocument.Page page = document.startPage(pageInfo); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.BLACK); textPaint.setTextSize(12); textPaint.setTextAlign(Paint.Align.LEFT); Typeface textTypeface = Typeface.create(Typeface.MONOSPACE, Typeface.NORMAL); textPaint.setTypeface(textTypeface); String text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum."; StaticLayout mTextLayout = new StaticLayout(text, textPaint, page.getCanvas().getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false); mTextLayout.draw(page.getCanvas()); // do final processing of the page document.finishPage(page); mPDFFilePath = new File(getFilesDir(), "demo_android.pdf"); try { FileOutputStream mFileOutStream = new FileOutputStream(mPDFFilePath); // write the document content document.writeTo(mFileOutStream); mFileOutStream.flush(); mFileOutStream.close(); } catch(Exception e) { Log.v("log_tag", e.toString()); } //close the document document.close(); 

Thanks again!

+7
java android pdf
source share
1 answer

Well, until someone finds or finds a better solution, I found this library that seems to work better than the ones that are supported by the Android API (at the moment), unfortunately.

The library may not support too complex drawings, but this is normal for what I need. It also compresses pdf, and it seems that now I can compare it with iOS and say that it is smaller than it.

0
source share

All Articles