procedure TMainform.ConvertImage1Click(Sender: TObject);
var
i,j,n: Integer;
ImgID, nImgID: Integer;
ImgCount: Integer;
ImgName1, ImgName2: widestring;
ImgType: Integer;
ImgConverted: Integer;
FPrivdir: string;
FileExt: string;
FFileName: string;
retval: Integer;
Fsize1, FSize2: Integer;
begin
Screen.Cursor := crHourGlass;
FPrivdir:= sm.slashAdd(LmdSysInfo1.TempPath);
FileExt:= sm.locase(ElcomboBox1.Text);
FSize1:= Qp.GetDocumentFileSize;
ImgConverted:= 0;
try
ImgCount := Qp.FindImages;
j:= 1;
imgId := QP.GetImageID(j);
while j <= ImgCount do
begin
qp.SelectImage(ImgID);
ImgType := Qp.ImageType;
if ImgType = 2 then
begin
ImgName1 := FPrivdir + Format('PdfImage%d.bmp',[j]);
ImgName2 := FPrivdir + Format('PdfImage%d.%s',[j,FileExt]);
QP.SaveImageToFile(Imgname1);
If sm.EqualIC(FileExt,'jpg') then
Bmp2Jpeg(ImgName1, ImgName2)
else if sm.EqualIC(FileExt,'gif') then
continue
else if sm.EqualIC(FileExt,'png') then
continue;
If FileExists(imgname2) then
begin
nImgID:= qp.AddImageFromFile(ImgName2,0);
if nImgID = 0 then
begin
ShowMessage('Cannot addimagefromfile '+ImgName2);
exit;
end;
end
else
begin
ShowMessage(ImgName2 + ' not found');
exit;
end;
retval:= qp.ReplaceImage(ImgID, nImgID);
If retval = 0 then
begin
ShowStatusBar('Image not replaced');
Sleep(500);
end
else
begin
retval:= qp.ClearImage(ImgID);
If retval = 0 then
ShowMessage('Image not cleared');
end;
FSize2:= Qp.GetDocumentFileSize;
Inc(ImgConverted);
end;
Inc(j);
imgId := QP.GetImageID(j);
end;
Screen.Cursor := crDefault;
If ImgConverted > 0 then
begin
ShowMessage(Format('%d Embedded images successfully converted.'+crlf+
'Originalsize: %d New size: %d',[ImgConverted, FSize1, FSize2]));
FFileName:= FSelectedFile;
n:= sm.posIC('.pdf', FFileName);
System.Insert('[IR]',FFileName, n);
Qp.SaveToFile(FFileName);
end
else
ShowStatusBar('No image to convert or converted');
finally
Screen.Cursor := crDefault;
end;
end;
procedure TMainform.Bmp2Jpeg(const BmpFileName, JpgFileName: string);
var
Bmp: TBitmap;
Jpg: TJPEGImage;
begin
Bmp := TBitmap.Create;
Jpg := TJPEGImage.Create;
try
Bmp.LoadFromFile(BmpFileName);
Jpg.Assign(Bmp);
Jpg.SaveToFile(JpgFileName);
finally
Jpg.Free;
Bmp.Free;
end;
end;
The image files in bmp and jpg as exported.
The image-replaced file size is slightly bigger than the original.