Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用PDFBox添加页码_Java_Pdf_Pdfbox - Fatal编程技术网

Java 使用PDFBox添加页码

Java 使用PDFBox添加页码,java,pdf,pdfbox,Java,Pdf,Pdfbox,如何向使用PDFBox生成的文档中的页面添加页码 有人能告诉我在合并不同的PDF后如何向文档添加页码吗?我正在使用Java中的PDFBox库 这是我的代码,它工作得很好,但我需要添加页码 PDFMergerUtility ut = new PDFMergerUtility(); ut.addSource("c:\\pdf1.pdf"); ut.addSource("c:\\pdf2.pdf"); ut.addSource("c:\\pdf3.pd

如何向使用PDFBox生成的文档中的页面添加页码

有人能告诉我在合并不同的PDF后如何向文档添加页码吗?我正在使用Java中的PDFBox库

这是我的代码,它工作得很好,但我需要添加页码

 PDFMergerUtility ut = new PDFMergerUtility();
        ut.addSource("c:\\pdf1.pdf");
        ut.addSource("c:\\pdf2.pdf");
        ut.addSource("c:\\pdf3.pdf");
        ut.mergeDocuments();

您可能需要查看PDFBox示例。中心代码是:

try (PDDocument doc = PDDocument.load(new File(file)))
{
    PDFont font = PDType1Font.HELVETICA_BOLD;
    float fontSize = 36.0f;

    for( PDPage page : doc.getPages() )
    {
        PDRectangle pageSize = page.getMediaBox();
        float stringWidth = font.getStringWidth( message )*fontSize/1000f;
        // calculate to center of the page
        int rotation = page.getRotation();
        boolean rotate = rotation == 90 || rotation == 270;
        float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth();
        float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight();
        float centerX = rotate ? pageHeight/2f : (pageWidth - stringWidth)/2f;
        float centerY = rotate ? (pageWidth - stringWidth)/2f : pageHeight/2f;

        // append the content to the existing stream
        try (PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true))
        {
            contentStream.beginText();
            // set font and font size
            contentStream.setFont( font, fontSize );
            // set text color to red
            contentStream.setNonStrokingColor(255, 0, 0);
            if (rotate)
            {
                // rotate the text according to the page rotation
                contentStream.setTextMatrix(Matrix.getRotateInstance(Math.PI / 2, centerX, centerY));
            }
            else
            {
                contentStream.setTextMatrix(Matrix.getTranslateInstance(centerX, centerY));
            }
            contentStream.showText(message);
            contentStream.endText();
        }
    }

    doc.save( outfile );
}
1.8.x吊坠为:

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

    List allPages = doc.getDocumentCatalog().getAllPages();
    PDFont font = PDType1Font.HELVETICA_BOLD;
    float fontSize = 36.0f;

    for( int i=0; i<allPages.size(); i++ )
    {
        PDPage page = (PDPage)allPages.get( i );
        PDRectangle pageSize = page.findMediaBox();
        float stringWidth = font.getStringWidth( message )*fontSize/1000f;
        // calculate to center of the page
        int rotation = page.findRotation(); 
        boolean rotate = rotation == 90 || rotation == 270;
        float pageWidth = rotate ? pageSize.getHeight() : pageSize.getWidth();
        float pageHeight = rotate ? pageSize.getWidth() : pageSize.getHeight();
        double centeredXPosition = rotate ? pageHeight/2f : (pageWidth - stringWidth)/2f;
        double centeredYPosition = rotate ? (pageWidth - stringWidth)/2f : pageHeight/2f;
        // append the content to the existing stream
        PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true,true);
        contentStream.beginText();
        // set font and font size
        contentStream.setFont( font, fontSize );
        // set text color to red
        contentStream.setNonStrokingColor(255, 0, 0);
        if (rotate)
        {
            // rotate the text according to the page rotation
            contentStream.setTextRotation(Math.PI/2, centeredXPosition, centeredYPosition);
        }
        else
        {
            contentStream.setTextTranslation(centeredXPosition, centeredYPosition);
        }
        contentStream.drawString( message );
        contentStream.endText();
        contentStream.close();
    }

    doc.save( outfile );
}
finally
{
    if( doc != null )
    {
        doc.close();
    }
}
PDDocument doc=null;
尝试
{
doc=PDDocument.load(文件);
List allPages=doc.getDocumentCatalog().getAllPages();
PDFont font=PDType1Font.HELVETICA_粗体;
浮动字体大小=36.0f;

对于(inti=0;i这很容易,请尝试以下代码

public static void addPageNumbers(PDDocument document, String numberingFormat, int offset_X, int offset_Y) throws IOException {
        int page_counter = 1;
        for(PDPage page : document.getPages()){
            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, false);
            contentStream.beginText();
            contentStream.setFont(PDType1Font.TIMES_ITALIC, 10);
            PDRectangle pageSize = page.getMediaBox();
            float x = pageSize.getLowerLeftX();
            float y = pageSize.getLowerLeftY();
            contentStream.newLineAtOffset(x+ pageSize.getWidth()-offset_X, y+offset_Y);
            String text = MessageFormat.format(numberingFormat,page_counter);
            contentStream.showText(text);
            contentStream.endText();
            contentStream.close();
            ++page_counter;
        }
    }



public static void main(String[] args) throws Exception {
        File file = new File("your input pdf path");
        PDDocument document = PDDocument.load(file);
        addPageNumbers(document,"Page {0}",60,18);
        document.save(new File("output pdf path"));
        document.close();
    }

请在句子的开头加一个大写字母。单词I和专有名称(如Java)也要大写,缩写和首字母缩写词(如JEE或WAR)也要大写。这使人们更容易理解和帮助。可能的改进:a您使用
false
作为
PDPageContentStream的
resetContext
参数。如果文档的内容流未以干净的图形状态结束,则可能会得到不需要的结果。B您使用
getMediaBox
作为页面大小;更好的选择是
getCropBox