Issue
I have a very long string which may be continuous without space or not continuous, and it should automatically generate pdf with with multiple pages if necessary and with proper multiline with PdfDocument.
I have tried firstly using paint but only one line with text overflowing was being printed. This problem was solved using StaticLayout and proper multiline is got but now the text gets overflown from below.
I have searched a lot but didn't exactly find one and I don't want to use iText as its only for opensource projects.
Solution
This solution by which I solved the problem by using simple PdfDocument and StaticLayout without using any external paid libraries. The problem as I have shared in my question is that maintaining proper structure of the texts in the generated pdf file.
I used the length and width of a A4 size page and have fixed the number of characters in each page to a limit, after that when it encounters the end of that line it automatically creates next page, also auto next line is handled by StaticLayout no overflowing of text is seen.
If you feel you can try with different character limit for each page and try different size as your need. Also you can try the StaticLayout.Builder which is supported in Android version Q/29 and above not below it.
The code sample I will share with you, which if you use, no matter how long the text you have, it will automatically handle multi page if text is long and generate the pdf, also it maintains paragraphs etc, you can also customise page shape, number of characters etc according to your need. Other details in the code are self-explanatory.
String text = "Lorem ipsum...very long text";
ArrayList<String> texts = new ArrayList<>();
int tot_char_count = 0;
//Counts total characters in the long text
for (int i = 0; i < text.length(); i++) {
tot_char_count++;
}
int per_page_words = 4900;
int pages = tot_char_count / per_page_words;
int remainder_pages_extra = tot_char_count % per_page_words;
if (remainder_pages_extra > 0) {
pages++;
}
int k = pages, count = 0;
while (k != 0) {
StringBuilder each_page_text = new StringBuilder();
for (int y = 0; y < per_page_words; y++) {
if (count < tot_char_count) {
each_page_text.append(text.charAt(count));
if (y == (per_page_words - 1) && text.charAt(count) != ' ') {
while (text.charAt(count) != '\n') {
count++;
each_page_text.append(text.charAt(count));
}
} else {
count++;
}
}
}
texts.add(each_page_text.toString());
k--;
}
PdfDocument pdfDocument = new PdfDocument();
int pageNumber = 0;
try {
pageNumber++;
for (String each_page_text : texts) {
PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(595, 842, pageNumber).create();
PdfDocument.Page myPage = pdfDocument.startPage(mypageInfo);
Canvas canvas = myPage.getCanvas();
TextPaint mTextPaint = new TextPaint();
mTextPaint.setTextSize(11);
mTextPaint.setTypeface(ResourcesCompat.getFont(context, R.font.roboto));
StaticLayout mTextLayout = new StaticLayout(each_page_text, mTextPaint, canvas.getWidth() - 60, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.save();
int textX = 30;
int textY = 30;
canvas.translate(textX, textY);
mTextLayout.draw(canvas);
canvas.restore();
pdfDocument.finishPage(myPage);
}
File file = new File(context.getFilesDir(), "GeneratedFile.pdf");
FileOutputStream fOut = new FileOutputStream(file);
pdfDocument.writeTo(fOut);
// Toast.makeText(context, "PDF file generated successfully.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
Answered By - abby
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.