#1 2019-07-19 11:57:16

Blanca
Member
Registered: 2019-07-19
Posts: 2

Create pdf from tiff w/o losing quality

Hello everybody,

I need to reduce pdf file size and i tried the following code.

 var TIFFImg:TTIFFImage;
    Filename,ImgOX : string;
    PDFImg:tpdfimage;
    stream:tfilestream;
    PDFGDI: TPdfDocumentGDI;
    PDFPage: TPdfpage;
    i:integer;
    jpeg:TJpegImage;
    wBMP: tbitmap;
 begin
    Filename := 'C:\TEMP\ScannedImage.pdf';
    TIFFImg:=TTIFFImage.Create;
    PDFGDI:=TPdfDocumentGDI.Create();
    Stream := TFileStream.Create(FileName, fmCreate);
    JPeg := TJPegImage.Create;
    wBMP := TBitmap.create;
    TIFFImg.LoadFromFile('C:\TEMP\ScannedImage.TIF');

    try
       PDFGDI.DefaultPaperSize:= psA4;
       PDFGDI.CompressionMethod:=cmFlateDecode;
       PDFGDI.GeneratePDF15File:=true;

       PDFGDI.ScreenLogPixels:= 72;
       PDFGDI.SaveToStreamDirectBegin(Stream);
       PDFGDI.ForceJPEGCompression := 60;

       for i:=0 to TIFFImg.GetPageCount-1 do
       begin
         TIFFImg.ActivePageIndex :=i;
         TIFFImg.ExtractPage(TIFFImg.ActivePageIndex, wBMP);
         Jpeg.Assign(wBMP);
         PDFGDI.DefaultPageWidth:=TIFFImg.Width;
         PDFGDI.DefaultPageHeight:=TIFFImg.Height;
         PDFPage:= PDFGDI.AddPage;
         PDFImg:= TPdfImage.Create(PDFGDI,Jpeg,true);
         ImgOx:='Im'+IntToStr(i);
         PDFGDI.AddXObject(ImgOx,PDFImg);
         PDFGDI.Canvas.DrawXObject(0,0, PDFGDI.DefaultPageWidth, PDFGDI.DefaultPageHeight, ImgOx);
         PDFGDI.SaveToStreamDirectPageFlush;
       end;
       PDFGDI.SaveToStreamDirectEnd;
     finally
       Stream.Free;
       PDFGDI.Free;
       TIFFImg.Free;
       jpeg.free;
     end;
end;

But althoug the file is very reduced, the quality in the output file is very bad. i've tried the same code without transforming to jpeg but the result is good quality too much size.
Is there any way to reduce pdf size? Thank you very much!

Offline

Board footer

Powered by FluxBB