【问题标题】:Convert Long String to multiline string in libharu在 libharu 中将长字符串转换为多行字符串
【发布时间】:2013-11-19 03:47:33
【问题描述】:

我有一条很长的线路,想转换成多条线路。 截至目前,该文本超出了我的 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;
}

【问题讨论】:

  • 这个问题是关于如何在不破坏打印边界的情况下向 libharu 生成的 pdf 文件显示/打印长行,而不是让 c/c++ 将长行分成几个小行吗?

标签: c++ c pdf-generation libharu


【解决方案1】:

好吧,假设我对 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

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

【讨论】:

    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2014-06-15
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多