#1 2016-02-02 15:44:03

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Cutting a long text on many pages with margins

Hello,

I need to cut a long text into many A4 pages. Each of my page needs a margin of 0.375 inch (36 pixels) on the top and bottom. I use the GDI ::DrawText() function to draw my text on the VCLCanvas provided by the TPdfDocument class.

I tried to calculate the ideal page height respecting the above mentioned constraints, and to apply a clipping rectangle on my VCL canvas by using the GDI clipping functions. I noticed 2 issues:
1. The text located beyond the clipping rect is effectively not drawn on the PDF page but still seems to exist, e.g. I can select and edit it in Adobe Acrobat. (NOTE I also applied the clipping on the metafile by calling the ::SetMetaRgn() function)
2. There is a big difference between the clipping applied in pixels and the real clipping zone in the PDF file. For example I calculated the bottom clipping by searching how many plain text lines can be fitted inside my page after applying margins (see code below). In theory, the text should not be cropped on the middle of a line, because the clipping height can exactly be divided by the number of lines. However, my PDF file shows the last line of my text cropped in the middle on each page

So I have some questions:
- How can I declare margins correctly for each page on my PDF document, respecting the above mentioned constraints? (NOTE I read the pinned post about margins on this forum, but I could not found a solution for my case)
- Why my clipping rect does not exclude the text completely in my PDF document (i.e. why I find a "ghosted" text outside of my clipping rect)? What is the correct way to apply a clipping region on the PDF VCLCanvas?

Regards

PS: For convenience, here is the code I use to apply my clipping region:
pPdfDoc->DefaultPaperSize = Synpdf::psA4;
pPdfDoc->UseUniscribe = true;
pPdfDoc->AddPage();

...

pPdfDoc->VCLCanvas->Font->Name = L"Arial Unicode MS";
pPdfDoc->VCLCanvas->Font->Size = 12;

...

::TEXTMETRIC textMetric;
::GetTextMetrics(pPdfDoc->VCLCanvas->Handle, &textMetric);

const int pageHeight = pPdfDoc->VCLCanvasSize.Height - (36 * 2); // 36 is calculated as follow: 0.375 inch * 96 dpi
const int lineCount = pageHeight / textMetric.tmHeight;
const int clipHeight = 36 + (lineCount * textMetric.tmHeight);
const TRect clipRect(0, 36, pPdfDoc->VCLCanvasSize.Width, clipHeight);

int savedDC = 0;

try
{
    savedDC = ::SaveDC(pPdfDoc->VCLCanvas);

    HRGN pClipRegion = NULL;

    try
    {
        ::SelectClipRgn(pPdfDoc->VCLCanvas->Handle, NULL);

        pClipRegion = ::CreateRectRgn(clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom);

        if (::SelectClipRgn(pPdfDoc->VCLCanvas, pClipRegion) != ERROR)
            ::SetMetaRgn(pPdfDoc->VCLCanvas);
    }
    __finally
    {
        if (pClipRegion)
            ::DeleteObject(pClipRegion);
    }

    ... my page is drawn here ...

}
__finally
{
    if (savedDC)
        ::RestoreDC(pPdfDoc->VCLCanvas, savedDC);
}

Offline

#2 2016-02-03 07:22:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Cutting a long text on many pages with margins

1. There is no such knowledge of "margin" neither at PDF nor TCanvas level.
The concept of "margin" is purely depending on the drawing code: you may even write within margins.
The mORMotReport.pas unit provides high-level code-based reporting class, with simple paragraph text processing, including margins.

2. Clipping should work in latest versions of the framework trunk.
Current is 1.18.2344.

Offline

#3 2016-02-05 13:48:22

jeanmilost
Member
Registered: 2016-01-21
Posts: 35

Re: Cutting a long text on many pages with margins

Thank you for your response.

As I know, I use the latest version, however it's difficult to be sure, because the source code shows only 1.18 as version number. For security I will download again from the site and retry.

For the margins, I resolved the question by creating my own margins system. I use the clipping to apply them, and I modified my functions to be able to cut the text itself for each pages.

Regards

Offline

Board footer

Powered by FluxBB