title: PDFBox - embedding RTF content
body: <p>I am creating PDF file using PDFBox from my java code. During that, I need to embed one of the RTF(Rich Text Formate) file in between this new PDF. I cannot find any way to do this by doing google. </p>
  
  <p>I am sure, I cannot write the RTF content being read directly to the PDF.Even though I tried it as below code and it gives me error while opening the PDF. Error says the PDF is corrupted.</p>
  
  <p><strong>Note:</strong> Similar things can be done in C# and VB, <a href="http://www.syncfusion.com/kb/845/how-to-insert-an-rtf-text-into-a-pdf-document" rel="nofollow">http://www.syncfusion.com/kb/845/how-to-insert-an-rtf-text-into-a-pdf-document</a>. So I am optimistic that there must be something similar in java as well.</p>
  
  <p>I am using PDFBox 1.8.10</p>
  
  <p><strong>Sample Code:</strong></p>
  
  <pre><code>public static void main(String arg[])  throws IOException, COSVisitorException
  {
      final PDFont font = PDType1Font.HELVETICA_OBLIQUE;
      final int font_size = 8;
  
      PDDocument document = new PDDocument();
      PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
      document.addPage(page);
      PDPageContentStream contents = new PDPageContentStream(document, page, true, true);
      float x = 10f;
      float y = page.getMediaBox().getHeight();  
      float yOffset = 15f;
      float yCurrent = y - yOffset;  
  
      contents.beginText();
      contents.moveTextPositionByAmount(x, yCurrent);
      contents.setFont(font, font_size);
      contents.drawString("Start Text before RTF"); 
      contents.endText();
  
      yCurrent = yCurrent - yOffset;
  
      FileInputStream fi = new FileInputStream("C:\\Test.rtf");
      StringBuffer sb = new StringBuffer();
      BufferedReader br= new BufferedReader(new InputStreamReader(fi));
      int c;
      try {
          while((c=br.read()) != -1){
              sb.append((char)c);
          }
      } catch (IOException e) {
          throw e;
      }
      br.close();
      String rtfContent = sb.toString();
  
      contents.beginText();
      contents.moveTextPositionByAmount(x, yCurrent);
      contents.setFont(font, font_size);
      contents.drawString(rtfContent); 
      contents.endText();
      yCurrent = yCurrent - yOffset;
  
      contents.beginText();
      contents.moveTextPositionByAmount(x, yCurrent);
      contents.setFont(font, font_size);
      contents.drawString("End Text after RTF"); 
      contents.endText();
  
      contents.close();
  
  
      File file = new File("C:\\pdf\\RTF\\RRFSample1.pdf");
      System.out.println("Sample file saved at : " + file.getAbsolutePath());
      document.save(file);
      document.close();
  }
  </code></pre>