<< Click to Display Table of Contents >> Navigation: Programming > Printing and PDF Export > Add PDF Export > Export using PaintRTFPage() |
Alternatively this simple code can be used to export PDF files from WPTools Version 9.
The drawing code used by WPTools Version 9 will makes sure that the hyperlinks and bookmarks are exported to PDF as they are with the 'old' WPPDFExport component. Please note that the component WPRichText1 is a TWPRichText component created on the form with the property Visible set to FALSE. All other properties have not been changed.
// uses WPRTEPaint, WPPDFR1, WPPDFR2;
procedure TForm1.ExportToPDF(Sender: TObject);
var WPPDFPrinter1: TWPPDFPrinter;
i,w,h : Integer;
begin
WPPDFPrinter1 := TWPPDFPrinter.Create(nil);
WPPDFPrinter1.FileName :='c:\wptools5demo.pdf';
WPPDFPrinter1.CompressStreamMethod := wpCompressFastFlate;
WPPDFPrinter1.AutoLaunch := TRUE;
WPPDFPrinter1.BeginDoc;
try
i := 0;
while i<WPRichText1.CountPages do
begin
w := MulDiv(WPRichText1.Memo._PaintPages[i].WidthTw,WPScreenPixelsPerInch,1440);
h := MulDiv(WPRichText1.Memo._PaintPages[i].HeightTw,WPScreenPixelsPerInch,1440);
if (w=0) or (h=0) then
begin
w := Round( WPRichText1.Memo.PaintPageWidth[i] / WPRichText1.Memo.CurrentZooming );
h := Round( WPRichText1.Memo.PaintPageHeight[i] / WPRichText1.Memo.CurrentZooming );
end;
WPPDFPrinter1.StartPage( w, h, Screen.PixelsPerInch, Screen.PixelsPerInch, 0);
try
// Use 0 as w and h to let the function calculate the width and height
WPRichText1.Memo.PaintRTFPage(i,0,0,0,0,WPPDFPrinter1.Canvas, [wppInPaintForwPDF] );
finally
WPPDFPrinter1.EndPage;
end;
inc(i);
end;
finally
WPPDFPrinter1.EndDoc;
WPPDFPrinter1.Free;
end;
end;
To create watermarks simply add additional code which prints on the WPPDFPrinter1.Canvas.
Or you can easily print 2 pages on the same PDF page, just make changes in 4 lines:
var WPPDFPrinter1: TWPPDFPrinter;
i, w, h : Integer;
begin
WPPDFPrinter1 := TWPPDFPrinter.Create(nil);
WPPDFPrinter1.FileName :='c:\wptools5demo.pdf';
WPPDFPrinter1.CompressStreamMethod := wpCompressFastFlate;
WPPDFPrinter1.AutoLaunch := TRUE;
WPPDFPrinter1.BeginDoc;
try
i := 0;
while i<WPRichText1.CountPages do
begin
h := WPRichText1.Memo.PaintPageHeight[i];
w := WPRichText1.Memo.PaintPageWidth[i];
WPPDFPrinter1.StartPage(w, h div 2,
Screen.PixelsPerInch, Screen.PixelsPerInch, 0);
try
// Use 0 as w and h to let the function calculate the width and height
WPRichText1.Memo.PaintRTFPage(i,0,0,w div 2,h div 2,WPPDFPrinter1.Canvas, [] );
WPRichText1.Memo.PaintRTFPage(i+1,w div 2,0,w div 2,h div 2,WPPDFPrinter1.Canvas, [] );
finally
WPPDFPrinter1.EndPage;
end;
inc(i,2);
end;
finally
WPPDFPrinter1.EndDoc;
WPPDFPrinter1.Free;
end;
end;