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

 

ImageEn Forum
Profile    Join    Active Topics    Forum FAQ    Search this forumSearch
 All Forums
 ImageEn Library for Delphi, C++ and .Net
 ImageEn and IEvolution Support Forum
 Load Base64 PDF Data -> Save JPG for each page

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

View 
UserName:
Password:
Format  Bold Italicized Underline  Align Left Centered Align Right  Horizontal Rule  Insert Hyperlink   Browse for an image to attach to your post Browse for a zip to attach to your post Insert Code  Insert Quote Insert List
   
Message 

 

Emoji
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Black Eye [B)]
Frown [:(] Shocked [:0] Angry [:(!] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
FLDelphi Posted - Jul 05 2022 : 11:18:14
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
2   L A T E S T    R E P L I E S    (Newest First)
FLDelphi Posted - Jul 06 2022 : 12:47:01
I think that's working great so far. Thanks for your help Nigel!
xequte Posted - Jul 05 2022 : 21:36:57
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