2017-11-06 11 views
0

私はJavaで印刷するときに余白を変更するためのさまざまな解決策に出くわしましたが、うまくいきません。 HereおよびHereJTextPaneの印刷マージンを変更する

TextPane textPane = new JTextPane(); 
StyledDocument doc = textPane.getStyledDocument(); 

// Define a keyword attribute 
SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
StyleConstants.setBold(keyWord, true); 

Style style = doc.addStyle("StyleName", null); 
StyleConstants.setIcon(style, new ImageIcon(qrcode)); 

doc.insertString(0, "Title Here\n", null); 
doc.insertString(doc.getLength(), "Ignored", style); 

textPane.print(); 

作り付けの印刷方法を使用する場合は、私がこれまでされており、マージンは25.4ミリメートルとしてデフォルトに設定されています。これらの余白を編集するには、を印刷しながら、印刷ダイアログを開くことができます。私が検証「できること」である

+0

印刷ダイアログには「ページ設定」タブはありませんか? – MadProgrammer

+0

@MadProgrammerはい、この値をプログラムで設定したいと思います。 – hahahakebab

答えて

1

、このようなものが

JTextPane textPane = new JTextPane(); 
StyledDocument doc = textPane.getStyledDocument(); 

// Define a keyword attribute 
SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
StyleConstants.setBold(keyWord, true); 

Style style = doc.addStyle("StyleName", null); 
//StyleConstants.setIcon(style, new ImageIcon(qrcode)); 

doc.insertString(0, "Title Here\n", null); 
doc.insertString(doc.getLength(), "Ignored", style); 

Paper paper = new Paper(); 
paper.setSize(fromCMToPPI(21.0), fromCMToPPI(29.7)); // A4 
paper.setImageableArea(fromCMToPPI(5.0), fromCMToPPI(5.0), 
       fromCMToPPI(21.0) - fromCMToPPI(10.0), fromCMToPPI(29.7) - fromCMToPPI(10.0)); 

PageFormat pageFormat = new PageFormat(); 
pageFormat.setPaper(paper); 

PrinterJob pj = PrinterJob.getPrinterJob(); 
pj.setPrintable(textPane.getPrintable(null, null), pageFormat); 
PageFormat pf = pj.pageDialog(pageFormat); 
if (pj.printDialog()) { 
    pj.print(); 
} 

通常出力変更された出力対出力のページサイズとマージンに影響を与えます

Normal(境界線が変更を強調するために後に追加しました)

、会話の方法Modified ...

protected static double fromCMToPPI(double cm) { 
    return toPPI(cm * 0.393700787); 
} 

protected static double toPPI(double inch) { 
    return inch * 72d; 
} 

MacOSが私にそれらを見る必要がないと決めたので、それらの値がページ設定またはプリンタダイアログのいずれかに現れるかどうかを確認できません:/

Windowsでの以前の経験から、私はそれを覚えているようです作業

+0

返信いただきありがとうございますが、変数 "pj"は何を指していますか? – hahahakebab

+1

Opps申し訳ありませんが、私は更新します:P – MadProgrammer

+1

@hahahakebabそれは 'PrinterJob pj = PrinterJob.getPrinterJob();'であり、例を更新しました – MadProgrammer

関連する問題