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.


Comments

  1. Sir it works but please tell me how to save it in downloads as I am unable to access it from file manager

    ReplyDelete
  2. How do I get sketchware to show location on a pdf (geopdf)? I'm using webview. Like the app Azenza Maps.

    ReplyDelete
  3. Hello Sanjeev,
    It'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

    ReplyDelete
  4. Not working without any error Message

    ReplyDelete
  5. IF WE ARE FETCHING LONG DATA IN LISTVIEW THEN CAN WEPRINT ALL DATA IN PDF?

    ReplyDelete
  6. hello How can I create App for pdf books???

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware