A
appleseeker
Unregistered / Unconfirmed
GUEST, unregistred user!
我自己编写了段打印程序。读文件内容,打印。但打印出来的,中文无法解决。请高手帮助帮助。我在BUTTON里调用下面这个printfile()函数。
public void printfile()
{
do
cFlavor flavor =do
cFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 400, 400, printService
, defaultService, flavor, pras);
if (service != null)
{
try
{
do
cPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream("c://c.txt");
do
cAttributeSet das = new HashDocAttributeSet();
do
cdo
c = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
if (page >= PAGES)
return Printable.NO_SUCH_PAGE;
g2.translate(pf.getImageableX(), pf.getImageableY());
return Printable.PAGE_EXISTS;
}
public int getPagesCount(String curStr)
{
int page = 0;
int position, count = 0;
String str = curStr;
while(str.length() > 0)
{
position = str.indexOf('/n');
count += 1;
if (position != -1)
str = str.substring(position + 1);
else
str = "";
}
if (count > 0)
page = count / 54 + 1;
return page;
}
}
public void printfile()
{
do
cFlavor flavor =do
cFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 400, 400, printService
, defaultService, flavor, pras);
if (service != null)
{
try
{
do
cPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream("c://c.txt");
do
cAttributeSet das = new HashDocAttributeSet();
do
cdo
c = new SimpleDoc(fis, flavor, das);
job.print(doc, pras);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public int print(Graphics g, PageFormat pf, int page) throws PrinterException
{
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.black);
if (page >= PAGES)
return Printable.NO_SUCH_PAGE;
g2.translate(pf.getImageableX(), pf.getImageableY());
return Printable.PAGE_EXISTS;
}
public int getPagesCount(String curStr)
{
int page = 0;
int position, count = 0;
String str = curStr;
while(str.length() > 0)
{
position = str.indexOf('/n');
count += 1;
if (position != -1)
str = str.substring(position + 1);
else
str = "";
}
if (count > 0)
page = count / 54 + 1;
return page;
}
}