#1 2011-07-14 19:56:58

AndersJ
Member
Registered: 2011-07-14
Posts: 6

Best way to make pdf of a multi page textfile

I down loaded the pdf engine and found an example that works fine.

uses SynPdf;
procedure TForm1.DoIt;
var
lPdf   : TPdfDocument;
lPage  : TPdfPage;
begin
  lPdf := TPdfDocument.Create;
  try
    lPdf.Info.Author        := 'Tester';
    lPdf.Info.CreationDate  := Now;
    lPdf.Info.Creator       := 'Tester';
    lPdf.DefaultPaperSize   := psA4;
    lPage := lPDF.AddPage;
    lPDF.Canvas.SetFont('Helvetica',10.0,[]);
    lPDF.Canvas.SetLeading(lPDF.Canvas.Page.FontSize);
    lPDF.Canvas.SetLineWidth(0.1);

    lPdf.Canvas.BeginText;
    lPdf.Canvas.TextOut( 300, 700,  'This is some text_700.');
    lPdf.Canvas.TextOut( 300, 600,  'This is some text_600.');
    lPdf.Canvas.TextOut( 300, 500,  'This is some text_500.');
    lPdf.Canvas.TextOut( 300, 400,  'This is some text_400.');
    lPdf.Canvas.TextOut( 100, 300,  'This is some text_100,300.');
    lPdf.Canvas.EndText;

    lPdf.SaveToFile('c:\temp\test.pdf');
  finally
    lPdf.Free;
  end;
end;

BUT,

How do I add a new page?
Is TextOut the best way to do this?
I basically want a bunch of WriteLn's, a new page, and more WriteLn's.

Thanks for any hint,
Anders J

Offline

#2 2011-07-15 16:18:26

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

Re: Best way to make pdf of a multi page textfile

AndersJ wrote:

1. How do I add a new page?
2. Is TextOut the best way to do this?

1. Use  lPDF.AddPage

2. TextOut() is the fastest way of doing this.
Use a loop, and compute the size of text to do the word wrap and such.

But I'd recommend using TGdiPages for direct writing of text, and export to PDF:

      Rep := TGDIPages.Create(Application);
      try
        Screen.Cursor := crHourGlass;
        Rep.BeginDoc;
        Rep.Font.Size := 10;
        Rep.DrawText(aMemo.Text);
        Rep.EndDoc;
        Rep.Caption := ...
        Rep.ExportPDFAuthor := ...
        Rep.ExportPDFApplication := ...
        Rep.ExportPDFSubject := ...
        Rep.ExportPDFKeywords := ...
        Rep.ExportPDF(FileName,True,false)
      finally
        Screen.Cursor := crDefault;
        Rep.Free;
      end;

This will do all the work for you, and you have an on-screen preview if you want.
You can add headers and footers, pictures, whatever you want....

Online

#3 2011-07-15 17:10:50

AndersJ
Member
Registered: 2011-07-14
Posts: 6

Re: Best way to make pdf of a multi page textfile

I get a runtime error when I use lPDF.Addpage.
Can you provide an example that uses lPDF.AddPage?

Where is TGDIPages defined?

AJ

Offline

#4 2011-07-15 17:47:22

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

Re: Best way to make pdf of a multi page textfile

Search for AddPage in the forum, you'll find sample code.

TGDIPages is defined in SQLIte3Pages.pas

Online

#5 2011-07-17 08:06:48

AndersJ
Member
Registered: 2011-07-14
Posts: 6

Re: Best way to make pdf of a multi page textfile

How do I specify the paper size the pdf is intended to be printed on?
I need A4?

Offline

#6 2011-07-17 16:47:37

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

Re: Best way to make pdf of a multi page textfile

For low-level SynPDF unit, if you search in the documentation, or only in the SynPdf unit source code for A4 you'll find:

  /// available known paper size (psA4 is the default on TPdfDocument creation)
  TPDFPaperSize = (
    psA4, psA5, psA3, psLetter, psLegal, psUserDefined);

  /// the main class of the PDF engine, processing the whole PDF document
  TPdfDocument = class(TObject)
(...)
    /// the default page width, used for new every page creation (i.e. AddPage method call)
    property DefaultPageWidth: cardinal read FDefaultPageWidth write SetDefaultPageWidth;
    /// the default page height, used for new every page creation (i.e. AddPage method call)
    property DefaultPageHeight: cardinal read FDefaultPageHeight write SetDefaultPageHeight;
    /// the default page orientation
    // - a call to this property will swap default page width and height if the
    // orientation is not correct
    property DefaultPageLandscape: boolean read GetDefaultPageLandscape write SetDefaultPageLandscape;
    /// the default page size, used for every new page creation (i.e. AddPage method call)
    // - a write to this property this will reset the default paper orientation
    // to Portrait: you must explicitely set DefaultPageLandscape to true, if needed
    property DefaultPaperSize: TPDFPaperSize read FDefaultPaperSize write SetDefaultPaperSize;

Use those properties to change the page size.

For SQLite3Pages, it just uses the current printer paper size.
So change the paper size for the current printer, just as if you were printing on it from normal Delphi code.

Online

#7 2011-07-19 07:41:40

AndersJ
Member
Registered: 2011-07-14
Posts: 6

Re: Best way to make pdf of a multi page textfile

To print cyrillic characters (russian) I tried setting font.Charset to "RUSSIAN CHARSET" (204)
Output is is however unchanged.

Using
      Pdf.BeginDoc;
      Pdf.DrawText(S);
      Pdf.EndDoc;
Fails

but substituting Pdf.Drawtext(S) with Pdf.Canvas.TextOut(300,100, S) works
appart from page layout, but the russian characters print OK

What is the correct method to print russian characters?

Last edited by AndersJ (2011-07-19 10:47:45)

Offline

#8 2011-07-19 11:14:06

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

Re: Best way to make pdf of a multi page textfile

You have to specify a charset in the TPDFDocument.Create.
And to change the current code page to match the expected one.
DrawText() uses the PDFString type, which is an AnsiString with the current code page settings.

Which Delphi version are you using?

In order to be sure that everything is OK with proper charset handling, you could use the Unicode version of the TPDFCanvas methods, or a VCLCanvas instead. Write just as an usual VCL TCanvas, and it will use the correct charset and font settings when creating the pdf content.

Online

Board footer

Powered by FluxBB