[ 
https://issues.apache.org/jira/browse/PDFBOX-1578?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13641730#comment-13641730
 ] 

Harippriya Parameswaran commented on PDFBOX-1578:
-------------------------------------------------

Hi ,

I tried the below piece of code as you told me to. The PDF it creates is fine 
as we directly write the text into it, but when we tried to save it into a 
image, since we need a image to be superimposed this issue occurs.
Please find the sample attachments. 


String fontfile = "Monika.ttf";
        String message = "Hello World"; 
                
                
                
        PDDocument doc = null;
        try
        {
            doc = new PDDocument();

            PDPage page = new PDPage();
            doc.addPage(page);
            PDFont font = PDTrueTypeFont.loadTTF(doc, fontFile);
                
            PDPageContentStream contentStream = new PDPageContentStream(doc,
                    page);
            contentStream.beginText();
            contentStream.setFont(font, 12);
            contentStream.moveTextPositionByAmount(100, 700);
            contentStream.drawString(message);
            contentStream.endText();
            contentStream.close();
            doc.save("/Users/Test/Test.pdf");
            
            BufferedImage buffer = null;
            buffer = page.convertToImage(BufferedImage.TYPE_INT_RGB, 94);
            ImageIO.write(buffer,"jpeg",new File("/Users/Test/"+ "text.jpeg")); 
          
            
        }
        finally
        {
            if (doc != null)
            {
                doc.close();
            }
        }


Regards,
Harippriya 
                
> The English Words are shown as Symbols and wrongly displayed 
> -------------------------------------------------------------
>
>                 Key: PDFBOX-1578
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1578
>             Project: PDFBox
>          Issue Type: Bug
>          Components: FontBox, JempBox
>    Affects Versions: 1.7.0, 1.8.1
>         Environment: Java, MacOS, Linux
>            Reporter: Harippriya Parameswaran
>            Assignee: Andreas Lehmkühler
>            Priority: Blocker
>         Attachments: Test.pdf, text.jpeg
>
>
> When i run the below piece of code in my server continuously it is creating 
> the text with symbols and showing incorrect word.
>   String fontFile = "Monika.ttf"; 
>               String text="Hello"; 
>              float fontSize = 20; 
>             Font font = null; 
>         try { 
>                  
>                  font = Font.createFont(Font.TRUETYPE_FONT, 
>                  Sample.class.getClassLoader() 
>                  .getResource(fontFile).openStream()); 
>                  font = font.deriveFont(fontSize); 
>         } catch (Exception e) { 
>                 throw new IOException("could not load TrueTypeFont for file: 
> " 
>                                 + fontFile, e); 
>         } 
>         FontRenderContext fc = new FontRenderContext(null, true, true); 
>         Rectangle2D bounds = font.getStringBounds(text, fc); 
>         int width = (int) bounds.getWidth(); 
>         int height = (int) bounds.getHeight(); 
>         int maxWidth = 500; 
>         int maxHeight = 50; 
>         int minFontSize = 20; 
>         while (width > (maxWidth - 2 *5) 
>                         || height > (maxHeight - 2 * 5)) { 
>                 if (fontSize <= minFontSize) { 
>                         break; 
>                 } 
>                 fontSize--; 
>                 font = font.deriveFont(fontSize); 
>                 bounds = font.getStringBounds(text, fc); 
>                 width = (int) bounds.getWidth(); 
>                 height = (int) bounds.getHeight(); 
>         } 
>         int paddingWidth = 5; 
>         int paddingHeight = 5; 
>         BufferedImage buffer = null; 
>                 PDDocument doc = new PDDocument(); 
>                 PDPage page = new PDPage(new PDRectangle(width + 2 * 
> paddingWidth, 
>                                 height + 2 * paddingHeight)); 
>                  
>                 BufferedImage newBufferedImage = ImageIO.read(Sample.class 
>                                 .getClassLoader() 
>                                 
> .getResource(Sample.get("image.blue.background")) 
>                                 .openStream()); 
>                 PDJpeg newImage = new PDJpeg(doc, newBufferedImage); 
>                 PDFont pdffont = null; 
>                 try { 
>                          
>                          pdffont = PDTrueTypeFont.loadTTF(doc, 
>                          
> Sample.class.getClassLoader().getResource(fontFile).openStream()); 
>                          
>                 } catch (Exception e) { 
>                         throw new IOException( 
>                                         "could not load PDTrueTypeFont for 
> file: " + fontFile, 
>                                         e); 
>                 } 
>                 PDPageContentStream stream = new PDPageContentStream(doc, 
> page); 
>                 stream.drawImage(newImage, 0, 0); 
>                 stream.setNonStrokingColor(Color.BLACK); 
>                 stream.setStrokingColor(Color.BLACK); 
>                 stream.beginText(); 
>                 stream.setFont(pdffont, fontSize); 
>                 stream.moveTextPositionByAmount(paddingWidth, 
>                                 (float) (height / 2.5 + paddingHeight)); 
>                 stream.drawString(text); 
>                 stream.endText(); 
>                 stream.close(); 
>                 buffer = page.convertToImage(BufferedImage.TYPE_INT_RGB, 
>                                 94); 
>      
>        // Convert image image to PDXObjectImage 
>         PDXObjectImage watermark = new PDJpeg(doc, buffer); 
>         @SuppressWarnings("rawtypes") 
>         List pages = doc.getDocumentCatalog().getAllPages(); 
>         Iterator iterator1 = pages.iterator(); 
>         PDPage page1=null; 
>         while(iterator1.hasNext()){ 
>             page1 = (PDPage) iterator1.next(); 
>         } 
>            PDPageContentStream stream1 = new PDPageContentStream(doc, page1, 
> true, true); 
>              stream1.drawImage(watermark,50,60); 
>             stream1.close(); 
>                  ImageIO.write(buffer, "jpeg", new 
>                   File("/Users/test/"+"text.jpeg")); 
>                 doc.close();    

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to