Creating PDF document containing grayscale images

381 views
Skip to first unread message

Stephen Boyd

unread,
Jul 25, 2015, 3:48:37 PM7/25/15
to pdfium
I am new to Pdfium. I am trying to create a PDF document containing gray scale images. I can create the document just fine but the grayscale images come out looking quite different from the image that went in. If I use the same code adjusted to create 24-bit bitmaps it all works as it should. I have attached a sample of the bitmap and the resulting PDF. I am running this on Windows 8.1 64-bit. I am doing this in Delphi using a Delphi class that wraps Pdfium.  Any hints as to what I am doing wrong are appreciated.

Here is the code that I am using:

        // bmp is a TBitmap which contains the grayscale image
        // Convert the DDB to a DIB
        bmp.HandleType := bmDIB;
        // Save the bitmap to the file that is attached to this post
        bmp.SaveToFile('c:\temp\test.bmp');
        // Create a PDF page
        pdfpg := FPdf.NewPage(8.5 * 72, 11 * 72, 0);
        // FPDFBitmap_CreateEx(w, h, 1, 0, NULL);
        pdfbmp := TPdfBitmap.Create(w, h, bfGrays, nil, 0);
              imgobj := pdfpg.NewImageObject;
        pdfbfr := PByte(pdfbmp.GetBuffer);
        bmpLineLen := BytesPerScanLine(bmp.Width, 8, 32);
        pdfLineLen := pdfbmp.BytesPerScanline;
        // Copy the DIB to the FPDF_BITMAP
        for r := 0 to bmp.Height - 1 do
        begin
            Move(bmp.ScanLine[r]^, pdfbfr^, Min(bmpLineLen, pdfLineLen));
                     Inc(pdfbfr, pdfLineLen);
        end;
        imgobj.SetBitmap(1, pdfbmp);
           imgobj.Transform(8.5 * 72, 0, 0, 11 * 72, 0, 0);
        pdfpg.InsertObject(imgobj);
        pdfpg.ApplyChanges;
        // Save the PDF to the file attached to this post
        FPdf.SaveToFile('c:\temp\test.pdf');





test.bmp
test.pdf
Reply all
Reply to author
Forward
0 new messages