ImageEn for Delphi and C++ Builder ImageEn for Delphi and C++ Builder

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
Forum membership is Free!  Click Join to sign-up
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Load Base64 PDF Data -> Save JPG for each page
 New Topic  Reply to Topic
Author Previous Topic Topic Next Topic  

FLDelphi

19 Posts

Posted - Jul 05 2022 :  11:18:14  Show Profile  Reply
I have PDF data encoded as base64. I'm trying to load it and save each page as a JPG. It ends up running an infinite loop, saving the first page over and over again.

I wrote a simple procedure to demonstrate the problem and attached the Base64 file.

This is Delphi Seattle + ImageEn 10.2.0


procedure TForm1.Button1Click(Sender: TObject);
var
  ImageEnIO : TImageEnIO;
  idx : Integer;
begin

  ImageEnIO := TImageEnIO.Create(nil);
  try
    ImageEnIO.LoadFromText('C:\Temp\ImageEnBase\source.txt', ietfBase64);

    {Save the first page}
    ImageEnIO.Seek(ieioSeekFirst);
    ImageEnIO.SaveToFileJpeg('C:\Temp\ImageEnBase\RDJpeg1.jpg');

    idx := 0;

    {Loop over the remaining pages and save them}
    while ImageEnIO.Seek( ieioSeekNext ) <> idx do
    begin
      ImageEnIO.SaveToFileJpeg('C:\Temp\ImageEnBase\RDJpeg' + idx.ToString + '.jpg');

      inc(idx);
    end;
  finally
    ImageEnIO.Free;
  end;
end;





attach/FLDelphi/202275111859_source.txt

xequte

38608 Posts

Posted - Jul 05 2022 :  21:36:57  Show Profile  Reply
Hi

TImageEnIO.Seek cannot be used with special files, like those decoded from text. It needs to be passed an image filename or a stream, e.g.

var
  fn: string;
  io : TImageEnIO;
  idx : Integer;
begin
  fn := 'D:\Testing_Multimedia\TIFF\TIFF_4pages_mono_3352x4743.tiff';
  io := TImageEnIO.Create(nil);
  try
    idx := 0;
    io.LoadFromFile( fn );
    Repeat
      io.SaveToFileJpeg('D:\TiffOut_' + idx.ToString + '.jpg');
      inc( idx );
    until io.Seek( ieioSeekNext, fn ) <> idx;
  finally
    io.Free;
  end;
end;



So you would need to load the text content and base64 decode it to a stream in order to use it with Seek.

If you don't have special reason to do that, you are better to just use the TImageEnView.PDFViewer to handle the loading, e.g.

var
  iev : TImageEnView;
  idx : Integer;
begin
  // Check PDF library available
  PDFiumLibAvailable( True );

  iev := TImageEnView.Create(nil);
  try
    iev.PdfViewer.Enabled := True;
    iev.IO.LoadFromText('D:\source.txt', ietfBase64);

    {Save the first page}
    iev.IO.Seek(ieioSeekFirst);
    iev.IO.SaveToFileJpeg('D:\PageOut1.jpg');

    idx := 0;

    {Loop over the remaining pages and save them}
    while iev.IO.Seek( ieioSeekNext ) <> idx do
    begin
      iev.IO.SaveToFileJpeg('D:\PageOut' + idx.ToString + '.jpg');

      inc(idx);
    end;
  finally
    iev.Free;
  end;
end;

In this case Seek works, because the PDFViewer loads the entire PDF and seek just redirects to the PDFViewers "Next Page" function.


BTW, I would prefer to use Repeat/Until in this situation to make the code a little more concise:

var
  iev : TImageEnView;
  idx : Integer;
begin
  // Check PDF library available
  PDFiumLibAvailable( True );

  iev := TImageEnView.Create(nil);
  try
    iev.PdfViewer.Enabled := True;
    iev.IO.LoadFromText('D:\source.txt', ietfBase64);
    idx := 0;

    Repeat
      iev.IO.SaveToFileJpeg('D:\PageOut_' + idx.ToString + '.jpg');
      inc( idx );
    until iev.IO.Seek( ieioSeekNext ) <> idx;

  finally
    iev.Free;
  end;
end;



Nigel
Xequte Software
www.imageen.com
Go to Top of Page

FLDelphi

19 Posts

Posted - Jul 06 2022 :  12:47:01  Show Profile  Reply
I think that's working great so far. Thanks for your help Nigel!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
Jump To: