Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to write a printing code in java

 
pandianjava
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get result from resultset and i want to print in particular location in my preprinter fromat. how can i write a printing code.
 
Arockiaraj Swaminathan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get result from resultset and i want to print in particular location in my preprinter fromat. how can i write a printing code.

Hi !
This is following code which will help u to print , If U are not understanding , let me know. Java has some classes which will help the developer to do the program to print.

The Code follows

import java.awt.*;
import java.awt.print.*;
import java.io.*;
class PrintListingPainter implements Printable {
private RandomAccessFile raf;
private String fileName;
private Font fnt = new Font("Helvetica", Font.PLAIN, 10);
private int rememberedPageIndex = -1;
private long rememberedFilePointer = -1;
private boolean rememberedEOF = false;
public PrintListingPainter(String file)
{
fileName = file;
try {
// Open file
raf = new RandomAccessFile(file, "r");
//System.out.println(" File Opened ");
}
catch (Exception e)
{
rememberedEOF = true;
System.out.println(" Opening Error :" +e);

}
}

public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException
{

try {
// For catching IOException
if (pageIndex != rememberedPageIndex)
{
// First time we've visited this page
rememberedPageIndex = pageIndex;
// If encountered EOF on previous page, done
if (rememberedEOF) return Printable.NO_SUCH_PAGE;
// Save current position in input file
rememberedFilePointer = raf.getFilePointer();
}
else
raf.seek(rememberedFilePointer);

g.setColor(Color.black);
Font fnt = new Font ("helvetica", Font.PLAIN, 10);
// g2d.setFont (titleFont);
g.setFont(fnt);
int x = (int) pf.getImageableX() + 10;//10;
int y = (int) pf.getImageableY() + 12;// 12;
// Title line
g.drawString("File: " + fileName + ", page: " + (pageIndex+1), x, y);
// Generate as many lines as will fit in imageable area
y += 36;
while (y + 12 < pf.getImageableY()+pf.getImageableHeight())
{
String line = raf.readLine();
//System.out.println(" Read line : " +line);

if (line == null)
{
rememberedEOF = true;
break;
}
g.drawString(line, x, y);
y += 12;

}
return Printable.PAGE_EXISTS;
}
catch (Exception e)
{
return Printable.NO_SUCH_PAGE;
}
}
}
public class PrintListing
{
public PrintListing(String args1)
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pf = job.pageDialog(job.defaultPage());
job.setPrintable(new PrintListingPainter(args1), pf);
job.setCopies(1);
if (job.printDialog())
{

try {
job.print();
}
catch (Exception e)
{
System.out.println(" Print Exception :" +e);
}
}
//System.exit(0);
}

/*public static void main(String[] args)
{
new PrintListing(args);


}
*/

}
I hope U can understan , if not mail me , I will help U
bye!

------------------
S Arockiaraj
 
Arockiaraj Swaminathan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pandian!
See pandian this what you want to do..

Assume the following Template

Emp Code : 1001Emp Name : S Arockiaraj

Salary : 20,000Address: Chennai.

This want you want do . Am I Correct ?
This can be achieved by writing into the file only.
For that first you design the template in the way which you want like how I have mentioned above.
Position your result set values accordingly in the temlate what you have designed.
Send the file to the printer. I Think this can be achived thru this only.
Sample Code which lead you to construct the full functionality.
public void Header()// This is the method to Draw the Line.
{
try
{

pr.println(" ");
pr.println("Employe Pay Slip"); pr.println("______________");
pr.print("Emp Code :")
pr.print(" ")
pr.print("Emp Name :")
pr.print("Emp Salary :")
pr.print(" ")
pr.print("Emp Address :")
}
catch (Exception e)
{
System.out.println("Error in Writing Header Data " + e);
}
}
/**********************End Of Method Header()***********/
/**********************Begin Of Method writeReportData()*/
public void writeReportData()
{
try{
pr.print(empcode);pr.print(" ");pr.print(Emp Name : );pr.print(empname);pr.println("Emp Salary ");pr.println(" ");pr.println(empsalary);
pr.print("Address ");pr.println(" ");pr.println(address);
}
catch(Exception e)
{
System.out.println("Error in writing Report Body:" + e);
}
}
/**********************End Of Method writeReportData()******/
It s a piece of code , just u build from this one.
Pr. It represents the PrintWriter class
One another way is
Using the Drawstring function you can position your result set values in any text area (ex. JTEXT ) and then printing the Text area.
This bit difficult compared to previous one.
It�s a proof of Idea only. I have not tried so far. If U try let me know
Wish U All the Best.

------------------
S Arockiaraj
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"pandianjava",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Don,
Your post was moved to a new topic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic