• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to print "Good morning" on paper

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am learning about PrintManager to print text on paper with bluetooth printer. In Visual Basic 6.0, it is very simple and easy to print text. For example if we want to print "Good morning" the code is like this:

Printer.CurrentX = 0
Printer.CurrentY = 0
Printer.Print "Good morning"
Printer.EndDoc

But when I learn  PrintManager in Android Studio Java, I still don't understand.

This is my code in Android Studio:

PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

printManager.print("print job name", adapter, null)

The question is where to put the text "Good morning" ?

Would you please help me?
 
Ranch Hand
Posts: 1015
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know Java especially on android that well.  

I asked chatGPT this Question:

how do I use the PrintManager object in Java?  Where does the output text belong?

It returned this plus an explanation for the  code.  I'm guessing this line is where the object is told what to print.

Everyone has been kind to me in answering my starting out questions so I thought I'd give it a try.  

import android.app.Activity;
import android.content.Context;
import android.print.PrintManager;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintAttributes;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.print.PrintDocumentInfo;

public class MyActivity extends Activity {
   // ...

   private void printDocument() {
       // Get a reference to the PrintManager system service
       PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

       // Provide a name for the print job
       String jobName = "MyPrintJob";

       // Create a WebView and load the content you want to print
       WebView webView = new WebView(this);
       webView.setWebViewClient(new WebViewClient());
       webView.loadData("<p>This is the content to be printed.</p>", "text/html", "UTF-8");

       // Create a PrintDocumentAdapter to handle the printing process
       PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

       // Start the print job
       PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());

       // Check if the print job has successfully started
       if (printJob.isStarted()) {
           // Retrieve information about the printed document
           PrintDocumentInfo info = printJob.getInfo();

           // Get the output text of the printed document
           String outputText = info.getName();

           // Do something with the output text
           // ...
       }
   }

   // ...
}
 
Rancher
Posts: 5119
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please edit your post and wrap the code in code tags to give it  formatting.
 
kevin Abel
Ranch Hand
Posts: 1015
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Please edit your post and wrap the code in code tags to give it  formatting.



I forgot this time to use the code wrapper.
 
Charles Ongky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr. Abel for your answer. I will write your code and add, outputText= "Good morning" and run it after I connect my smart phone with bluetooth printer.
 
Charles Ongky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good evening Mr. Norm.

I am a new comer in ranch and don't know how to wrap my code in code tags to give it formating. How to wrap code?
 
Norm Radder
Rancher
Posts: 5119
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Ongky wrote:Good evening Mr. Norm.

I am a new comer in ranch and don't know how to wrap my code in code tags to give it formating. How to wrap code?


Edit the post, select the code, press the Code button (top row of buttons in the middle)
 
Charles Ongky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in May 26 post there is no edit button. There is only quote button.
 
Ranch Hand
Posts: 607
11
Android Python Open BSD VI Editor Slackware
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Charles Ongky wrote:But in May 26 post there is no edit button. There is only quote button.


please check this llnk from official documentation to print from Android link
 
Charles Ongky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Charles Ongky
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print the text "Good morning" on Android, you need to use PrintDocumentAdapter in PrintManager. Define a subclass of PrintDocumentAdapter and in the onWrite() method, you can draw the text on the PdfDocument.
reply
    Bookmark Topic Watch Topic
  • New Topic