eDocEngine VCL
Create documents and reports in 18 formats
Compatibility
Delphi C++Builder

Insert attachments to PDF when exporting reports using eDocEngine

Learn how to insert attachments, add additional pages and other content to the generated PDF when exporting reports using eDocEngine.

eDocEngine VCL support inserting attachments, adding additional pages and other content to the generated PDF when exporting reports. One of the many useful features of PDF, among others, is its ability to carry attached files, just as e-mail messages can carry attached files. Any kind of file and any number of files can be pulled into a PDF file. File attachment feature is used for attaching ZUGFeRD electronic invoice. This feature can also be used to attach original editable versions of documents along with the PDF.

When exporting reports programmatically, we use the RenderDocument method. RenderDocument exports contents from a specified report file to the specified format. This method internally calls the BeginDoc and EndDoc methods of the specified engine to start and end the document creation process. So, any calls to content insertion methods on the Engine before or after the call to RenderDocument are ignored by the Engine.

There are two techniques to work with this behaviour of the RenderDocument method and insert additional content, such as file attachments, into the PDF being exported from reports using the RenderDocument method.

Technique 1: By handling the OnBeforeEndDocevent on the PDFEngine

Technique 2: Setting one or both of DoBeginDoc/DoEndDoc properties to False on the Export Interface, then calling BeginDoc/EndDoc on the linked engine object, depending on which property we have turned off, and placing the RenderDocument and additional code on the engine in between the BeginDoc/EndDoc calls.

Below code snippets show how you can attach a file to PDF when exporting reports.

Scenario 1: Add attachment when exporting prepared FastReport reports to PDF by handling OnBeforeEndDoc event

{ 
Add the following components to a form.
  1. TgtFRExportInterface
  2. TgtPDFEngine
  3. TfrxReport
}
procedure TForm1.Button1Click(Sender: TObject);
begin
  // Load report
  frxReport1.PreviewPages.LoadFromFile('FastReport.fp3');
  // Set output file name for the PDF engine
  gtPDFEngine1.FileName := 'Test1';
  // Connect report export interface to PDF engine
  gtFRExportInterface1.Engine := gtPDFEngine1;
  // Export report to PDF
  gtFRExportInterface1.RenderDocument(frxReport1, True, False);
End

// Handle event OnBeforeEndDoc to add attachment
procedure TForm1.gtPDFEngine1BeforeEndDoc(Sender: TgtCustomDocumentEngine);
begin
  gtPDFEngine1.AttachFile('D:\Work\1.bmp', '1.bmp');
  gtPDFEngine1.AttachFile('D:\Work\2.jpg', '2.jpg');
end;

Scenario 2: Adding attachment when exporting FastReport reports to PDF by turning off DoBeginDoc and DoEndDoc on the FR Export interface

{ 
Add the following components to a form.
  1. TgtFRExportInterface
  2. TgtPDFEngine
  3. TfrxReport
}
procedure TForm1.Button2Click(Sender: TObject);
begin
  // Load report
  frxReport1.LoadFromFile('FastReportDemo.fr3');
  // Set output file name for the PDF engine
  gtPDFEngine1.FileName := 'Test2';
  // Connect report export interface to PDF engine
  gtFRExportInterface1.Engine := gtPDFEngine1;
  // Turn off DoBeginDoc and DoEndDoc
  gtFRExportInterface1.DoBeginDoc := False;
  gtFRExportInterface1.DoEndDoc := False;
  // Call BeginDoc
  gtPDFEngine1.BeginDoc;
  // Export report to PDF
  gtFRExportInterface1.RenderDocument(frxReport1, False, False);
  // Attach the file
  gtPDFEngine1.AttachFile('D:\Work\ZUGFeRD-invoice.xml', 'ZUGFeRD-invoice.xml');
  // Call EndDoc
  gtPDFEngine1.EndDoc;
end;

Scenario 3: Adding New page when exporting QuickReport reports to PDF by handling OnAfterBeginDoc event.

{ 
Add the following components to a form.
  1. TgtQRExportInterface
  2. TgtPDFEngine
  3. TQRPQuickrep
}
procedure TForm2.Button1Click(Sender: TObject);
begin
  // Set output file name for the PDF engine
  gtPDFEngine1.FileName := 'eDoc_Quickreport_Demo.pdf';
  // Connect report export interface to PDF engine
  gtQRExportInterface1.Engine := gtPDFEngine1;
  // Export Report to PDF
  gtQRExportInterface1.RenderDocument(QRPQuickrep1, False);
end;
// Handle OnAfterBeginDocEvent to add new page at beginning
procedure TForm2.gtPDFEngine1AfterBeginDoc(Sender: TgtCustomDocumentEngine);
begin
  gtPDFEngine1.NewPage;
end;

Scenario 4: Adding new page while exporting QuickReport to PDF by turning off DoBeginDoc and DoEndDoc of QuickReport Export interface.

{ 
Add the following components to a form.
  1. TgtQRExportInterface
  2. TgtPDFEngine
 }
procedure TForm2.Button2Click(Sender: TObject);
begin
  // Set output file name  for the PDF Engine
  gtPDFEngine1.FileName := 'QuickReport_Demo';
  // Connect report export interface to PDF engine
  gtQRExportInterface1.Engine := gtPDFEngine1;
  // Turn off DoBeginDoc and DoEndDoc
  gtQRExportInterface1.DoBeginDoc := False;
  gtQRExportInterface1.DoEndDoc := False;
  // Call BeginDoc
  gtPDFEngine1.BeginDoc;
  // Add new page
  gtPDFEngine1.NewPage;
  // Export the report to PDF
  gtQRExportInterface1.RenderDocument('QuickReport.QRP');
  // Add new page
  gtPDFEngine1.NewPage;
  // Call EndDoc
  gtPDFEngine1.EndDoc;
end;

Scenario 5: Adding Image Watermark when exporting ReportBuilder reports to PDF by handling OnAfterBeginDoc event.

{
 Add the following components to a form.
  1. TgtRBExportInterface
  2. TgtPDFEngine
  3. TppReport
}
procedure TForm1.Button1Click(Sender: TObject);
begin
  // Connect report export interface to PDF engine
  gtRBExportInterface1.Engine := gtPDFEngine1;
  // Set output file name for the PDF engine
  gtPDFEngine1.FileName := 'eDoc_RBDemo';
  // Load Report
  ppReport1.Template.FileName := 'ReportSample.rtm';
  ppReport1.AllowPrintToFile := True;
  ppReport1.PrintToDevices;
  // Export generated  report to PDF
  gtRBExportInterface1.RenderDocument(ppReport1);
end;
// Handle OnAfterBeginDocEvent to add Image Watermark
procedure TForm1.gtPDFEngine1AfterBeginDoc(Sender: TgtCustomDocumentEngine);
var
  JPGimage: TJPEGImage;
begin
  JPGimage := TJPEGImage.Create;
  // Load Image
  JPGimage.LoadFromFile('Gnostice.jpg ');
  with gtPDFEngine1 do
  begin
    // Add Image Watermark
    BeginWaterMark;
    ImageSettings.Stretch := True;
    // for Report export, measurement unit is set to pixels internally so rect 
    // should be given in pixels
    DrawImage(gtRect(0, 0, 200, 100), JPGimage);
    EndWaterMark;
  end;
end;

Scenario 6: Adding Text watermark when exporting ReportBuilder reports to PDF by turning off DoBeginDoc and DoEndDoc of RB Export interface.

{
 Add the following components to a form.
  1. TgtRBExportInterface
  2. TgtPDFEngine
  3. TppArchiveReader
}
procedure TForm1.Button2Click(Sender: TObject);
begin
  // Set output file name for the PDF engine
  gtPDFEngine1.FileName := 'sample_rb_export.pdf';
  // Connect report export interface to PDF engine
  gtRBExportInterface1.Engine := gtPDFEngine1;
  // Load report
  ppArchiveReader1.ArchiveFileName := 'ReportBuilder.raf';
  ppArchiveReader1.PrintToDevices;
  // Turn off DoBeginDoc and DoEndDoc
  gtRBExportInterface1.DoBeginDoc := False;
  gtRBExportInterface1.DoEndDoc := False;
  // Call BeginDoc
  gtPDFEngine1.BeginDoc;
  // Export generated report to PDF
  gtRBExportInterface1.RenderDocument(TppReport(ppArchiveReader1));
  // Add Text Watermark
  gtPDFEngine1.BeginWaterMark;
  gtPDFEngine1.Font.Size := 20;
  gtPDFEngine1.Font.Color := clRed;
  gtPDFEngine1.TextOut(7, 1, 'Gnostice information Technologies');
  gtPDFEngine1.EndWaterMark;
  // Call EndDoc
  gtPDFEngine1.EndDoc;
  ppArchiveReader1.Reset;
end;

Using the techniques demonstrated in this article, you can add attachments, additional pages and other content to the output being generated as a report export operation. These techniques can be used with all of the supported reporting tools.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2024 Gnostice Information Technologies Private Limited. All rights reserved.