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

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer vector::operator[] retrieves the Nth element of the vector. Such an… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer I think the best option would be Microsoft ASP.NET MVC… May 12, 2026 at 1:03 am
  • Editorial Team
    Editorial Team added an answer FWIW this has changed in SL3 RTM: Silverlight 3 Beta… May 12, 2026 at 1:03 am

Related Questions

I am currently trying to implement a Swing component, inheriting from JLabel which should
I am working on a GUI where the JComponents are stamped on the screen.
I am trying to create colored headers and footers when printing a JTable. Specifically,
I am printing a Jpanel and its working fine, but now I need the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.