Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!

Debenu Quick PDF Library - PDF SDK Community Forum Homepage
Forum Home Forum Home > For Users of the Library > I need help - I can help
  New Posts New Posts RSS Feed - Reduce PDF file size
  FAQ FAQ  Forum Search   Register Register  Login Login

Reduce PDF file size

 Post Reply Post Reply
Author
Message
kk aw View Drop Down
Team Player
Team Player
Avatar

Joined: 02 Feb 10
Location: Malaysia
Status: Offline
Points: 36
Post Options Post Options   Thanks (0) Thanks(0)   Quote kk aw Quote  Post ReplyReply Direct Link To This Post Topic: Reduce PDF file size
    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.



Back to Top
AndyD View Drop Down
Senior Member
Senior Member
Avatar

Joined: 30 Apr 13
Location: UK
Status: Offline
Points: 54
Post Options Post Options   Thanks (0) Thanks(0)   Quote AndyD Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
AndrewC View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 08 Dec 10
Location: Geelong, Aust
Status: Offline
Points: 841
Post Options Post Options   Thanks (0) Thanks(0)   Quote AndrewC Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
kk aw View Drop Down
Team Player
Team Player
Avatar

Joined: 02 Feb 10
Location: Malaysia
Status: Offline
Points: 36
Post Options Post Options   Thanks (0) Thanks(0)   Quote kk aw Quote  Post ReplyReply Direct Link To This Post 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?


Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.01
Copyright ©2001-2014 Web Wiz Ltd.

Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. AboutContactBlogSupportOnline Store