-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrintEG.java
132 lines (111 loc) · 4.33 KB
/
PrintEG.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.xyz.bill_master;
import android.content.Intent;
import android.graphics.pdf.PdfDocument;
import android.os.Build;
import android.os.Environment;
import android.print.PrintAttributes;
import android.print.pdf.PrintedPdfDocument;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class PrintEG extends AppCompatActivity {
private static final String LOG_TAG = "Error";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print_eg);
}
/**
* Prints the contents on the screen to a PDF file,
* which i then saved in Documents/PDF
* @param view The clicked view
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void printPDF(View view){
if(isExternalStorageWritable()) {
String filename = getFileName();
File file = new File(getAlbumStorageDir("PDF"), filename);
try {
FileOutputStream outputStream = new FileOutputStream(file);
createPDF(outputStream);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
/**
* Checks if external storage is available for read and write
* @return boolean
*/
private boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* Returns a name for the file that will be created
* @return String
*/
private String getFileName() {
//TODO: 06/10/2015
return "file2" + ".pdf";
}
/**
* Creates a PDF document and writes it to external storage using the
* received FileOutputStream object
* @param outputStream a FileOutputStream object
* @throws IOException
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPDF(FileOutputStream outputStream) throws IOException {
PrintedPdfDocument document = new PrintedPdfDocument(this,
getPrintAttributes());
// start a page
PdfDocument.Page page = document.startPage(1);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
//. . .
// add more pages
//. . .
// write the document content
document.writeTo(outputStream);
//close the document
document.close();
}
private View getContentView() {
return findViewById(R.id.relativeLayout);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private PrintAttributes getPrintAttributes() {
PrintAttributes.Builder builder = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("res1","Resolution",50,50)).setMinMargins(new PrintAttributes.Margins(5, 5, 5, 5));
PrintAttributes printAttributes = builder.build();
return printAttributes;
}
private File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
public void load_other_activity(View view) {
Intent i=new Intent(getApplicationContext(),Billing.class);
startActivity(i);
}
}