我正在使用以下代码段在矩形内创建文本模式:
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 |
答案 0 :(得分:3)
我不确定我是否理解你的问题,所以我会给你两个答案:
您要查找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();
据我所知,在模式中不止一次添加文本,是实现所需模式类型的唯一方法。