Someone asked me before how to use itext7 to insert text in the absolute position of the pdf? In the past few days, I have also checked relevant information and told you about relevant application scenarios and solutions.
Application scenarios
In fact, there are still many application scenarios for inserting text in the absolute position in the pdf. The most typical application scenarios are as follows:
- Contract template uses PDF
- Most of the text in the contract has been written, and a small part of the content is filled in by the user, such as name, ID number, etc.
- The absolute position of the content to be filled in has been determined. At this time, our web page or other terminals receive the content filled in by the user and fill in the corresponding position in the PDF according to the content.
This is a typical application scenario. In fact, there are still many places where you need to use this operation, let's how to realize this operation.
Do you remember that we mentioned in the second chapter of itext7 that the origin of the most primitive coordinates is at the lower left position of the PDF.
Method 1: setFixedPosition-Advanced API
The easiest way is to use Paragraph
the setFixedPosition
method of the object , which is an inherited com.itextpdf.layout.ElementPropertyContainer
method. The method prototype is as follows:
Function prototype :
public T setFixedPosition(int pageNumber, float left,
float bottom,
UnitValue width)
Parameters :
- pageNumber: To set the page number of the absolute position
- left: the x coordinate of the lower left corner of the added text relative to the origin
- bottom: The y coordinate of the lower left corner of the added text relative to the origin
- width: the horizontal width of the added text
Code example :
Text text = new Text(String.format("Page %d", pdf.getNumberOfPages() - 1));
text.setBackgroundColor(Color.WHITE);
//前面这个text主要是设置背景色为白色,如果text的位置上面有内容就会覆盖掉内容
document.add(new Paragraph(text).setFixedPosition(
pdf.getNumberOfPages(), 549, 742, 100)); //这里面width取决于留空的宽度,这里我们尽量取大一点
Operation result :
Selection of width parameter
Here we need to pay attention to the selection of the width parameter. If your width is selected too small, then your text will be displayed in multiple lines. The following figure shows the effect when width=10:
When width=20, the effect is as follows:
Method 2: Use Canvas-low-level API
This approach is the use of Rectangle
and Canvas
combine to add text in a particular location, we look directly following code:
PdfPage page = pdf.addNewPage();
PdfCanvas pdfCanvas = new PdfCanvas(page);
Rectangle[] columns = {new Rectangle(6, 650, 100, 30),
new Rectangle(50, 500, 100, 100),}; //几个Rectangle对应几个位置
pdfCanvas.rectangle(columns[0]);
pdfCanvas.stroke();
Canvas canvas = new Canvas(pdfCanvas, pdf, columns[0]);
Paragraph p=new Paragraph("hssssas").setFont(f3).setBold().setFontSize(10); //Bold为设置粗体
canvas.add(p);
Proceed as follows:
- Design the corresponding number according to the space left in the contract
Rectangle
- Use the first rectangle to set
pdfCanvas
the position of the current canvas, indicating that you want to add content to this rectangle - Call the
stroke()
function to display the rectangle. Of course, you can leave this function unused and the rectangle will not be displayed. pdfCanvas
Create anCanvas
instance based on- Create an
Paragraph
object, bold it, set the font style, and set the font size. - To
Canvas
add this inParagraph
The result is shown below:
PS: Adding content to the rectangle will encounter other situations. For example, the added content exceeds the size of the rectangle. What will happen to the added content? Is there a solution? You can try it yourself first, I will mention this problem in other chapters