Fisher
Fisher

Reputation: 509

Android: How to print EditText with font type and size by WebView

I use WebView to print the content in an EditText as the sample code below. The print function works well but the different fonts and sizes , that were set in EditText, do not display in the print output. I tried to change "text/plain" to "text/html" but it is still the same. Please help if any suggestion. Thanks!

void printFile(){
    WebView myWebView = new WebView(getApplicationContext());
    myWebView.loadData(editText2.getText().toString(), "text/plain", "UTF-8");
    myWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
        @Override
        public void onPageFinished(WebView view, String url) {

        }
    });

    PrintManager printManager = (PrintManager) primaryBaseActivity.getSystemService(Context.PRINT_SERVICE);
    //create object of print adapter
    PrintDocumentAdapter printAdapter = myWebView.createPrintDocumentAdapter();
    //provide name to your newly generated pdf file
    String jobName = getString(R.string.app_name) + " Print";
    //open print dialog
    printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
}

Upvotes: 0

Views: 32

Answers (1)

Fisher
Fisher

Reputation: 509

finally I find this modification can print multiple font size

...
WebSettings wSettings = myWebView.getSettings();
String st = editText2.getText().toString();
st = st.replaceAll("\n", "<br>");
myWebView.loadDataWithBaseURL("file:///android_asset/", st, "text/html", "UTF-8", null);
wSettings.setTextZoom((int)(editText2.getTextSize()*2));//You can change 2 to the multiple you want
...

Upvotes: 0

Related Questions