I tried desesperately to write a PrintService and here is a problem 
regarding margins.

This is my DiscoverySession code where I define my printer capabilities:

    int LEFT_MARGIN = 0; // Or 500
    int RIGHT_MARGIN = 0;

    public void finishEvent(PrinterEvent event) {
        Log.d(TAG, "finishEvent");

        int eventType = event.getEventType();
        switch(eventType) {
            case PrinterEvent.EVENT_FINISHED_DISCOVERY:
            case PrinterEvent.EVENT_CANCELED_DISCOVERY:
                mHandler.post(new Runnable() {
                    public void run() {
                        ArrayList<com.me.PrinterInfo> list = 
mManager.getFoundPrinter();
                        int count = list.size();
                        for(int index = 0; index < count; index++) {

                            com.me.PrinterInfo myInfo = list.get(index);

                            if 
(myInfo.getPrinterModelName().equals("MyPrinter")) {

                                addMyPrinters(myInfo);
                            }
                        }
                    }
                });
                break;
        }
    }

    private void addMyPrinters(com.me.PrinterInfo seikoInfo) {

        PrinterInfo info = addPrinterCapabilities(seikoInfo);

        mPrinters.add(info);
        Log.d(TAG, "Adding printer !");
        addPrinters(mPrinters);

        
((MyPrinterApplication)mService.getApplication()).setBluetoothAddress(seikoInfo.getBluetoothAddress());

        if (!mManager.isConnect()) {
            try {
                mManager.connect(298, myInfo.getBluetoothAddress(), true);
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }

    private PrinterInfo addPrinterCapabilities(com.me.PrinterInfo myInfo) {
        Log.d(TAG, "addPrinterCapabilities !");

        PrinterId printerId = 
mService.generatePrinterId(myInfo.getPrinterModelName());

        PrintAttributes.MediaSize rollSize = new 
PrintAttributes.MediaSize("Roll58mm", "Roll58mm", 2283, 4566);
        PrintAttributes.Margins rollMargins = new 
PrintAttributes.Margins(LEFT_MARGIN, 0, RIGHT_MARGIN, 0);

        PrinterCapabilitiesInfo capabilities = new 
PrinterCapabilitiesInfo.Builder(printerId)
                .addMediaSize(rollSize, true)
                .setColorModes(PrintAttributes.COLOR_MODE_MONOCHROME, 
PrintAttributes.COLOR_MODE_MONOCHROME)
                .addResolution(new PrintAttributes.Resolution("200dpi", 
"paperRoll", 203,203), true)
                .setMinMargins(rollMargins)
                .build();

        PrinterInfo printerInfo = new PrinterInfo.Builder(printerId, 
myInfo.getPrinterModelName(), PrinterInfo.STATUS_IDLE)
                .setCapabilities(capabilities)
                .build();

        return printerInfo;
     }


And here is a simple application which uses a Webview to try to print 
something:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = new WebView(this);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.setScrollContainer(false);

        webView.setPadding(0, 0, 0,0);

        String content = "<html><body>" +
                "CARTE BANCAIRE LMV<br/>" +
                "A0000000421010<br/>" +
                "CB<br/>" +
                "LE 05/04/14 A 10:27:30<br/>" +
                "DEGARDIN SEBASTI<br/>" +
                "PARIS<br/>" +
                "6271191 49234274600038<br/>" +
                "30066<br/>" +
                "XXXXXXXXXXXX<br/>" +
                "7A1A6FCF0CC7ED04<br/>" +
                "001 000001 103 C<br/>" +
                "MONTANT :<br/>" +
                "<span class=\"amount\">57,30 EUR</span><br/>" +
                "DEBIT<br/>" +
                "TICKET CLIENT<br/>" +
                "A CONSERVER<br/>" +
                "</body></html>";

        content = "<link rel=\"stylesheet\" type=\"text/css\" 
href=\"style.css\" />" + content;
        webView.loadDataWithBaseURL("file:///android_asset/", content, 
"text/html", "UTF-8", null);

    }

    public void createWebPrintJob(View view) {

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

        if (printManager != null) {
            String jobName = getString(R.string.app_name);
            PrintJob job = null;

            PrintDocumentAdapter printAdapter = 
webView.createPrintDocumentAdapter(jobName);

            if ((job = (printManager.print(jobName, printAdapter,
                    new PrintAttributes.Builder().build()))) != null) {
                Toast.makeText(this, "Job succesfully created", 
Toast.LENGTH_SHORT).show();
            }
        }
    }

I tried to change LEFT_MARGIN to 0 or to 500 and here are the resulting 
layout.
As you can see below, in this first try I've set the left margin to 0 and 
the layout is realized with a margin superior to 0:

[![enter image description here][1]][1]


In this example, I've set the LEFT_MARGIN to 500. As you can see the layout 
seems have been prepared with the same margin as for the test above and the 
500 pixels margin is drawn on top of the layout !?!
[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/wqmIQ.png
  [2]: https://i.stack.imgur.com/cSh7V.png

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/0236fe16-e4c6-424c-bf75-164efc0535ed%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to