1. Home
  2. Foxit Quick PDF Library
  3. Programmatically impose multiple pages on one page (imposition)

Programmatically impose multiple pages on one page (imposition)

There is not a single “imposition” function in Foxit Quick PDF Library but it’s easy to use the CapturePage and DrawCapturedPage functions to do the imposition. There is some sample code below that demonstrates how to take a few different PDFs, merge them together, and then impose each page onto one page in a new document. The sample is written in Delphi, but should be easy enough to understand for users of other languages.

unit ImpoDemo;
interface
 
uses
Windows, Messages, SysUtils, Classes, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DebenuPDFLibrary1113, ExtCtrls;
 
type
TfrmDemo = class(TForm)
PanControl: TPanel;
BtnCreatePage: TButton;
StaticText2: TStaticText;
ImpositionScrollBox: TScrollBox;
Memo1: TMemo;
Label1: TLabel;
procedure BtnCreatePageClick(Sender: TObject);
private
public
{ Public declarations }
end;
 
var
frmDemo: TfrmDemo;
 
implementation
{$R *.dfm}
 
procedure TfrmDemo.BtnCreatePageClick(Sender: TObject);
type
ObjRec=record // description of imposition-object
id: integer; // page-id
w, h: double; // size
end;
var
PDFLib: TDebenuPDFLibrary1113;
Counter: integer;
posx, posy: double; // left, bottom
dimx, dimy: double; // sheet size
w, h: double;
i, j: integer;
capt:array of ObjRec;
begin
PDFLib := TDebenuPDFLibrary1113.Create;
if PDFLib.UnlockKey('...add_license_key_here...') = 1
then begin
// Quelldateien laden
PDFLib.AddToFileList('List', 'tiger_emf.pdf');
PDFLib.AddToFileList('List', 'debenu final tm.pdf');
// workfile
PDFLib.MergeFileList('List', '_temp.PDF');
PDFLib.LoadFromFile('_temp.PDF');
PDFLib.SetMeasurementUnits(1); // millimeters
 
DeleteFile('_temp.PDF');
Counter := PDFLib.PageCount; // PageCount is number of objects
SetLength(capt, Counter); // make the array
for i:=0 to Counter-1
do begin // get the sizes from Mediabox
StaticText2.Caption := IntToStr(i);
capt[i].w := PDFLib.GetPageBox(1,2);
capt[i].h := PDFLib.GetPageBox(1,3);
capt[i].id := PDFLib.CapturePage(1); // from the top
// to see the values
Memo1.Lines.Add(Format('%d: %fx%f (%d)',[i, capt[i].w, capt[i].h, capt[i].id]));
if (PDFLib.PageCount=1) then PDFLib.NewPage; // never empty
end;
// imposition
PDFLib.SelectPage(1);
dimx := 600;
dimy := 400;
PDFLib.SetPageDimensions(dimx, dimy); // size for output
 
Memo1.Lines.Add('');
posx := 5; // left border 5mm
posy := 5;
for i:=0 to Length(capt)-1 // every object as often as possible in a line
do begin
StaticText2.Caption := IntToStr(i+1);
w := capt[i].w;
h := capt[i].h;
j := 1;
while (posx + w < dimx-5)
do begin
PDFLib.DrawCapturedPage(capt[i].id, posx, posy+h, w, h);
Memo1.Lines.Add(Format('object %d as %fx%f',[i+1, w, h]));
posx := posx + w+10; // distance between objects
w := w*j/(j+1); // more and more less
h := h*j/(j+1);
inc(j);
end;
posy := posy + capt[i].h+10;
posx := 5; // left border 5mm
end;
 
// Delete the extra page that was created when the original pages were captured
PDFLib.DeletePages(PDFLib.PageCount(), 1);
// Save our work
PDFLib.SaveToFile('debenu_quick_pdf_library_imposition.pdf');
end
else ShowMessage('Sorry, but the license key you provided could not be validated.');
end;
 
end.
Updated on April 9, 2017

Was this article helpful?

Related Articles

Ready to try Foxit PDF SDK?
Click the link below to download your trial
Free Trial