有人在c++上使用jagPDF吗?
我正试着从http://www.jagpdf.org/doc/index.htm提供的手册中学习如何使用它。不幸的是,他们所有的例子都是用Python编写的。有时候适应c++并不是什么大问题,但有时候却令人困惑。
例如,我试图学习如何对齐文本,这在教程中是在文本对齐。找到padding后,将padding放入文本行的函数是:
canvas.text(txt, [padding / 2.0], [0])
查找该函数的引用,翻译表为:
[py] text(txt_u, offsets, positions)
[c++] void text(Char const* txt_u, Double const* offsets, UInt offsets_length, Int const* positions, UInt positions_length);
参数:
- txt_u:零终止字符串。
- offsets:以字形空间表示的字形偏移量(即以文本空间单位的千分之一表示)。
- offsets_length:偏移量的数目。
- position:将txt_u中的字形偏移与字形索引相关联。
- positions_length:位置数。
我已经尝试了一些东西,但我还没有弄清楚c++在Python上需要的两个额外输入参数。如果有人使用过jagPDF并知道如何在c++中实现它,我将不胜感激。
这是我第一次接触JagPDF。有一节文本对齐,但代码是用Python编写的。
在c++中应该是这样:
// line width
pdf::Double line_width = 597.6;
// text to show
pdf::Char *txt = "Everything should be made as simple as possible, but no simpler.";
// calculate text width
pdf::Double text_width = font.advance(txt);
// calculate gap - difference in line_width and text_width
pdf::Double gap = line_width - text_width;
// calculate the text padding
pdf::Double padding = -1000.0 / font.size() * gap;
// create array with paddings - I set all padings, but is used only 1
pdf::Double const pp[] = { padding, padding / 2.0 }; // index 0 for right alignment, 1 for center
// not sure what are positions, but need for function `text()` - set as Doc to zero (0)
pdf::Int const dd[] = { 0};
// get canvas where the text is written
pdf::Canvas canvas = doc.page().canvas();
// start text block
canvas.text_start(0, 480);
// set font
canvas.text_font(font);
// right alignment text
canvas.text(txt, pp, 1, dd, 1);
canvas.text_translate_line(0, font.height());
// center alignment text
canvas.text(txt, &pp[1], 1, dd, 1);
canvas.text_translate_line(0, font.height());
// finish text block
canvas.text_end();
希望有所帮助