2011-03-02 3 views
1

기본 Spring 메시지 수신기가 실행 중입니다. 의 onMessage, 그것은 끝난 TextMessage (NOT BytesMessage의)로 제공 명중Spring JMS TextMessage에서 PDF로 쓰기

어떻게 쓰기 않는 PDF 파일로?

내가 아래에있는 내 코드에 약간의 문제가 있다고 생각 ... 그래서 파일에 기록하지만, 어떤 제안에 대한 PDF 파일이 열리지 않습니다 ...

if (message instanceof TextMessage) { 
     try { 
      //System.out.println(((TextMessage) message).getText()); 

      TextMessage txtMessage = (TextMessage)message; 
      ByteArrayInputStream bais = new ByteArrayInputStream(txtMessage.getText().getBytes("UTF8")); 

      String outStr=bais.toString(); 

      File newFile=new File("D:\\document.pdf"); 
      FileOutputStream fos = new FileOutputStream(newFile); 
      int data; 
      while((data=bais.read())!=-1) 
      { 
      char ch = (char)data; 
      fos.write(ch); 
      } 
      fos.flush(); 
      fos.close(); 

감사

답변

1

고려하시기 바랍니다 pdf 특정 API를 사용하여 pdf 파일을 작성/업데이트합니다. 나는 iText을 강력히 추천합니다. pdf 파일은 단순히 바이트 스트림이 아닙니다. 많은 것들이 관련되어 있고 글꼴, 페이지 크기, X 및 Y 좌표 시작, 텍스트 방향, 새 페이지 추가, 테이블 구조 또는 무료 스타일 및 목록 등을 고려해야합니다.

사이트에 시작될 수있는 코드 예제가 많이 있습니다. 다음은 iText API를 사용하여 pdf 파일에 텍스트를 추가하는 단순화 된 스 니펫입니다.

try { 
    ... 

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pdfFile)); 

    ... 

    PdfReader reader = new PdfReader(bis); 

    /* outs could be any output stream */ 

    stamper = new PdfStamper(reader,outs); 

    ... /* removed the code to get current page */ 

    PdfContentByte over = stamper.getOverContent(currentPage); 
    over.beginText(); 
    over.setFontAndSize(myFont, myFontSize); 
    over.setTextMatrix(xPoint, yPoint); 
    over.showText("Add this text"); 
    over.endText(); 
    ... /* removed code to adjust x and y coordinate and add page if needed */ 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} finally { 
    try { 
     stamper.close(); 
    } catch (Exception ex) {/* handle exception */} 

    try { 
     outs.flush(); 
     outs.close(); 
    } catch (Exception ignored) {/* handle exception */} 

} 
관련 문제