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.
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.
Excellent Work Thanks
ReplyDeleteNice work
ReplyDeleteSir it works but please tell me how to save it in downloads as I am unable to access it from file manager
ReplyDeleteHow do I get sketchware to show location on a pdf (geopdf)? I'm using webview. Like the app Azenza Maps.
ReplyDeleteHello Sanjeev,
ReplyDeleteIt's not working. Still, i am gonna try. Can i insert, in the linearview, as mutch as i want or how give it a A4page format? And what about, if i have several pages? Please, can you help me?
Belgium greetings, Tony
Not working without any error Message
ReplyDeleteIF WE ARE FETCHING LONG DATA IN LISTVIEW THEN CAN WEPRINT ALL DATA IN PDF?
ReplyDeletehello How can I create App for pdf books???
ReplyDeleteGood written and informative stuff for all of us.
ReplyDeleteVisit here https://www.creativejobscentral.com/closelooptechnologies