Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8201177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:43:08+00:00 2026-06-07T06:43:08+00:00

I have a form where the user can fill in the fields with data.

  • 0

I have a form where the user can fill in the fields with data.
Thereafter, he/she shall be able to export the data as a pdf which I have written already as it can be seen below:

public void onSubmit() {
try {
    ManagerModel manager = managerDao.getManager(person);
    PictureModel picture = new PictureModel();
    if (person.getPhotoId() != null) {
        picture = new PictureModel(pictureDao.findPictureById(person.getPhotoId()));
    }
    getRequestCycle().setRequestTarget( new FileRequestTarget(Exporter.exportFile(person, manager, picture), person.getPdfName()));
} catch (Exception e) {
    Log.warn(e);
}

now this provides me with a pdf export along with all data. i like to also create a button which allows the user to print the data which has been entered in those fields. now, that should be a print button on the form rather than requiring the user to export then print.

can someone advise how i can create this print button? should i just use the output from the pdf export then send that to the printer? if so, how do i write that in java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:43:10+00:00Added an answer on June 7, 2026 at 6:43 am

    Well you can create a button by simple:

     import javax.swing.*;
     ....
     JButton button = new JButton("Print");
    

    then add an ActionListener to the button:

    import java.awt.*;
    ....
    button.addActionListener(new ActionListener() {
                @override
                public void actionPerformed(ActionEvent e)
                {
                    //Execute when button is pressed
                    printPDF("path/to/file/.pdf");
                }
            });   
    

    then to print the PDF you could use this method:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import javax.print.*;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    ....
    public static void printPDF(String file) {
    
        FileInputStream psStream = null;
        try {
            psStream = new FileInputStream(file);
        } catch (FileNotFoundException ffne) {
            ffne.printStackTrace();
        }
        if (psStream == null) {
            return;
        }
        DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);
    
        // this step is necessary because I have several printers configured  
        PrintService myPrinter = null;
        for (int i = 0; i < services.length; i++) {
            String svcName = services[i].toString();
            System.out.println("service found: " + svcName);
            if (svcName.contains("printer closest to me")) {
                myPrinter = services[i];
                System.out.println("my printer found: " + svcName);
                break;
            }
        }
        if (myPrinter != null) {
            DocPrintJob job = myPrinter.createPrintJob();
            try {
                job.print(myDoc, aset);
    
            } catch (Exception pe) {
                pe.printStackTrace();
            }
        } else {
            System.out.println("no printer services found");
        }
    }
    

    Addendum:

    • To make this work on a specific printer that might not have “printer closest to me”:
      change this code to include your printers name, or the exact printer name using contains() or equals() respectively:

      String printerName="";
      ....
      if (svcName.contains(printerName)||svcName.equals(printerName)) {
                          myPrinter = services[i];
                          System.out.println("my printer found: " + svcName);
                          break;
      }
      

    References:

    • Printing PDF Java
    • How do you add an ActionListener onto a JButton in Java
    • How to Create JButton
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form into which the user can add more fields to fill
I have a form which a user can fill in x times with the
I have a form made up of pairs of text fields. The user can
If I have form where user can upload files via Ajax (and upload is
I have a search form where user can search for products within their specified
I have a form where the user can upload a zip file, it works
I have a form where a user can enter the number of students that
I have a form on a page that a user can use to create
I have an HTML form where the user can type in any number:5, 8,
I have form where user submits field. Field can have letters, numbers, and punctuation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.