Convert a View to pdf in Sketchware

To convert a LinearLayout along with its contents, into a pdf document, follow the steps given below.
1. Create a new project in Sketchware.

2. In View area add a LinearV linear1. Inside linear1, add contents for the pdf document page. For linear1, set background colour to WHITE or something else.


3. Create a String variable path.

4. Create a more block extra. In more block extra use following codes and blocks:

// Create OptionsMenu.
}

private static final int PDF = 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, PDF, 0, "toPdf").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}

// Add code for menu item selection.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case PDF:
// Create pdf document and add linear1 as a page to it.
try {
android.graphics.pdf.PdfDocument document = new android.graphics.pdf.PdfDocument();
android.graphics.pdf.PdfDocument.PageInfo pageInfo = new android.graphics.pdf.PdfDocument.PageInfo.Builder(linear1.getWidth(), linear1.getHeight(), 1).create();

android.graphics.pdf.PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
linear1.draw(canvas);
document.finishPage(page);

// Set String path to the path for saving pdf document. And write empty string to the path.
// Save pdf document to the path.
java.io.File myfile = new java.io.File(path);
java.io.FileOutputStream fout = new java.io.FileOutputStream(myfile);
java.io.OutputStreamWriter myOutWriter = new java.io.OutputStreamWriter(fout);

document.writeTo(fout);
document.close();
myOutWriter.close();
fout.close();
showMessage("File Saved");
} catch ( Exception e){
showMessage(e.toString());
}
return true;
}
return true; }
{

5. Run the project.