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

标签 c++ c pdf-generation libharu

我有下面的行,而且行很长,想转换成多行。 截至目前,文本超出了我的 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/

相关文章:

c++ - 内存分配c++

c++ - 可以在构造函数中创建 Pthreads 吗?

c++ - 将基类转换为派生类还是在基类上创建虚函数更好?

c - Bignum 库,慢素数生成器

c - 类型检查和代码模式检测,Frama-C

google-chrome - 使用 Chrome pdfium 将 HTML 转换为 PDF

c++ - Hana BOOST_HANA_DEFINE_STRUCT 不适用于 std::unique_ptr

在 do while 中继续计算,即使不存在任何列

c# - 在显示给用户后立即删除动态生成的 PDF 文件

java - 使用来自 html 的 Flying Saucer 渲染 PDF 中的嵌入图像