Jump to content

Recommended Posts

Posted

Olá a todos,

Estou a ter o seguinte problema, a aplicação permite imprimir relatórios, estes relatórios são documentos pdf gerados automaticamente pela aplicação utilizando o iText. O problema é que quando mando imprimir esse pdf ele é enviado para impressora como sendo do tipo Letter e não como A4, o que em impressoras mais antigas é um problema, pois é necessário ir até à impressora e autorizar a impressão do documento do tipo Letter.

O método para imprimir é o seguinte:

public boolean printPdf(String completeFilePath, Boolean delete)
{
        File f = null;
    	try
    	{
    		PrintService selectedPrintService = null;

    		PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    		for (PrintService service : printServices)
    		{
    		    if (service.getName().compareTo(confInfo.getPrinter())==0)
    		    { 
    		    	selectedPrintService=service;
    		    	break;
    		    }
    		}
    		if (selectedPrintService!=null)
    		{
    			f = new File(completeFilePath);

    			FileInputStream fis = new FileInputStream(f);  
    			FileChannel fc = fis.getChannel(); 
    			java.nio.ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());  
    			PDFFile pdfFile = new PDFFile(bb);  
    			PDFPrintPage pages = new PDFPrintPage(pdfFile);  
    		
    			PrinterJob printJob = PrinterJob.getPrinterJob();  
    			PageFormat pf = PrinterJob.getPrinterJob().defaultPage();  
    		
    			Paper paper = new Paper();
    			paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
    			pf.setPaper(paper);
    			
    			PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    			aset.add(MediaSizeName.ISO_A4);
    			
    			printJob.setJobName(f.getName());  
    			Book book = new Book();  
    			book.append(pages, pf, pdfFile.getNumPages());  
    			printJob.setPageable(book);  

    			printJob.setPrintService(selectedPrintService);

    			printJob.print(aset);
    			fis.close();
                
    			f.delete();
    			
    			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    		return false;
    	}
     }    

Há alguma coisa de errado no código? Existe alguma outra forma para imprimir um documento pdf?

Desde já agradeço a vossa atenção.

Posted

Problema resolvido.

Defini o tamanho de uma folha A4 à mão e as impressoras mais velhas já gostaram.

    			
Paper paper = new Paper();
paper.setSize(594.936, 841.536);
paper.setImageableArea(0, 0, 594.936, 841.536);
pf.setPaper(paper);

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.