• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Print text file using java

Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello every one..
Am strucking with printing textfile using java..
What the problem i faced is the printer neglected all the empty spaces .
i have to print that text as per in the document..
Can any one help me...

Thanks in advance....

My code is
package src;

import com.dao.dto.LrMasterDTO;
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.*;

* Using JAVA to print simple text file to a printer

public class LRPrint implements Printable {

static AttributedString myStyledText = null;

public static void main(String args[]) {
/**Location of a file to print**/
WriteText wt=new WriteText();
InvoiceText it=new InvoiceText();
// it.main();
String fileName = "D:/dddd.txt";

/**Read the text content from this location **/
String mText = readContentFromFileToPrint(fileName);

/**Create an AttributedString object from the text read*/
myStyledText = new AttributedString(mText);



* This method reads the content of a text file.
* The location of the file is provided in the parameter
private static String readContentFromFileToPrint(String fileName) {
String dataToPrint = "";

try {
BufferedReader input = new BufferedReader(new FileReader(fileName));
String line = "";
/**Read the file and populate the data**/
while ((line = input.readLine()) != null) {
dataToPrint += line + "\n";
} catch (Exception e) {
return dataToPrint;
return dataToPrint;

* Printing the data to a printer.
* Initialization done in this method.
public static void printToPrinter() {
* Get a Printer Job
PrinterJob printerJob = PrinterJob.getPrinterJob();

* Create a book. A book contains a pair of page painters
* called printables. Also you have different pageformats.
Book book = new Book();
* Append the Printable Object (this one itself, as it
* implements a printable interface) and the page format.
book.append(new LRPrint(), new PageFormat());

* Calling the printDialog will pop up the Printing Dialog.
* If you want to print without user confirmation, you can directly call printerJob.print()
* doPrint will be false, if the user cancels the print operation.
boolean doPrint = printerJob.printDialog();

if (doPrint) {
try {
} catch (PrinterException ex) {
System.err.println("Error occurred while trying to Print: "
+ ex);

* This method comes from the Printable interface.
* The method implementation in this class
* prints a page of text.
public int print(Graphics g, PageFormat format, int pageIndex) {

Graphics2D graphics2d = (Graphics2D) g;
* Move the origin from the corner of the Paper to the corner of the imageable
* area.
graphics2d.translate(format.getImageableX(), format.getImageableY());

/** Setting the text color**/
* Use a LineBreakMeasurer instance to break our text into lines that fit the
* imageable area of the page.
Point2D.Float pen = new Point2D.Float();
AttributedCharacterIterator charIterator = myStyledText.getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator,
float wrappingWidth = (float) format.getImageableWidth();
while (measurer.getPosition() < charIterator.getEndIndex()) {
TextLayout layout = measurer.nextLayout(wrappingWidth);
pen.y += layout.getAscent();
float dx = layout.isLeftToRight() ? 0 : (wrappingWidth - layout
layout.draw(graphics2d, pen.x + dx, pen.y);
pen.y += layout.getDescent() + layout.getLeading();
return Printable.PAGE_EXISTS;


please help me to find the problem
Posts: 3742
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've already been told once to EaseUp. Folk on here tend to ignore people who think all their problems are urgent.
I suggest you edit the title of your post if you want to get help.

You were also given a link to HowToAskQuestionsOnJavaRanch, which included a link to UseCodeTags
    Bookmark Topic Watch Topic
  • New Topic