将段落或单元格旋转任意度数——Itext [英] Rotate paragraphs or cells some arbitrary number of degrees -- Itext

查看:34
本文介绍了将段落或单元格旋转任意度数——Itext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户可以在其中上传照片并创建相册.此外,他们可以在绝对位置、旋转和对齐位置添加文本.文本可以有新行.

I have a web site where the users upload photos and create photobooks. Also, they can add text at absolute positions, rotations, and alignments. The text can have new lines.

我一直在使用 Itext 库来自动创建稍后打印的高质量 Pdf 相册.

I've been using the Itext Library to automatize the creation of the Photobooks High Quality Pdfs that are printed latter on.

将用户上传的图像添加到 PDF 非常简单,当我尝试添加文本时出现问题.

Adding the user uploaded images to the PDFs was really simple, the problem comes when I try to add the text.

理论上我需要做的是定义一段具有一定宽度和高度的段落,设置用户文本、字体、字体样式、对齐方式(居中、左、右、对齐),最后设置旋转.

In theory what I would need to do, is to define a paragraph of some defined width and height, set the users text, font, font style, alignment (center, left, right, justify), and finally set the rotation.

对于我所阅读的有关 Itext 的内容,我可以创建一个段落来设置用户属性,并使用 ColumnText 对象来设置绝对位置、宽度和高度.但是,不可能设置任何大于单行的旋转.

For what i've read about Itext, i could create a paragraph set the user properties, and use a ColumnText Object to set the absolute position, width and height. However it's not possibly to set the rotation of anything bigger than single line.

我也不能使用表格单元格,因为旋转方法只允许 90 的倍数.

I can't use table cells either, because the rotation method only allow degrees that are multiples of 90.

有没有一种方法可以添加一段旋转(比如 20 度)而无需使用 ColumnText.showTextAligned() 方法和所有涉及的数学逐行添加文本?​​

Is there a way to add a paragraph with some rotation (say 20 degrees) without having to add the text line by line using the ColumnText.showTextAligned() method and all math that involves?

---- 08-Ago-2013 ----

如果它对任何人有帮助,这是我用来解决这个问题的代码(感谢 Bruno):

If it helps anyone, this is the code I used to solve this problem (thanks to 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);

推荐答案

  • 创建一个 PdfTemplate 对象;只是一个矩形.
  • 在这个PdfTemplate上画出你的ColumnText;不用担心旋转,只需用您要添加到列中的任何内容填充矩形即可.
  • PdfTemplate 包装在 Image 对象中;这只是为了方便,避免数学.这并不意味着您的文本会被光栅化.
  • 现在对 Image 应用旋转和绝对位置并将其添加到您的文档中.
    • Create a PdfTemplate object; just a rectangle.
    • Draw your ColumnText on this PdfTemplate; don't worry about the rotation, just fill the rectangle with whatever content you want to add to the column.
    • Wrap the PdfTemplate inside an Image object; this is just for convenience, to avoid the math. This doesn't mean your text will be rasterized.
    • Now apply a rotation and an absolute position to the Image and add it to your document.
    • 你的问题现在解决了 ;-)

      Your problem is now solved ;-)

      PS:我是 iText in Action 书籍的作者.

      PS: I'm the author of the iText in Action books.

      这篇关于将段落或单元格旋转任意度数——Itext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆