I have a very long block of text (from a text file) that needs to be inserted into a PDF document generated with tADVPDFLib. However, the text remains on a single page and is therefore truncated. Is there a property that allows the text to be split when it reaches the end of a page, or do I need to process the text and break it down into multiple paragraphs manually?
This is possible with the following code (example)
uses
IOUtils;
procedure TForm14.Button1Click(Sender: TObject);
var
Remaining: UnicodeString;
Overflow: Integer;
I: Integer;
R: TRectF;
FileName: string;
begin
// Build sample text directly in memory.
Remaining := '';
for I := 1 to 100 do
Remaining := Remaining +
Format('Paragraph %d. ', [I]) +
'This example demonstrates how to write a long block of text ' +
'to a PDF document using TAdvPDFLib. The text wraps within the ' +
'page margins. When the available space is filled, the remaining ' +
'text continues on a new page. You do not need to calculate ' +
'the page breaks or split the paragraphs manually.' +
sLineBreak + sLineBreak;
FileName := TPath.Combine(TPath.GetDocumentsPath, 'LongText.pdf');
AdvPDFLib1.PageNumber := pnFooter;
AdvPDFLib1.PageNumberFormat := 'Page %d';
AdvPDFLib1.PageNumberAlignment := gtaCenter;
AdvPDFLib1.BeginDocument(FileName);
try
while Remaining <> '' do
begin
AdvPDFLib1.NewPage;
AdvPDFLib1.Graphics.Font.Name := 'Arial';
AdvPDFLib1.Graphics.Font.Size := 11;
AdvPDFLib1.Graphics.Font.Color := gcBlack;
AdvPDFLib1.Graphics.Alignment := gtaLeading;
// Coordinates are in PDF points (72 points = 1 inch).
// Leave extra space at the bottom for the page number.
R := RectF(
40,
40,
AdvPDFLib1.PageWidth - 40,
AdvPDFLib1.PageHeight - 60
);
// Draw into one column and return the number of characters
// that did not fit. False means draw, not just measure.
Overflow := AdvPDFLib1.Graphics.DrawText(
Remaining, R, 1, 0, False);
// Prevent an endless loop if the drawing area is too small.
if Overflow >= Length(Remaining) then
raise Exception.Create('The text area is too small.');
Remaining := Copy(
Remaining,
Length(Remaining) - Overflow + 1,
Overflow
);
end;
finally
AdvPDFLib1.EndDocument(True);
end;
end;