Skia4Delphi makePDF

55 views Asked by At

Please how to create a multipage PDF from multiple images. It's easy for one image. Thank you in advance.

procedure TmainForm.makePDF(imgPath, pdfPath: string);
var
  LCanvas: ISkCanvas;
  LDocument: ISkDocument;
  LDocumentStream: TStream;
  sk:ISkImage;
  LSize: TSizeF;
  paperW, paperH: Single;
begin

  sk := TSkImage.MakeFromEncodedFile(imgPath);

  // A4 210mmx297mm
  paperW := sk.Width;
  paperH := sk.Height;

  LSize := TSizeF.Create(paperW, paperH);

  LDocumentStream := TFileStream.Create(pdfPath, fmCreate);
  try
    LDocument := TSkDocument.MakePDF(LDocumentStream);
    try
      LCanvas := LDocument.BeginPage(LSize.Width, LSize.Height);
      try

        LCanvas.DrawImageRect(sk, RectF(0, 0, sk.Width, sk.Height));

      finally
        LDocument.EndPage;
      end;
    finally
      LDocument.Close;
    end;
  finally
    LDocumentStream.Free;
  end;

end;

Images are loaded from a single folder and are in PNG format. I need to somehow recursively add pages and an extra image to each page.

Done. A demonstration of how to add multiple pages to a PDF.

procedure TmainForm.makeMultiplePDF();
var
  FileList: TStringDynArray;
  s, pth, pdfPath: string;
  folderList: TStringList;
  LCanvas: ISkCanvas;
  LDocument: ISkDocument;
  LDocumentStream: TStream;
  sk:ISkImage;
  LSize: TSizeF;
  f:ISkFont;
  imgW, imgH: Double;
  scale: Double;
  paperW, paperH: Single;
  ixy: integer;
begin
    folderList := TStringList.Create;
    pth := mainDir + pathDelim + edNazev.Text + pathDelim + 'photo';
    FileList := TDirectory.GetFiles(pth, '*sample*.jpg');
    pdfPath := mainDir + pathDelim + edNazev.Text + pathDelim + 'test.pdf';
    if FileList <> nil then
    begin
      LDocumentStream := TFileStream.Create(pdfPath, fmCreate);
      LDocument := TSkDocument.MakePDF(LDocumentStream);

      for s in FileList do
      begin
        folderList.Add(s);

        // PDF
        sk := TSkImage.MakeFromEncodedFile(s);

        // A4 210mmx297mm
        paperW := sk.Width;
        paperH := sk.Height;

        LSize := TSizeF.Create(paperW, paperH);

        LCanvas := LDocument.BeginPage(LSize.Width, LSize.Height);
        LCanvas.DrawImageRect(sk, RectF(0, 0, sk.Width, sk.Height));
        LDocument.EndPage;

      end;

      LDocument.Close;
      LDocumentStream.Free;

    end;

end;

I'm sure many programmers will like the code.

0

There are 0 answers