2

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

 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"));


                }

            }

     } 
4

0 回答 0