Открытие диалогового окна «Печать» при нажатии кнопки на разных языках программирования и платформах

Чтобы открыть диалоговое окно печати одним нажатием кнопки, вы можете использовать различные методы в зависимости от языка программирования и платформы, с которой вы работаете. Вот несколько часто используемых методов в разных контекстах:

  1. JavaScript (Интернет):

    function printPage() {
     window.print();
    }

    HTML:

    <button onclick="printPage()">Print</button>
  2. C# (Windows Forms):

    using System.Drawing.Printing;
    private void PrintButton_Click(object sender, EventArgs e)
    {
       PrintDocument printDocument = new PrintDocument();
       PrintDialog printDialog = new PrintDialog();
       printDialog.Document = printDocument;
       if (printDialog.ShowDialog() == DialogResult.OK)
       {
           printDocument.Print();
       }
    }
  3. Java (Swing):

    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class PrintDialogExample extends JFrame {
       private JButton printButton;
       public PrintDialogExample() {
           printButton = new JButton("Print");
           printButton.addActionListener(e -> {
               PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
               PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
               if (printService != null) {
                   printService.createPrintJob().print(pras);
               }
           });
           // Add the button to the frame and other necessary code
       }
       public static void main(String[] args) {
           PrintDialogExample example = new PrintDialogExample();
           example.setVisible(true);
       }
    }