Test.java worked on Dymo user/blake/lp1741299-hatch-dymo
authorbmagic007 <blake@mobiusconsortium.org>
Wed, 9 May 2018 13:11:32 +0000 (08:11 -0500)
committerbmagic007 <blake@mobiusconsortium.org>
Wed, 9 May 2018 13:11:32 +0000 (08:11 -0500)
PrintDemo.java [new file with mode: 0644]
Test.java [new file with mode: 0644]

diff --git a/PrintDemo.java b/PrintDemo.java
new file mode 100644 (file)
index 0000000..558aec7
--- /dev/null
@@ -0,0 +1,113 @@
+package org.carlfx;
+import javafx.application.Application;
+import javafx.beans.property.BooleanProperty;
+import javafx.beans.property.SimpleBooleanProperty;
+import javafx.beans.value.ChangeListener;
+import javafx.concurrent.Worker.State;
+import javafx.print.*;
+import javafx.scene.Node;
+import javafx.scene.Scene;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextField;
+import javafx.scene.layout.BorderPane;
+import javafx.scene.layout.HBox;
+import javafx.scene.transform.Scale;
+import javafx.scene.web.WebEngine;
+import javafx.scene.web.WebView;
+import javafx.stage.Stage;
+/**
+ * Demo to use JavaFX 8 Printer API.
+ *
+ * @author cdea
+ */
+public class PrintDemo extends Application {
+    @Override
+    public void start(Stage primaryStage) {
+        final TextField urlTextField = new TextField();
+        final Button printButton = new Button("Print");
+        final WebView webPage = new WebView();
+        final WebEngine webEngine = webPage.getEngine();
+        HBox hbox = new HBox();
+        hbox.getChildren().addAll(urlTextField, printButton);
+        BorderPane borderPane = new BorderPane();
+        borderPane.setTop(hbox);
+        borderPane.setCenter(webPage);
+        Scene scene = new Scene(borderPane, 300, 250);
+        primaryStage.setTitle("Print Demo");
+        primaryStage.setScene(scene);
+        // print button pressed, page loaded
+        final BooleanProperty printButtonClickedProperty = new SimpleBooleanProperty(false);
+        final BooleanProperty pageLoadedProperty = new SimpleBooleanProperty(false);
+        // when the a page is loaded and the button was pressed call the print() method.
+        final BooleanProperty printActionProperty = new SimpleBooleanProperty(false);
+        printActionProperty.bind(pageLoadedProperty.and(printButtonClickedProperty));
+        // WebEngine updates flag when finished loading web page.
+        webEngine.getLoadWorker()
+                 .stateProperty()
+                 .addListener( (ChangeListener) (obsValue, oldState, newState) -> {
+                    if (newState == State.SUCCEEDED) {
+                        pageLoadedProperty.set(true);
+                    }
+                 });
+        // When user enters a url and hits the enter key.
+        urlTextField.setOnAction( aEvent ->  {
+            pageLoadedProperty.set(false);
+            printButtonClickedProperty.set(false);
+            webEngine.load(urlTextField.getText());
+        });
+        // When the user clicks the print button the webview node is printed
+        printButton.setOnAction( aEvent -> {
+            printButtonClickedProperty.set(true);
+        });
+        // Once the print action hears a true go print the WebView node.
+        printActionProperty.addListener( (ChangeListener) (obsValue, oldState, newState) -> {
+            
+                print(webPage);
+            
+        });
+        primaryStage.show();
+    }
+    /** Scales the node based on the standard letter, portrait paper to be printed.
+     * @param node The scene node to be printed.
+     */
+    public void print(final Node node) {
+        Printer printer = Printer.getDefaultPrinter();
+        PageLayout pageLayout = printer.createPageLayout(Paper.NA_LETTER, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
+        double scaleX = pageLayout.getPrintableWidth() / node.getBoundsInParent().getWidth();
+        double scaleY = pageLayout.getPrintableHeight() / node.getBoundsInParent().getHeight();
+        node.getTransforms().add(new Scale(scaleX, scaleY));
+        PrinterJob job = PrinterJob.createPrinterJob();
+        if (job != null) {
+            boolean success = job.printPage(node);
+            if (success) {
+                job.endJob();
+            }
+        }
+    }
+    /**
+     * The main() method is ignored in correctly deployed JavaFX application.
+     * main() serves only as fallback in case the application can not be
+     * launched through deployment artifacts, e.g., in IDEs with limited FX
+     * support. NetBeans ignores main().
+     *
+     * @param args the command line arguments
+     */
+    public static void main(String[] args) {
+        launch(args);
+    }
+}
\ No newline at end of file
diff --git a/Test.java b/Test.java
new file mode 100644 (file)
index 0000000..4b26fa9
--- /dev/null
+++ b/Test.java
@@ -0,0 +1,77 @@
+
+// https://www.java-forums.org/javafx/95904-illegalargumentexception-bad-margins-when-printing-dymo-labelwriter-450-a.html
+
+import javafx.print.*;
+import javafx.scene.web.WebEngine;
+import javafx.collections.ObservableSet;
+import javafx.collections.SetChangeListener;
+import javafx.application.Application;
+import javafx.stage.Stage;
+
+
+import javafx.scene.Node;
+import javafx.scene.shape.Shape;
+import javafx.scene.shape.Circle;
+import javafx.scene.control.TextArea;
+
+import javax.print.PrintService;
+import javax.print.*;
+import javax.print.PrintServiceLookup;
+import javax.print.attribute.Attribute;
+import javax.print.attribute.AttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
+import javax.print.attribute.standard.Media;
+import javax.print.attribute.standard.OrientationRequested;
+
+import java.lang.IllegalArgumentException;
+import java.io.StringWriter;
+import java.io.PrintWriter;
+
+public class Test extends Application {
+       
+       public static void main(String[] args) {
+               Application.launch(args);
+       }
+
+    public void start(Stage stage) {
+        Printer printer = Printer.getDefaultPrinter();
+        System.out.printf("Selected: %s\n", printer.getName());
+
+               PrinterAttributes attributes = printer.getPrinterAttributes();
+               System.out.printf("\nLabel Size: %g, %g(%s)\n", 
+               attributes.getDefaultPaper().getWidth(), attributes.getDefaultPaper().getHeight(), 
+               attributes.getDefaultPaper().toString());
+               
+               Paper label = attributes.getDefaultPaper();
+               System.out.println("PAPER TYPE = "+label.getName());
+               
+               // PageOrientation orientation = printer.getPrinterAttributes().getDefaultPageOrientation();
+               PageOrientation orientation = PageOrientation.valueOf("PORTRAIT");
+               PageLayout layout = printer.createPageLayout(label, orientation, Printer.MarginType.HARDWARE_MINIMUM);
+               // PageLayout layout = printer.createPageLayout(label, orientation, 0,0,0,0);
+
+               System.out.printf("\nPageLayout: %s\n", layout.toString());
+               PrinterJob printerJob = PrinterJob.createPrinterJob();
+               try {
+
+
+                       TextArea textarea = new TextArea();
+                       textarea.setText("hello");
+                       boolean success = printerJob.printPage(layout, textarea);
+                       System.out.println("printerJob.printPage : " + success);
+
+                       if(success) {                           
+                               System.out.println("This WORKED");
+                       }
+                       System.out.println("LAST LINE IN TRY");
+
+               } catch(IllegalArgumentException e) {
+                       // e.printStackTrace();
+                       System.out.println("This failed - CATCH BLOCK");
+               }
+               finally {
+                       // printerJob.endJob();
+               }
+                               
+    }
+}