c# - 如何在每个 x 步向文本模式添加 x 偏移量? (itext)

标签 c# itext

我正在使用以下代码片段在矩形内创建文本模式:

var canvas = m_writer.DirectContent;

float fillTextSize = 6.0f;
string filltext = "this is the fill text!";
float filltextWidth = m_dejavuMonoBold.GetWidthPoint(filltext, fillTextSize);

PdfPatternPainter pattern = canvas.CreatePattern(px, py, filltextWidth, fillTextSize);
pattern.BeginText();
pattern.SetTextMatrix(0, 0);
pattern.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
pattern.SetRGBColorStroke(190, 190, 190);
pattern.SetRGBColorFill(190, 190, 190);
pattern.SetFontAndSize(m_dejavuMonoBold, 6.0f);
pattern.ShowText(filltext);
pattern.EndText();

canvas.Rectangle(px, py, pw, ph);
canvas.SetPatternFill(pattern);
canvas.Fill();

填充文本有规律地重复。我想添加图案的每条打印线以添加水平偏移。有没有办法增加模式的 xStep 参数?

我正在寻找这样的模式:

|this is the fill text! this is the fill text! this|
|his is the fill text! this is the fill text! this |
|is is the fill text! this is the fill text! this i|
|s is the fill text! this is the fill text! this is|
| is the fill text! this is the fill text! this is |

最佳答案

我不确定我是否理解你的问题,所以我会给你两个答案:

要么您正在寻找 PdfPatternPainter 中的 setXStep()setYStep() 方法.这些方法可用于设置图案的水平和垂直间隔。结果将是一个规则的模式。

或者您正在寻找更改形状内的间隔。例如:使用相同的模式增加间隔,导致不规则模式。这在 PDF 中是不可能的。

更新:

根据您的评论和问题的更新,我现在明白这就是您所需要的:text_pattern.pdf

enter image description here

我们正在使用平铺模式,您遇到的问题是所有平铺都是规则的。如果你想要一个不规则的模式,你需要使用一个变通方法。 TextPattern 中显示了此类解决方法示例。

在这个例子中,我创建了这样的模式:

// This corresponds with what you have
PdfContentByte canvas = writer.getDirectContent();
BaseFont bf = BaseFont.createFont();
String filltext = "this is the fill text! ";
float filltextWidth = bf.getWidthPoint(filltext, 6);
// I create a bigger "tile"
PdfPatternPainter pattern = canvas.createPattern(filltextWidth, 60, filltextWidth, 60);
pattern.beginText();
pattern.setFontAndSize(bf, 6.0f);
// I start with an X offset of 0
float x = 0;
// I add 6 rows of text
// The font is 6, so I used a leading of 10:
for (float y = 0; y < 60; y += 10) {
    // I add the same text twice to each row
    // 1.
    pattern.setTextMatrix(x - filltextWidth, y);
    pattern.showText(filltext);
    // 2.
    pattern.setTextMatrix(x, y);
    pattern.showText(filltext);
    // I change the X offset to 1/6th of the text width
    x += (filltextWidth / 6);
}
pattern.endText();

据我所知,将文本多次添加到模式中是获得所需模式类型的唯一方法。

关于c# - 如何在每个 x 步向文本模式添加 x 偏移量? (itext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262460/

相关文章:

c# - 如何确定 Azure StorageV2(具有分层命名空间的数据湖存储)BlobItem 是否为虚拟目录

java - java 中 AcroFields 的下划线(iTextSharp)

pdf - 使用 iTextSharp 删除 PDF 中的对象并保存

c# - 在 .NET WebRequest 中处理重定向

c# - 如何在共享主机环境中运行 RavenDB?

c# - 使用 itextsharp 在现有表格/图像上写入文本

java - 使用 IText 库进行 PDF 签名

pdf - 如何为时间戳签名启用 LTV?

c# - 添加导航 :Frame causes NullReferenceException

c# - 无法首先从 EF 代码打开生成的数据库