【问题标题】:Rotate multiline text with Columntext ITextSharp使用 Columntext ITextSharp 旋转多行文本
【发布时间】:2014-09-12 12:28:43
【问题描述】:

如何在 itextsahrp 中旋转多行文本?

我试过了:

float x = 200;
float y = 100;
PdfContentByte cb = stamper.GetOverContent(i);
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(new Phrase(new Chunk("Test \n new", FontFactory.GetFont(FontFactory.HELVETICA, 18, iTextSharp.text.Font.NORMAL))),
                   x, reader.GetCropBox(i).Height -( y+400),500+x, y, 10, Element.ALIGN_LEFT | Element.ALIGN_TOP);

ct.Go(); 

ColumnText.ShowTextAligned(
    cb, Element.ALIGN_CENTER,
    new Phrase(new Chunk("Test \n new", FontFactory.GetFont(FontFactory.HELVETICA, 18, iTextSharp.text.Font.NORMAL))), x, reader.GetCropBox(i).Height-y, 12);

ct.SetSimpleColumn 显示多重文本,但我如何旋转它?

ColumnText.ShowTextAligned 不显示多行。

【问题讨论】:

  • 我在回答您之前的问题时已经对此进行了解释:不要将ColumnText 添加到stamper.GetOverContent(i),而是创建一个Form XObject(使用PdfTemplate 对象)。将ColumnText 添加到此PdfTemplate 并使用AddTemplate() 方法将模板添加到stamper.GetOverContent(i),该方法接受参数以旋转模板。

标签: c# itextsharp


【解决方案1】:

我的机器上没有 C# 环境,所以我用 Java 编写了一个名为 AddRotatedTemplate 的示例。我已经获取了一个带有“Hello World”字样的现有 PDF 文件,我创建了一个带有一些文本的模板/XObject,并且我使用 addTemplate() 方法添加了该模板/XObject 两次(一次仅使用 x,y坐标和一次使用以 PI / 4 等级旋转文本的参数)。结果,添加到模板中的文本被添加了两次;见hello_template.pdf)。

这是代码:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    // Get canvas for page 1
    PdfContentByte cb = stamper.getOverContent(1);
    // Create template (aka XOBject)
    PdfTemplate xobject = cb.createTemplate(80, 120);
    // Add content using ColumnText
    ColumnText column = new ColumnText(xobject);
    column.setSimpleColumn(new Rectangle(80, 120));
    column.addElement(new Paragraph("Some long text that needs to be distributed over several lines."));
    column.go();
    // Add the template to the canvas
    cb.addTemplate(xobject, 36, 600);
    double angle = Math.PI / 4;
    cb.addTemplate(xobject,
            (float)Math.cos(angle), -(float)Math.sin(angle),
            (float)Math.cos(angle), (float)Math.sin(angle),
            150, 600);
    stamper.close();
    reader.close();
}

addTemplate() 方法的其他变体中可能有更简单的方法来定义旋转,但我是一名工程师,我非常习惯使用转换矩阵,因此我从不觉得需要或希望使用任何其他方法方法的类型。

【讨论】:

  • 谢谢你!它有很大帮助。但是在第二个 addTemplate 方法中,三角函数的顺序是错误的。正确的顺序是 cos() -sin() sin() cos()。您的示例适用于 PI/4 的角度,但对于任何其他角度,矩形变形而不是旋转。
猜你喜欢
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2011-04-04
相关资源
最近更新 更多