我有一个网站,用户可以上传照片并创建相册。此外,他们还可以在绝对位置、旋转和对齐方式下添加文本。文本可以有换行符。
我一直在使用Itext库来自动创建稍后打印的Photobooks高质量PDF。
将用户上传的图像添加到PDF中非常简单,但当我尝试添加文本时就会出现问题。
理论上,我需要做的是定义一个指定宽度和高度的段落,设置用户的文本、字体、字体样式、对齐方式(居中、左对齐、右对齐、两端对齐)以及最后设置旋转。
从我了解的Itext方面来看,我可以创建一个段落,设置用户属性,然后使用ColumnText对象设置绝对位置、宽度和高度。但是,无法为大于单行的任何内容设置旋转。
我也不能使用表格单元格,因为旋转方法只允许是90的倍数度数。
有没有一种方法可以添加具有某些旋转(比如20度)的段落,而不必使用ColumnText.showTextAligned()方法逐行添加文本并涉及所有数学计算呢?
----编辑:08-Ago-2013----
如果有帮助的话,这是我用来解决这个问题的代码(感谢Bruno):
//Create the template that will contain the text
PdfContentByte canvas = pdfWriter.getDirectContent();
PdfTemplate textTemplate = canvas.createTemplate(imgWidth, imgHeight); //The width and height of the text to be inserted
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, imgWidth, imgHeight);
columnText.addElement(paragraph);
columnText.go();
//Create de image wraper for the template
Image textImg = Image.getInstance(textTemplate);
//Asign the dimentions of the image, in this case, the text
textImg.setInterpolation(true);
textImg.scaleAbsolute(imgWidth, imgHeight);
textImg.setRotationDegrees((float) -textComp.getRotation()); //Arbitrary number of degress
textImg.setAbsolutePosition(imgXPos, imgYPos);
//Add the text to the pdf
pdfDocument.add(textImg);