Print Page | Close Window

Reduce PDF file size

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=2846
Printed Date: 22 Nov 24 at 5:39PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Reduce PDF file size
Posted By: kk aw
Subject: Reduce PDF file size
Date Posted: 12 Mar 14 at 10:14AM
I have a large pdf document, and I want to reduce the document size.

Can I use replaceimage and clearimage to reduce the document size?

In my test with both version 9.16 and 10.13, it does not seem to work.

The replaceimage function always returned 0.  Does this mean that it failed?

Using the clearimage function by itself works and the image is no longer visible in the document. Unfortunately, the filesize is not reduced.






Replies:
Posted By: AndyD
Date Posted: 12 Mar 14 at 10:30AM
I expect you've already tried this but just in case, have you used the compress page function so that QP reduces your PDF as much as possible?
 
For a = 1 To QP.PageCount
    Call QP.SelectPage(a)
    Call QP.CompressPage
Next a
QP.SaveToFile (SaveString)
 
Worth mentioning just in case.


Posted By: AndrewC
Date Posted: 12 Mar 14 at 11:50PM
In versions of Debenu Quick PDF Library since version 8 or so the CompressImages flag has been set to true.

This flag does not resize images. All it does is turn on image compression so that when a PDF file is saved then the image data is compressed and stored much like a Zip file compresses a file.   If the image data in the PDF is not compressed (quite rare these days) then compressing it will save some space.  Most files will not be affected.

Currently if you would like to resample and downscale images then you would need to extract the image, load the image into an image processing library such as ImageEn, ImageMagick - process it and then call QP.ReplaceImage to replace the image with smaller compressed, lower quality image data.  

Andrew.



Posted By: kk aw
Date Posted: 13 Mar 14 at 7:04PM
I use the following codes to convert the bmp image format to jpg image format:

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 retval in the line:   retval:= qp.ReplaceImage(ImgID, nImgID) is always 0.

If I carry one regardless, qp.clearimage(imgid) returns 1.....

The image-replaced file size is slightly bigger than the original.

Any ideas what is happening?





Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk