将格式化文本导出为pdf

时间:2017-08-31 21:03:42

标签: delphi pdf

我有一个带有格式化文本的TRichEditView,如下所示:

**Header**

**paragraph 1 title**
__________________
paragraph 1 line 1
paragraph 1 line 2
paragraph 1 line 3

**paragraph 2 title**
__________________
paragraph 2 line 1
paragraph 2 line 2
paragraph 2 line 3

我想将文本导出为PDF文件。

目前,该项目正在使用PDFLibrary在PDF文件上绘制每个页面,但如果行数约为9000,则需要很长时间才能导出,例如15分钟。

我正在逐行创建PDF文件,但我想知道是否有更快的方式将格式化文本转换为PDF?

这是我要导出的代码的一部分:

var
  pdfLibrary: TDebenuPDFLibrary;
begin
  ...
  pdfLibrary.SetInformation(5, '---'); // Creator
  pdfLibrary.SetMeasurementUnits(2);           // Inches
  pdfLibrary.SetOrigin(1);                     // Top left
  pdfLibrary.CompressImages(1);                // Flate compression
  pdfLibrary.CompressFonts(1);                 // Compress all subsequently added fonts
  pdfLibrary.SetPageSize('A4');

  // Set current page size. This will be inherited for new pages
  pdfLibrary.SetPageDimensions(pageSetup.Size.Width, pageSetup.Size.Height);

  marginRect.Create(0, 0,
    trunc(pageSetup.Size.Width * TResolutionHelper.STANDARD_WINDOWS_DPI),
    trunc(pageSetup.Size.Height * TResolutionHelper.STANDARD_WINDOWS_DPI));

  cntr := 0;
  fontID := pdfLibrary.AddStandardFont(5);
  while cntr < 1 do //for now just do for first line
  begin
    if cntr > 1 then
      if pdfLibrary.NewPage = 0 then
        raise Exception.Create(format('%d page cannot be added', [cntr]));

      //pdfLibrary.SelectFont( fontID );
      pdfLibrary.SetTextSize( RVStyle1.TextStyles[1].SizeDouble );
      pdfLibrary.SelectPage(0);

      x := (pdfLibrary.PageWidth / 2) - pdfLibrary.GetTextWidth( tseHeader.GetItemText(0) );
      pdfLibrary.DrawText( x, 0.5, tseHeader.GetItemText(0) );

      inc(cntr);
    end;
  ...
end;

现在,SetTextAlign()没有对齐,这需要很长时间才能完成。我正在寻找更有意义的东西。

1 个答案:

答案 0 :(得分:0)

这有点解决我的问题

procedure TfrmDemo.Button1Click(Sender: TObject);
var
  I: Integer;
  QP: TDebenuPDFLibrary;
  X: Integer;
  footer: string;
  footerHeight: double;
  textHeight: double;
begin
  dlgSave.FileName := 'Rotated.pdf';
  if dlgSave.Execute then
  begin
    QP := TDebenuPDFLibrary.Create;
    try
      if QP.UnlockKey(edtLicenseKey.Text) = 1 then
      begin
        footer := 'footer is here!';
        QP.AddStandardFont(8);
        footerHeight := QP.GetTextHeight;

        // Set the paper size
        QP.SetPageSize('A4');

        // Set the origin to the top-left corner
        QP.SetOrigin(1);

        // Set the measurement units to millimetres
        QP.SetMeasurementUnits(1);

        // Add a standard font
        QP.AddStandardFont(11);  // Times Roman Bold Italic

        QP.SetTextSize(10);

        QP.SetTextAlign( 1 );

        I := 0;
        textHeight := QP.GetTextHeight;
        while 10 + (textHeight * I) < qp.PageHeight - footerHeight - textHeight do
        begin
          QP.DrawText( (qp.PageWidth / 2)  , 10 + (textHeight * I),
          '1 Quick PDF Library, Quick PDF Library, Quick PDF Library, Quick PDF Library, Quick PDF Library');
          inc(I);
        end;

        qp.DrawLine( 10, 10 + (textHeight * I), qp.PageWidth - 10, 10 + (textHeight * I) );
        inc(I);

        QP.AddStandardFont(8);
        QP.SetTextSize(8);
        QP.DrawText( (qp.PageWidth / 2)  , 10 + (textHeight * I), footer);

        // Compress the contents of the file
        QP.CompressContent;

        // Save the file
        QP.SaveToFile(dlgSave.FileName);
      end else
        MessageDlg('The license key is invalid or has expired.', mtError, [mbOK], 0);
    finally
      QP.Free;
    end;
  end;
end;