How to draw a string at a specific position on a pdf page in java using pdfbox?

前端 未结 2 772
执笔经年
执笔经年 2021-01-16 16:06

I have a pdf coordinate (x, y) as input . I need to draw a string at the given input coordinate[Eg :- (x,y)=(200,250)]. I am using pdfbox , When I am using the below method

相关标签:
2条回答
  • 2021-01-16 16:36

    Getting rid of graphic state changes from the existing page content

    You use the PDPageContentStream constructor with two boolean arguments:

    new PDPageContentStream(document, page,true,true);
    

    This constructor is implemented as:

    this(document, sourcePage, appendContent, compress, false);
    

    i.e. it calls the constructor with three boolean arguments using false for the final one. This final boolean argument is documented as:

    * @param resetContext Tell if the graphic context should be reseted.
    

    Thus, you append to the page content without resetting the graphic context. This means that any changes to the current transformation matrix done in the existing page content still transforms your coordinates. To prevent that from happening you should use the PDPageContentStream constructor with three boolean arguments:

    new PDPageContentStream(document, page, true, true, true);
    

    Using this one can easily position text.

    Drawing rectangles and test

    The OP mentioned that he was successful drawing rectangles but not drawing text.

    The following code

    PDPage firstPage = allPages.get(0);
    PDRectangle pageSize = firstPage.findMediaBox();
    
    float x = 121;
    float y = 305;
    float w = 262;
    float h = 104;
    
    PDPageContentStream contentStream = new PDPageContentStream(document, firstPage, true, true, true);
    
    contentStream.setNonStrokingColor(Color.yellow);
    contentStream.fillRect(pageSize.getLowerLeftX() + x, pageSize.getLowerLeftY() + y, w, h);
    
    contentStream.beginText();
    contentStream.moveTextPositionByAmount(pageSize.getLowerLeftX() + x, pageSize.getLowerLeftY() + y);
    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
    contentStream.setNonStrokingColor(Color.red);
    contentStream.drawString("My Text Here");
    contentStream.endText();
    contentStream.close();
    

    results in

    Screenshot of result

    as would be expected.

    Meaning of input coordinates must be explained

    The OP also mentioned X:-121,Y:-305,W:-262,h:-104 as coordinates from external application in his comments.

    As PDFs most often have positive coordinates inside the media box, these X and Y coordinates make no sense for PDFs in general.

    Furthermore the OP was unable to share the document.

    Therefore, it could not be found out whether or not those negative coordinates make sense for his special PDF.

    Additionally negative values for widths and height are accepted by the rectangle drawing operations, but if used for text, they might imply that the Y coordinate does not denote the baseline, or that the text is not expected to start at X but to end there, or that the text shall be mirrored, or, or, or...

    Thus, the meaning of those negative coordinates and dimensions must first be explained. Which is the origin of those coordinates, are the positive y coordinates above or below, is X,Y the lower left of the rectangle, what is the meaning of a negative width or height, where in relation to X, Y shall the string be drawn?

    0 讨论(0)
  • 2021-01-16 16:39

    I found that this one worked for me .

    PDPageContentStream contentStream = new PDPageContentStream(document, page,true,true);
    contentStream.beginText();
    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
    contentStream.moveTextPositionByAmount(xindex, yindex);
    contentStream.setNonStrokingColor(color);
    contentStream.drawString(comment);                      
    contentStream.endText();
    
    0 讨论(0)
提交回复
热议问题