#1 2013-10-24 10:53:40

bodnarj
Member
Registered: 2013-10-24
Posts: 2

Create multipage PDF images

I want to create a PDF document from images. One image per pages.
My code is not work correct.
The first page is correct, but  i see the second page, the Acrobat Reader throw "Out of memmory" error dialog.
If i create unique TPdfPage variable all pages the PSD document is corret, but i use array of TPdfPage the pdf document not correct.
What is the problem in this code?

This code create incorrect PDF document:

const
  PDF_W = 595;
  PDF_H = 842;
  A4_W  = 210;
  A4_H  = 297;

var pdf : TPdfDocument;
    PdfImage: TPdfImage;
    JpgImage : TJPEGImage;
    mstream : TMemoryStream;
    PageList: array of TPdfPage;
    w, h : extended;
    i : integer;
begin
  pdf := TPdfDocument.Create;
    SetLength(PageList,imgList.Count);
    for i:= 0 to imgList.Count-1 do
      begin
        w := TBitmap(ImageList[i]).Width;
        h := TBitmap(ImageList[i]).Height;
        w:=((w/felbontas)*2.54)*(PDF_W/A4_W)*10;
        h:=((h/felbontas)*2.54)*(PDF_H/A4_H)*10;
        lblW.caption := FloatToStr(w);
        lblh.caption := FloatToStr(h);
        JpgImage.Assign(TBitmap(ImageList[i]));
          JpgImage.CompressionQuality := 70;
        JpgImage.Compress;
        JpgImage.SaveToStream(mstream);
        PageList[i] := PDF.AddPage;
        Pdf.Canvas.SetPage(PageList[i]);
        PdfImage := TPdfImage.CreateJpegDirect(pdf,mstream);
        Pdf.AddXObject('Image'+IntToStr(i), PdfImage);
        Pdf.Canvas.DrawXObject(10, PageList[i].PageHeight-h-30, w, h, 'Image'+IntToStr(i));
      end;

This code create correct PDF document:

        Page := PDF.AddPage;
        Pdf.Canvas.SetPage(Page);
        PdfImage := TPdfImage.CreateJpegDirect(pdf,mstream);
        Pdf.AddXObject('Image0', PdfImage);
        Pdf.Canvas.DrawXObject(10, page.PageHeight-h-30, w, h, 'Image0');

        Page1 := PDF.AddPage;
        Pdf.Canvas.SetPage(Page1);
        PdfImage := TPdfImage.CreateJpegDirect(pdf,mstream);
        Pdf.AddXObject('Image1', PdfImage);
        Pdf.Canvas.DrawXObject(100, page.PageHeight-h-30, w, h, 'Image1');


        Page2 := PDF.AddPage;
        Pdf.Canvas.SetPage(Page2);
        PdfImage := TPdfImage.CreateJpegDirect(pdf,mstream);
        Pdf.AddXObject('Image2', PdfImage);
        Pdf.Canvas.DrawXObject(10, page.PageHeight-h-230, w, h, 'Image2');

Thanks.
Best Regards.
Joe

Offline

#2 2013-10-24 12:04:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,183
Website

Re: Create multipage PDF images

Do not STORE the pages in an array.
Let the TPdfDocument class handle them.

You can then "rewind" to any previous page using the SetPage() method.

Offline

#3 2013-10-24 12:37:31

bodnarj
Member
Registered: 2013-10-24
Posts: 2

Re: Create multipage PDF images

I modifi the code, but the problem is same.

    for i:= 0 to imgList.Count-1 do
      begin
        w := TBitmap(ImageList[i]).Width;
        h := TBitmap(ImageList[i]).Height;
        w:=((w/felbontas)*2.54)*(PDF_W/A4_W)*10;
        h:=((h/felbontas)*2.54)*(PDF_H/A4_H)*10;
        lblW.caption := FloatToStr(w);
        lblh.caption := FloatToStr(h);
        JpgImage.Assign(TBitmap(ImageList[i]));
        JpgImage.CompressionQuality := 70;
        JpgImage.Compress;
        JpgImage.SaveToStream(mstream);
        page := PDF.AddPage;
        Pdf.Canvas.SetPage(page);
        PdfImage := TPdfImage.CreateJpegDirect(pdf,mstream);
        Pdf.AddXObject('Image'+IntToStr(i), PdfImage);
        Pdf.Canvas.DrawXObject(10, PDF_H-h-30, w, h, 'Image'+IntToStr(i));
      end;

So you think that?

Offline

Board footer

Powered by FluxBB