저장 단추를 클릭하면 PDF로 JEditorPane에서 텍스트를 저장하려고합니다.JEditorPane을 PDF로 저장하는 방법은 무엇입니까?
saveAs.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String title = JOptionPane.showInputDialog(null, "Enter a name for file...");
try{
paintToPDF(newBlanktoEdit, title);
}catch (Exception exc){
exc.printStackTrace();
}
}
});
올바르게 작업을 수행하는 방법은 paintToPDF
그러나 Pane
는 graphics2D
요소로서 해석 등 광고를 배치하는 것은 불가능하다. 나는 외부 라이브러리를 기반으로하는 솔루션을 찾고 있어요
protected void paintToPDF(JEditorPane newPane, String title) throws Exception{
newPane.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60));
Document doc = new Document();
FileOutputStream out = new FileOutputStream(title + ".pdf");
PdfWriter writer = PdfWriter.getInstance(doc, out);
doc.setPageSize(new com.lowagie.text.Rectangle(612, 792));
doc.open();
PdfContentByte cb = writer.getDirectContent();
cb.saveState();
cb.concatCTM(1, 0, 0, 1, 0, 0);
DefaultFontMapper mapper = new DefaultFontMapper();
mapper.insertDirectory("c:/windows/fonts");
Graphics2D g = cb.createGraphics(612, 792, mapper, true, .92f);
AffineTransform at = new AffineTransform();
at.translate(convertToPixels(20), convertToPixels(20));
at.scale(pixelToPoint, pixelToPoint);
g.transform(at);
g.setColor(Color.WHITE);
g.fill(newPane.getBounds());
Rectangle alloc = getVisivleEditorRect(newPane);
newPane.getUI().getRootView(newPane).paint(g, alloc);
g.setColor(Color.BLACK);
g.draw(newPane.getBounds());
g.dispose();
cb.restoreState();
doc.close();
out.flush();
out.close();
}
private float convertToPixels(int points){
return (float) (points/pixelToPoint);
}
private Rectangle getVisivleEditorRect(JEditorPane newPane){
Rectangle alloc = newPane.getBounds();
if((alloc.width > 0) && (alloc.height > 0)){
alloc.x = alloc.y = 0;
Insets insets = newPane.getInsets();
alloc.x += insets.left;
alloc.y += insets.top;
alloc.width -= insets.left + insets.right;
alloc.height -= insets.top + insets.bottom;
return alloc;
}
return null;
}
,
int inch = Toolkit.getDefaultToolkit().getScreenResolution();
float pixelToPoint = (float) 72/(float) inch;
, 내가 지금까지 아무 소용, iText를하고 PDFBox와 expermenting 시도.
위의 솔루션이 com.lowagie
라이브러리를 사용하고 있음을 지적하고 싶습니다.
당신은 편집기 창 또는 텍스트의 형태를 저장할은? – Frakcool
텍스트를 명확히하기 위해서 (때문에), 당신이 염두에두고있는 해결책에 응해, JEditorPane를 JtextPane 또는 JTextArea로 변경할 수 있습니다. –
먼저 StandardPrint를 사용하여 JEditorPane (또는 다른 구성 요소) 디스플레이를 이미지로 변환 한 다음 lowagie를 사용하여 PDF로 변환 할 수 있습니다. https://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/ print/StandardPrint.java – ControlAltDel