在PDFbox上将PDF设置为图像到图像

时间:2013-05-21 08:24:41

标签: java pdf fonts pdfbox true-type-fonts

我正在尝试设置一个外部ttf,以便能够从包含阿拉伯语字符的pdf中渲染图像,我需要设置的字体是“Arial”,完成后控制台继续通知它正在使用defaultFont而不是arial。

 org.apache.pdfbox.pdmodel.font.PDSimpleFont drawString
ATTENTION: Changing font on <3> from <Arial> to the default font

这是我的代码:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;

    public class PdfBoxTest
     {
        public static void main(String[] args)
        {
            PdfBoxTest obj = new PdfBoxTest();
                try 
                {
                    obj.read_pdf();
                }

                catch (IOException ex)
                {
                    System.out.println("" + ex);
                }

        }

        void read_pdf() throws IOException 
        {
                PDDocument document = null; 
                try 
                {
                    document = PDDocument.load("c:/result.pdf");
                }
                catch (IOException ex)
                {
                    System.out.println("" + ex);
                }


                int i =1;
                String name = null;
                PDFont font = PDTrueTypeFont.loadTTF(document, new File( "c:/arial.ttf" ));
                font.setWidths(PDType1Font.HELVETICA.getWidths());
                List<PDPage>pages =  document.getDocumentCatalog().getAllPages();
                Iterator<PDPage> iter =  pages.iterator(); 
                while (iter.hasNext()) 
                {
                    PDPage page = (PDPage) iter.next();


                    BufferedImage img = page.convertToImage();
                    ImageIO.write(img, "png", new
                             File("C:/" + i + ".png"));


                }

            }

     } 

0 个答案:

没有答案