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

  • SEARCH
  • Home
  • 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 236975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:23:18+00:00 2026-05-11T20:23:18+00:00

I am printing a Swing component that contains text. The Swing component renders the

  • 0

I am printing a Swing component that contains text. The Swing component renders the text just fine on the screen, but, when I print it (to a .tif file), the characters are all smashed together. Why is this?

Run this code to see what I mean:

import javax.swing.*;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

public final class PrintingDemo2 implements Printable {

    private final JTextPane textPane;
    private static final String WORDS = "GOOD MORNING\u00AE AMERICA";
    private static final String TEXT = WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS + '\n' + WORDS;

    public static void main(String[] args) {
        new PrintingDemo2();
    }

    public PrintingDemo2() {
        textPane = new JTextPane();
        textPane.setText(TEXT);
        final StyledDocument document = textPane.getStyledDocument();

        String[] fontFamilies = new String[]{"Tahoma", "SimSum", "MS Mincho", "Batang", "Arial", "Times New Roman"};
        for (int i = 0; i < fontFamilies.length; i++) {
            final MutableAttributeSet attributeSet = new SimpleAttributeSet();
            StyleConstants.setFontFamily(attributeSet, fontFamilies[i]);
            StyleConstants.setFontSize(attributeSet, 14);
            document.setParagraphAttributes(i * 22, 21, attributeSet, true);
        }

        final AbstractButton printContextButton = new JButton("Print Context");
        printContextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                final PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(PrintingDemo2.this);
                try {
                    job.print();
                } catch (PrinterException ex) {
                    throw new RuntimeException("Printing Failed.", ex);
                }
            }
        });

        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final Container contentPane = frame.getContentPane();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        contentPane.add(printContextButton);
        contentPane.add(new JScrollPane(textPane));
        frame.setSize(400, 200);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setVisible(true);
            }
        });
    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        if (pageIndex >= 1) return Printable.NO_SUCH_PAGE;

        RepaintManager mgr = RepaintManager.currentManager(textPane);
        mgr.setDoubleBufferingEnabled(false);
        final Graphics2D graphics2D = (Graphics2D) graphics;
        graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        textPane.paint(graphics);
        mgr.setDoubleBufferingEnabled(true);

        return Printable.PAGE_EXISTS;
    }
}
  • 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-05-11T20:23:18+00:00Added an answer on May 11, 2026 at 8:23 pm

    You could try rasterizing it (painting it to a BufferedImage at 300dpi) and then printing that image. Hacky, and bad for performance (huge rasterized file sent to printer instead of vector data), but at least you won’t have font problems.

    To rasterize it, create a BufferedImage that is 4.17x (300/72) the size of your on-screen panel and scale its graphics object be the same abount, and then paint the panel onto the buffered image’s Graphics2D object.

    Disclaimer: this isn’t elegant and I know it. If someone knows how to convince every make and model of printer to receive fonts from a Java printing process, please chime in!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just starting to learn about printing with Java Swing, so please bear with
I need to create a Linked List from a text file that looks like
A java program that does frequent swing/awt painting animation (but nothing more advanced than
I have some code that uses the Swing/AWT printing functionality that I want to
I'm printing a complicated swing application UI to a physical printer via an Airport.
I need to do some printing from my Java Swing application. I have tried
When printing a page from a browser, it refers to a print.css stylesheet declared
I'm printing the first line from a file: with open(path,r,encoding='utf8') as f: for i,
Why is this printing out 0 back in main but 6 when it is
I am writing a custom Swing component with my own painting. I'd like to

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.