2013-07-22 50 views
0

我想通过使用PDFBox来应用“文本渲染模式不可见”。我编写了这段代码,但我不知道如何继续调用过程函数来设置文档中不可见的文本。 任何答复赞赏。带PDFBox的文本渲染模式

public class Test1 extends OperatorProcessor{ 
    private static final String src="..."; 
    private static PDFStreamEngine pp; 
    private static PDPageContentStream content; 
    private static PDType1Font font; 
    public static void CreatePdf(String src) throws IOException, COSVisitorException{ 
    PDRectangle rec= new PDRectangle(400,400); 
    PDDocument document= null; 
    document = new PDDocument(); 
    PDPage page = new PDPage(rec); 
    document.addPage(page); 
    PDDocumentInformation info=document.getDocumentInformation(); 
    info.setAuthor("PdfBox"); 
    info.setCreator("Pdf"); 
    info.setSubject("Stéganographie"); 
    info.setTitle("Stéganographie dans les documents PDF"); 
    info.setKeywords("Stéganographie, pdf"); 
    content= new PDPageContentStream(document, page); 
    pp=new PDFStreamEngine(); 
    font= PDType1Font.HELVETICA; 
    String texte="hello"; 
    content.beginText(); 
    content.setFont(font, 12); 
    content.moveTextPositionByAmount(15, 385);   
    content.drawString(texte); 
    content.endText(); 
    content.close(); 
    document.save("doc.pdf"); 
    document.close();  
    } 
    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException, COSVisitorException { 
     // TODO code application logic here 
     Test1 tes= new Test1(); 
     tes.CreatePdf(src); 
    } 
    @Override 
    public void process(PDFOperator pdfo, List<COSBase> list) throws IOException { 
      COSNumber mode = (COSNumber)list.get(0); 
      pp.getGraphicsState().getTextState().setRenderingMode(mode.intValue()); 
    } 
} 

此致敬礼, 李斯特。

回答

1

当通过PDPageContentStream文档http://pdfbox.apache.org/apidocs/,我觉得没有明确的方法(我可能是错误的,因为我不熟悉此产品)。

有一个appendRawCommands通过,这似乎是一个很好的逃生。我希望你会做这样的事情(未经测试):

content.beginText(); 
content.setFont(font, 12); 
content.moveTextPositionByAmount(15, 385);   
content.appendRawCommands("3 Tr "); 
content.drawString(texte); 
content.endText();