Print Preview and Synopse PDF library

Please post bug reports, feature requests, or any question regarding the DELPHI AREA products here.

Print Preview and Synopse PDF library

Postby Gabberkooij » July 20th, 2010, 8:23 pm

Hi,

I've been looking if i could create a export to pdf without the need for a external dll and i 've found SynPDf. With the change in the following methods (and the addition of SynPDF to the uses) you can create PDF's with it:

Code: Select all
procedure TPrintPreview.SaveAsPDF(const FileName: String);

  function PaperTypeToPDFPaperSize(PaperType: TPaperType): TPDFPaperSize;
  begin
    case PaperType of
      pLetter     : Result := psLetter;
      pLegal      : Result := psLegal;
      pA3         : Result := psA3;
      pA4         : Result := psA4;
      pA5         : Result := psA5;
    else
      Result := psUserDefined; // Default to custom
    end;
  end;

var
  PaegNo: Integer;
  pdf: TPdfDocument;
begin
  pdf := TPdfDocument.Create;
  try
    ChangeState(psSavingPDF);
    try
      if FileExists(FileName) and not IsFileWritable(FileName) then begin
        raise Exception.Create(Format(_('Unable to write to file %s. Is the file opened by another application?'), [FileName]));
      end;

      pdf.NewDoc;
      try
        pdf.Info.Author := FPDFDocumentInfo.Author;
        pdf.Info.CreationDate := Date;
        pdf.Info.Creator := FPDFDocumentInfo.Creator;
        //pdf.Info.Keywords
        //pdf.Info.ModDate
        pdf.Info.Subject := FPDFDocumentInfo.Subject;
        pdf.Info.Title := FPDFDocumentInfo.Title;
        //FPDFDocumentInfo.Producer

        pdf.DefaultPaperSize := PaperTypeToPDFPaperSize( PaperType );
//        if pdf.DefaultPaperSize = psUserDefined then begin
          pdf.DefaultPageWidth := ConvertX(PaperWidth, Units, mmPoints);
          pdf.DefaultPageHeight := ConvertY(PaperHeight, Units, mmPoints);
//        end;

        for PaegNo := 1 to TotalPages do begin
          pdf.AddPage;
          pdf.Canvas.RenderMetaFile(Pages[PaegNo]);
        end;
      finally
        pdf.SaveToFile(FileName);
      end;
    finally
      ChangeState(psReady);
    end;
  finally
    pdf.Free;
  end;
end;

function TPrintPreview.CanSaveAsPDF: Boolean;
begin
  Result := true;
end;



You can find the Synopse PDF libray at: http://www.synopse.info
User avatar
Gabberkooij
Active Member
Active Member
 
Posts: 7
Joined: May 17th, 2010, 7:28 pm
Location: Netherlands

Re: Print Preview and Synopse PDF library

Postby Gabberkooij » July 21st, 2010, 6:51 pm

I've commented the "if pdf.DefaultPaperSize = psUserDefined then begin" line above to fix Orientation trouble...
User avatar
Gabberkooij
Active Member
Active Member
 
Posts: 7
Joined: May 17th, 2010, 7:28 pm
Location: Netherlands

Re: Print Preview and Synopse PDF library

Postby Kambiz » July 30th, 2010, 4:56 am

This is interesting, Thank you!

I included Synopse PDF library in PrintPreview (not released yet). But, seems it doesn't work as expected. I tried to create the PDF file using the pages of general demo. The first two pages are rendered fine, but the content of the last page (the rich edit page) appeared as a thumbnail on top left corner of the PDF page.

I don't know whether I should release the update or not!

Here is my code:

Code: Select all
procedure TPrintPreview.SaveAsPDF(const FileName: String);
var
  PageNo: Integer;
  pdf: TPdfDocument;
begin
  pdf := TPdfDocument.Create;
  try
    ChangeState(psSavingPDF);
    try
      pdf.Info.CreationDate := Now;
      pdf.Info.Creator := FPDFDocumentInfo.Creator;
      pdf.Info.Author := FPDFDocumentInfo.Author;
      pdf.Info.Subject := FPDFDocumentInfo.Subject;
      pdf.Info.Title := FPDFDocumentInfo.Title;
      pdf.DefaultPageWidth := ConvertX(PaperWidth, Units, mmPoints);
      pdf.DefaultPageHeight := ConvertY(PaperHeight, Units, mmPoints);
      pdf.NewDoc;
      for PageNo := 1 to TotalPages do
      begin
        pdf.AddPage;
        pdf.Canvas.RenderMetaFile(Pages[PageNo]);
      end;
      pdf.SaveToFile(FileName);
    finally
      ChangeState(psReady);
    end;
  finally
    pdf.Free;
  end;
end;

By the way, there is no need to set the DefaultPaperSize property of TPdfDocument. It just sets DefaultPageWidth and DefaultPageHeight with pre-calculated values.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Print Preview and Synopse PDF library

Postby Kambiz » July 30th, 2010, 9:27 pm

Here is what I've found about the bug:

To render a rich text, mapping mode should be MM_TEXT. However, Synopse library does not process the EMR_SETMAPMODE records.

Actually, the Synopse library processes only a few of meta record types.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm

Re: Print Preview and Synopse PDF library

Postby Gabberkooij » August 1st, 2010, 8:02 pm

I saw a update for his own preview component that added support for rtf, maybe that change can help?
User avatar
Gabberkooij
Active Member
Active Member
 
Posts: 7
Joined: May 17th, 2010, 7:28 pm
Location: Netherlands

Re: Print Preview and Synopse PDF library

Postby Kambiz » August 3rd, 2010, 8:03 pm

There is no update PDF engine, and support for mapping mode is still missing.

In Synopse preview, user does not have access to preview canvas and cannot draw freely on the preview page. The user can call only Synopse preview methods and uses only millimeters as unit. Therefore, the preview canvas always keeps the default mapping mode, which is MM_TEXT.

This is why Synopse preview can render rich text, and PrintPreview component cannot.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2408
Joined: March 7th, 2003, 7:10 pm


Return to DELPHI AREA Products

Who is online

Users browsing this forum: No registered users and 1 guest

cron