如果您不知道如何将 pt 转换为 x,y 位置,也许这会有所帮助:
要从像素到 Em,只需除以 16
使用 Reed Design 的表格,该表格列出了从点到像素的转换
到 Em 到 %。这是一个近似值,取决于字体、浏览器
和操作系统,但这是一个很好的起点。
https://answers.yahoo.com/question/index?qid=20070723201114AAzsYjC
否则,我会为图像执行类似于 yo 的操作:
/**
* Adds an image to the PDF
* @param imageData The data for the image to add
* @param page The page number of where to add the photo
* @param xPos The x coordinate of where to put the image. Note that 0 is the left of the page
* @param yPos The y coordinate of where to put the image. Note that 0 is the top of the page. This is
* different than the addText function for some reason.
* @param layer
* @throws Exception If there was an error adding the image to the PDF
*/
public void addImageToPDF(float xPos, float yPos, byte[] imageData, int page, Layer layer) throws Exception
{
PdfContentByte content = getPdfContentByte(page, layer);
Image img = Image.getInstance(imageData);
img.setAbsolutePosition(xPos, yPos);
content.addImage(img);
}
/**
* Gets a PdfContentByte. This is used to make changes to the PDF
* @param page The page number that is to be modified
* @param layer The layer that is to be modified
* @return A PdfContentByte that can be used to modify the PDF
*/
private PdfContentByte getPdfContentByte(int page, Layer layer)
{
return layer == Layer.OVER ? stamper.getOverContent(page) : stamper.getUnderContent(page);
}
Layer 是一个简单的枚举,用于确定 PDF 的哪一层:
/**
* Represents a layer of the PDF relative to the original file. For example,
* if an operation requires a Layer argument, OVER will make the changes ontop
* of the original PDF. UNDER will make the changes under the original PDF. So
* if there is text in the original PDF and you choose UNDER, the changes you make
* may be covered up by the original text. If you choose OVER, you changes will be
* ontop of whatever was in the original PDF
*/
public enum Layer{
/**
* Represents ontop of the original PDF
*/
OVER,
/**
* Represents under the original PDF
*/
UNDER
}