Reputation: 31
I am printing contents on an interface inside a scrollview using PrintDocumentAdapter. The final result, however, has black space on right side of the generated document. There is no such blank space on actual interface.
public void printScrollViewContent() {
PrintManager printManager = (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
String jobName = getString(R.string.app_name) + " Document";
printManager.print(jobName, new PrintDocumentAdapter() {
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
// Get the dimensions of the scroll view
int height = scrollView.getHeight();
// Get the total size of the content in the scroll view
int totalHeight = scrollView.getChildAt(0).getHeight();
// Calculate the number of pages needed
numberOfPages = (int) Math.ceil((double) totalHeight / height);
Rect pageDimensions = new Rect();
scrollView.getDrawingRect(pageDimensions);
// Prepare the PDF document layout
PrintDocumentInfo.Builder builder = new PrintDocumentInfo.Builder("print_output.pdf");
builder.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(numberOfPages);
PrintDocumentInfo info = builder.build();
callback.onLayoutFinished(info, true);
}
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
// Render content and write to PDF document
PdfDocument pdfDocument = new PdfDocument();
for (int i = 0; i < numberOfPages; i++) {
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(scrollView.getWidth(), scrollView.getHeight(), i).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();
// Calculate the portion of the content to be drawn for this page
int start = i * canvas.getHeight();
int end = Math.min((i + 1) * canvas.getHeight(), scrollView.getChildAt(0).getHeight());
// Create bitmap of the portion of the scroll view content for this page
Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), end - start, Bitmap.Config.ARGB_8888);
Canvas contentCanvas = new Canvas(bitmap);
contentCanvas.translate(0, -start); // Translate canvas to the appropriate position
scrollView.getChildAt(0).draw(contentCanvas);
// Draw the bitmap onto the canvas
canvas.drawBitmap(bitmap, 0, 0, null);
// Finish the page
pdfDocument.finishPage(page);
}
try {
// Write the PDF document to the output stream
pdfDocument.writeTo(new FileOutputStream(destination.getFileDescriptor()));
} catch (IOException e) {
e.printStackTrace();
} finally {
pdfDocument.close();
}
callback.onWriteFinished(new PageRange[]{new PageRange(0, numberOfPages - 1)});
}
}, null);
}
I tried playing around with x index of the canvas but it did not help. The right space remains there. I need help figuring how to fix the extra blank space on right in the generated PDF. Thanks!
Upvotes: 0
Views: 42
Reputation: 10252
This seems like a lot of over complication and probably won't produce the best results and seem to ignore all the reasons to use a PrintAdaptor instead of just writing the PDF as a file.
But I don't think that is the cause of the issue.
It's most likely because you size the bitmap and pdf page to a different thing to what you actually draw.
The bitmap and pdf page are sized to scrollView.getWidth()
but then you draw scrollView.getChildAt(0)
these two items are not necessary the same width.
I would try sizing the pdf and bitmap to the thing you actually draw e.g. scrollView.getChildAt(0).getwidth()
Upvotes: 0