小编Ani*_*rni的帖子

使用PDFBox将标题添加到现有PDF文件

我正在尝试将标头添加到现有的PDF文件中.它可以工作,但现有PDF中的表头由字体的变化搞砸了.如果我删除设置字体,则标题不会显示.这是我的代码:

    // the document
    PDDocument doc = null;
    try
    {
        doc = PDDocument.load( file );

        List allPages = doc.getDocumentCatalog().getAllPages();
        //PDFont font = PDType1Font.HELVETICA_BOLD;

        for( int i=0; i<allPages.size(); i++ )
        {
            PDPage page = (PDPage)allPages.get( i );
            PDRectangle pageSize = page.findMediaBox();
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
            PDFont font = PDType1Font.TIMES_ROMAN;
            float fontSize = 15.0f;
            contentStream.beginText();
            // set font and font size
            contentStream.setFont( font, fontSize);
            contentStream.moveTextPositionByAmount(700, 1150);
            contentStream.drawString( message);
            contentStream.endText();

            //contentStream.
            contentStream.close();}

        doc.save( outfile );
    }
    finally
    {
        if( …
Run Code Online (Sandbox Code Playgroud)

java pdf pdfbox

3
推荐指数
1
解决办法
7805
查看次数

标签 统计

java ×1

pdf ×1

pdfbox ×1