Skip to main content
 首页 » 编程设计

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

2024年02月06日9cyq1162

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

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

我们正在使用平铺模式,您遇到的问题是所有平铺都是规则的。如果你想要一个不规则的模式,你需要使用一个变通方法。 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(); 

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