Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create landscape PDF using android native PdfDocument API?

I am using PdfDocument API to write a PDF from View using Android

Problem

If am writing PDF of A4 size. How can i make in landscape mode?

Thanks in Advance.

like image 622
Karthiknew Avatar asked Jan 28 '16 10:01

Karthiknew


People also ask

How to generate PDF in Android application?

The Android print feature allows you to save any printable content as a PDF. To create a PDF from the print preview, select Save as PDF as the target printer.

How do I create a kotlin PDF?

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. Navigate to the app > res > layout > activity_main. xml and add the below code to that file.

How can I open PDF in android programmatically?

The very first and the easiest way of displaying the PDF file is to display it in the WebView. All you need to do is just put WebView in your layout and load the desired URL by using the webView. loadUrl() function. Now, run the application on your mobile phone and the PDF will be displayed on the screen.


1 Answers

A typical use of the Android PDF APIs looks like this:

// create a new document
PdfDocument document = new PdfDocument();

// crate a page description
PageInfo pageInfo = new PageInfo.Builder(300, 300, 1).create();

// start a page
Page page = document.startPage(pageInfo);

// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());

// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());

// close the document
document.close();

According to the developer.android.com reference:

public PdfDocument.PageInfo.Builder (int pageWidth, int pageHeight, int pageNumber)

Added in API level 19

Creates a new builder with the mandatory page info attributes.

Parameters

pageWidth The page width in PostScript (1/72th of an inch).

pageHeight The page height in PostScript (1/72th of an inch).

pageNumber The page number.

To create a PDF with portrait A4 pages, therefore, you can define the page descriptions like this:

PageInfo pageInfo = new PageInfo.Builder(595, 842, 1).create();

and for a PDF with landscape A4 pages, you can define them like this:

PageInfo pageInfo = new PageInfo.Builder(842, 595, 1).create();
like image 107
mkl Avatar answered Nov 15 '22 10:11

mkl