gpt4 book ai didi

c++ - 在 libharu 中将长字符串转换为多行字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:26 25 4
gpt4 key购买 nike

我有下面的行,而且行很长,想转换成多行。截至目前,文本超出了我的 pdf 范围。

int show_data(){

YPOS -= 10;
NextPage();
double ypos = YPOS;
double maxWidth = HPDF_Page_GetWidth(currentpage) - (m_Margin * 2);
double indentStart = 2 * m_Margin;
double xpos = indentStart;
bool thereIsText = (text.length() > 0);
int textPos = 0;
string newText;
while (thereIsText) {
// maxWidth-xpos is the max length of text allowed per line;
newText = text.substr(textPos);
HPDF_TextWidth spacewidth =
HPDF_Font_TextWidth(font, (const HPDF_BYTE *) " ", 1);

HPDF_TextWidth tw =
HPDF_Font_TextWidth(font, (const HPDF_BYTE *) newText.c_str(),
newText.length());



textPos += (newText.length() + 1);
HPDF_Page_BeginText(currentpage);
loginfo<<xpos<<ypos<<endl;
HPDF_Page_SetFontAndSize (currentpage, font, 24);
HPDF_Page_SetLineWidth(currentpage, 80);
HPDF_Page_MoveTextPos(currentpage,-220, ypos);
//HPDF_Page_MoveToNextLine
//HPDF_Page_TextOut(currentpage,xpos,ypos, newText.c_str());
//HPDF_Page_MoveTo(currentpage, 120, 195);
//HPDF_Page_MoveTextPos (currentpage, 20, 20);
HPDF_Page_ShowTextNextLine(currentpage, newText.c_str());
HPDF_Page_EndText(currentpage);

if ((unsigned int) textPos >= text.length())
thereIsText = false;

ypos -= 10;
loginfo<<" ypos ... "<<ypos<<endl;
loginfo<< " m_Margin.... "<<m_Margin<<endl;
/*if (ypos <= m_Margin) {
NextPage();
ypos = HPDF_Page_GetHeight(currentpage) - m_Margin;
}*/
xpos = m_Margin;
}
YPOS = ypos;
}

最佳答案

好吧,假设我对 OP 的评论/问题几乎是相关的,我理解这个问题是如何在不超过打印边界的情况下将很长的行插入到 libharu 生成的 PDF 文档中。

Libharu 支持文本框 (HPDF_Page_TextRect()),它为写入的文本创建边界。它在完整的单词(即没有插入“...”或“-”)上自动换行(在外行人对该术语的定义中)并且做得很好。您需要做的就是定义要使用的 HPDF_Rect 的大小。当然,这意味着您最终可能会将框的尺寸设置得太小而无法容纳所有文本,因此您在设置时仍然必须相当小心。

在 Libharu 站点 ( https://github.com/libharu/libharu/wiki/API%3A-Graphics#wiki-HPDF_Page_TextRect ) 中,您可以找到 HPDF_Page_TextRect 的格式,但基本上您可以为其指定尺寸、要显示的文本和一些格式/行为参数。

动物451

您现在可能已经自己回答了这个问题。

关于c++ - 在 libharu 中将长字符串转换为多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20062784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com