Java 使用PDFBox的文本渲染模式

Java 使用PDFBox的文本渲染模式,java,pdf,documentation,pdfbox,Java,Pdf,Documentation,Pdfbox,我试图通过使用PDFBox应用“文本呈现模式不可见”。我写了这段代码,但我不知道如何继续,甚至不知道如何调用process函数来设置文档中不可见的文本。 如蒙答复,不胜感激 public class Test1 extends OperatorProcessor{ private static final String src="..."; private static PDFStreamEngine pp; private static PDPageConten

我试图通过使用PDFBox应用“文本呈现模式不可见”。我写了这段代码,但我不知道如何继续,甚至不知道如何调用process函数来设置文档中不可见的文本。 如蒙答复,不胜感激

    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());
    }
}
公共类Test1扩展了运算符处理器{
私有静态最终字符串src=“…”;
私有静态PDFStreamEngine pp;
私有静态PDPageContentStream内容;
私有静态PDType1Font字体;
publicstaticvoidcreatepdf(stringsrc)抛出IOException、COSVisitorException{
PDRectangle rec=新的PDRectangle(400400);
PDDocument文件=null;
文档=新的PDDocument();
PDPage=新的PDPage(rec);
文件。添加页(第页);
PDDocumentInformation=document.getDocumentInformation();
信息集作者(“PdfBox”);
info.setCreator(“Pdf”);
info.setSubject(“圣加诺文字”);
info.setTitle(“Stéganographie dans les documents PDF”);
info.setKeywords(“Stéganographie,pdf”);
内容=新PDPageContentStream(文档,页面);
pp=新的PDFStreamEngine();
font=PDType1Font.HELVETICA;
String texte=“hello”;
content.beginText();
content.setFont(字体,12);
content.moveTextPositionByAmount(15385);
内容。抽绳(texte);
content.endText();
content.close();
document.save(“doc.pdf”);
document.close();
}
/**
*@param指定命令行参数
*/
publicstaticvoidmain(字符串[]args)抛出IOException、COSVisitorException{
//此处的TODO代码应用程序逻辑
Test1-tes=新的Test1();
tes.CreatePdf(src);
}
@凌驾
公共作废进程(PDFOOperator pdfo,List List)引发IOException{
COSNumber mode=(COSNumber)list.get(0);
pp.getGraphicsState().getTextState().setRenderingMode(mode.intValue());
}
}
致以最良好的祝愿,
李斯特。

在浏览PDPageContentStream的文档时,我感觉没有明确的方法来实现这一点(我可能是错的,因为我不熟悉此产品)

有一个命令通过,这似乎是一个很好的逃避。我希望您会这样做(未经测试):

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