Page 2 of 2

Creating pdf files with ScaledRichView and wPdf

Posted: Tue Oct 14, 2014 5:33 pm
by Jim Knopf
I struggled throuh this bit of code for many hours. The documentation is pretty poor or perhaps the writer is only thinking a little different then me.
One day we compared notes and so I don't want to keep the solution back for somebody with similar problems.
I didn't delete some surrounding code like support of scrollbar and progress notification

Code: Select all

procedure PrintPDF;
var Units: TRVUnits;
    Res: Integer;
    I: Integer;
    PageF, PageT: Integer;
begin
  Units := SRichView.UnitsProgram;
  Res := Screen.PixelsPerInch;

  WPDF_Start('UserName', 'ABCUserCodeXYZ');

  // document information
  wPdf.Info.Author := BN.Strings[tcExAuth.Index];
  wPdf.Info.Title := BN.Strings[tcExTit.Index];
  wPdf.Info.Subject := BN.Strings[tcExType.Index];
  wPdf.Info.Keywords := BN.Strings[tcExTags.Index];

  // some output modes
  if rbPDFFontEmbed.Checked or (rgPMode.ItemIndex = 3) then
    wPdf.FontMode := wpEmbedTrueTypeFonts
  else
    wPdf.FontMode := wpUseTrueTypeFonts;

  if rbPDFFontEmbedCid.Checked then
    wPdf.CidFontMode := wpCIDUnicode
  else
    wPdf.CidFontMode := wpCIDOff;

  if rbPDFCompress.Checked then
    wPdf.CompressStreamMethod := wpCompressFastFlate
  else
    wPdf.CompressStreamMethod := wpCompressNone;

  SRichView.UnitsProgram := rvuPixels;

  // select pages
  PageF := 1;
  PageT := SRichView.PageCount;
  if not sbPrintRange.Down then
  begin
    PageF := iePageF.Value;
    PageT := iePageT.Value;
  end;

  // progressbar
  pb2.Max := PageT;
  pb2.Position := PageF-1;
  paPDF.Visible := True;
  try
    // output process
    wPdf.FileName := sd6.FileName;
    wPdf.BeginDoc;
    try
      for I := PageF to PageT do
      begin
        // next three lines are the secret
        wPdf.StartPage(Round(SRichView.PageProperty.PageWidth), Round(SRichView.PageProperty.PageHeight), Res, Res, 0);
        SRichView.DrawPage(I, Round(SRichView.PageProperty.PageWidth), Round(SRichView.PageProperty.PageHeight), 0, 0, wPdf.Canvas, False, False, False);
        wPdf.EndPage;
        pb2.Position := I;
        laPDF.Caption := Format('pdf output page %d to %d', [I, PageT]);
        Application.ProcessMessages;
      end;
    finally
      wPdf.EndDoc;
    end;
  finally
    SRichView.UnitsProgram := Units;
    paPDF.Visible := False;
  end;
end;