WPCubed Product Support

Images not transparent in PDF export

Hi

I have a WP tools document (v7.33.1) with pictures (embedded or through Insertpoints). These pictures are transparent (PNG). In the WPTools editor, this seems fine (I have a background image that sees through the image). But when I export my document to PDF using wPDF the images are not transparent anymore and get a white background covering my background, creating ugly white blocks.

Because I need further processing of the canvas after each page is drawn, I cannot use the TWPPdfExport component. So I use this code to draw the pages on the wPDF canvas:

    bodyText.Refresh;
    C := BodyText.PageCount;
    if C > 0 then
    begin
      result := true;
      PDFCreator.StartDocument;  // Create a default page on the engine canvas
      try
        for I := 0 to C-1 do
        begin
           // Render contents of current page of rich-edit control
           // on the PDF engine canvas
          BodyText.PaintPageOnCanvas(
                i,  // page number
                0,  // left margin (x-offset) on the canvas
                0,  // top margin (y-offset) on the canvase
                0,  // rectangle width on the canvas (0 for full width)
                0,  // rectangle height on the canvas (0 for full height)
                PDFCreator.Canvas,  // canvas of the PDF engine
                [wppInPaintForwPDF, wppNoPageBackground, wppWhiteIsTransparent],
                0,  // horizontal resolution of canvas (0 for autodetection)
                0,  // vertical resolution of canvas (0 for autodetection)
                -1, // top clipping (-1 for no clipping)
                -1, // height of the rectangle (-1 for no clipping)
                [wpUseProvidedWidthHeight, wpUseWorldScaling]);
           // Create next page if required
           if I < C -1 then begin
             PDFCreator.NewPage;
           end;
        end;
      finally
        PDFCreator.EndDocument;
      end;
    end;

The PDFCreator is a wrapper around the wPDF component. The code inside the PDFCreator component looks like this:

type
  TENKwPDFCreator = class(TInterfacedObject, IENKPDFCreator)
  private
    FPDFEngine: TWPPDFPrinter;
    ..
    function GetCanvas: TCanvas;
    procedure Startpage;
  protected
  public
    procedure CreatePDFDocument(afilename: string; aDPI: integer);
    procedure StartDocument;
    procedure NewPage;
    procedure EndDocument;
  end;

Implementation

function TENKwPDFCreator.GetCanvas: TCanvas;
begin
  result := FPDFEngine.Canvas;
end;

procedure TENKwPDFCreator.CreatePDFDocument(afilename: string; aDPI: integer);
begin
  FFilename := aFilename;
  FPDFEngine.Free; //Clean up possible old engine
  FPDFEngine := TWPPDFPrinter.Create(nil);
  WPDF_Start($$$$$$,$$$$$$$);
  FPDFEngine.AutoLaunch := false;
  FPDFEngine.FileName := afilename;

  FPDFEngine.ConvertJPEGData := false; //or True makes no difference!!!!

  FPDFEngine.Modes := [wpClipRectSupport, wpArabicAlwaysAsOutline, wpConvertColorsToCMYK, wpAllowTransparentBit];
  FDPI := aDPI;
  FPagenr := 0;
end;

procedure TENKwPDFCreator.Startpage;
begin
  FPageWidth  := round( 8.27 * FDPI); //for now Fixed A4 size
  Fpageheight := round(11.69 * FDPI); //for now Fixed A4 size
  FPDFEngine.StartPage(FPageWidth, FPageHeight , FDPI, FDPI, 0);
end;

procedure TENKwPDFCreator.StartDocument;
begin
  FPagenr := 1;
  FPDFEngine.BeginDoc;
  StartPage;
end;

procedure TENKwPDFCreator.NewPage;
begin
  Inc(FPagenr);
  FPDFEngine.EndPage;
  Startpage;
  //My extra processing of the page
end;

procedure TENKwPDFCreator.EndDocument;
begin
  FPDFEngine.EndPage;
  FPDFEngine.EndDoc;
  FreeAndNil(FPDFEngine);
end;
#1 ♥ 0
26.1k Views 4 users read 9 min
wpsupport

In WPObj_Image there should be code inside of the IFDEF EMBEDD_PNGJPG

It makes sure that a PNG is embedded instead of painted to the Canvas which will always destroy the transparency. This requires wPDF V4.

Please place a breakpoint there to find out why the code is not executed when you are inside BodyText.PaintPageOnCanvas. I assume this has to do with $DEFINES.

#2 ♥ 0

When I active {$DEFINE WPDF4} the PNG is printed correctly (with transparency) but when I load JPG files the images don't show, to fix I needed to change the line 1553 in WPOBJ_Image, I'm using Wptools 8 with Delphi 7

{$IFDEF EMBEDD_PNGJPG} //WPDF4

to

{$IFNDEFEMBEDD_PNGJPG} //WPDF4

is correct ? tks

#3 ♥ 0
wpsupport

JPEG is a format which can be directly embedded into PDF so executing

WPWriteComment( toCanvas.Handle, 3022, BoundsRect, DataPtr, DataLen);

is correct, as long as the data is really JPEG data. Is the file extension incorrectly set?

#4 ♥ 0

Yes, I am checking extensions and format (using irfanview) for the image files

#5 ♥ 0
wpsupport

fileextension is a property of the image object. If you load the date from stream it must be set.

#6 ♥ 0

Yes, I'm using the code below, running LoadFromFile I dont need set fileextension

Fn := 'picture.jpg';

try

wpobj := TWPOImage.Create(WPRichText1.Memo.RTFData);

wpobj.LoadFromFile(Fn);

finally

DeleteFile(Fn);

end;

#7 ♥ 0
wpsupport

I am sorry, I don't know anymore what the problem is. If you found the fix above what is the problem?

WPTools 9.1 would be the current version.

#8 ♥ 0

with this line I can print jpeg image, can you check ?

{$IFDEF EMBEDD_PNGJPG} //WPDF4

// wPDF4 allows transparent PNG embedding

// WPDFCOMM_ADDIMAGEDATA = 3020; // Add image data Param = TWPDFImageCommand

// WPDFCOMM_DRAWPNGDATA = 3021; // Add PNG Data

// WPDFCOMM_DRAWJPEGDATA = 3022; // Add JPEG Data

if (wpPaintObjDestIsPDF in PaintMode) and (DataIs('jpg')or DataIs('jpeg')) then

begin

WPWriteComment( toCanvas.Handle, 3022, BoundsRect, DataPtr, DataLen);

if not bDisablePaint then inherited Paint(toCanvas, BoundsRect, ParentTxtObj, PaintMode);

end else

#9 ♥ 0
wpsupport

Basically you just add the bitmap data. I assume something is done between the printing and the PDF export with wPDF so the data written by WPWriteComment does not get through. This appears to be connected to the concrete project.

#10 ♥ 0

Have the same problem. Have a PNG transparent image and looks fine in the WPT9 editor but not when exported to PDF using wPDF4

#11 ♥ 0
wpsupport

I just tried with the word processor demo - it works fine.

Try to disable GDIPP in Wpobj_image.pas

#12 ♥ 0

Sorry, I do not see a GDIPP switch in WPObj_Image. Could it be "USEGPP"? I tried disabling USEGPP and it made no difference.

Also, how did you test it in your demo? I downloaded WPCubedWordprocessorDemo and could not find a way to insert a PNG image to test.

To test, I created a one row two column table. I then shade the table cells and insert a linked PNG image into one of the cells. Print preview shows it correct, but then creating the PDF is where it is not transparent, i.e. the cell color does not come through the transparent parts of the image.

#13 ✎ ♥ 0

OK, Instead of using the Insert Image option in your demo which did not give me the option for PNG images, I tried dragging the image into the table cell which accepted it. Creating the PDF worked fine as you said.

Maybe it has something to do with linked images? Or perhaps I have some PDF creation setting set wrong. I'll dig into it more and let you know what I find.

#14 ♥ 0

I created a new project, so nothing but defaults except for the AcceptFiles option is now true in the WPRichText. I pre-loaded a shaded table with a transparent PNG in one cell. Created the PDF and it is not transparent. I then dragged that same PNG image I used in your demo, and dropped it into the other cell. PDF again created without transparency.

I will send you the sample. I am at a loss as to what might be different between your demo and my basic sample app.

#15 ♥ 0
wpsupport

For PNG export with transparency you need wPDF 4.

Please also add {$DEFINE WPDF4} to the file WPINC.INC or WPDF4 to the project conditionals and do a BuildAll.

#16 ♥ 0

Thanks, it was the missing {$DEFINE WPDF4} switch in the WPINC file. Adding that fixed it. 🙂

#17 ♥ 0

I just noticed that the define is no longer fixing transparency. I still have {$DEFINE WPDF4} in WPINC.INC, but transparent PNG images that look fine in print preview are not transparent in the PDF.

WPTools 9.2.3 and 9.2.4

Delphi 11.1

#18 ✎ ♥ 0

Apparently our PNG signature images are not working even though they are transparent PNG images. I tried an older standard transparent PNG image and it worked fine. I'll send you a sample signature PNG image... maybe you can tell me why it doesn't work for those even though it is transparent in the print preview. The signature images are ones we converted from BMP to PNG using the following code, maybe I'm missing something:

procedure TfrmSignature.btnAcceptClick(Sender: TObject);
var
  vBitmap: TBitmap;
  vPng: TPngImage;
begin
  vBitmap := TBitmap.Create;
  try
    AdvSignatureCapture1.SaveToBitmap(vBitmap);
    vBitmap.TransparentColor := clWhite;
    vBitmap.Transparent := true;
    vPng := TPngImage.Create;
    try
      vPng.Assign(vBitmap);
      vPng.SaveToStream(fImageStream);
      fImageStream.Position := 0;
    finally
      FreeAndNil(vPng);
    end;
  finally
    FreeAndNil(vBitmap);
  end;
end;
#19 ♥ 0

I found if I force the Bitmap to 8bit, it works fine. Strange because my old sample PNG image is 32bit and works fine, but the only bit depth for the signature file that is still transparent in the PDF is 8bit (I tried all the pf values):

vBitmap.PixelFormat := pf8bit;

Is there an issue with the PDF exporter?

#20 ♥ 0

Gets weirder. If I save either the 8bit or 32 bit versions of the PNG to a file and insert it into the WPRichText as a linked image, it works fine in the PDF (both are transparent). However, for signatures I insert them into the WPRichText as embedded images.

So I guess that would be the difference, and would be why the sample PNG I had was working because it was a linked image and the signature testing was an embedded image.

Something you may want to check out. Why an embedded PNG image needs to be 8bit to be transparent in a PDF but a 32bit PNG is not transparent in the PDF. Both are transparent in the editor and in Print Preview.

#21 ♥ 0
wpsupport

8 bit transparent are color coded while 24bit transparent use the alpha channel.

wPDF4 requires an alpha channel for transparency - so it does not support the ptmBit Transparency Mode since PDF does not do colorkey for 24bit color images.

#22 ♥ 0

OK, but they come out transparent if added as linked images but not when added as embedded images. I have no problem making them 8bit since the signatures are a single color anyway. Just strange that there seems to be a difference between being embedded vs being linked.

To make them linked in a test, I simply wrote the stream to a file and inserted the file as a linked image whereas for embedded I take the same stream and embed the image:

      var vWPObject := WPCreateObjectForExtension(WPRichText1, 'png');
      if vWPObject <> nil then
      begin
        try
          vWPObject.FileExtension := 'png';
          vWPObject.LoadFromStream(vImageStream);
          PasteSignature(WPRichText1, vWPObject);
        except
          FreeAndNil(vWPObject);
        end;

And PasteSignature basically just does this (pWPObject is vWPObject passed in):

vTextObj := pWPRichText.TextObjects.Insert(pWPObject, pWPObject.ContentsWidth, pWPObject.ContentsHeight);

#23 ♥ 0
wpsupport

They are apparently different then. They key is the transparent color setting, alpha channel always works.

#24 ♥ 0

Please ignore the 8bit version. I only mentioned that to indicate that it is fine and transparent even in the PDF for just that format when it is an embedded image.

The 32bit versions should be the same. In testing, I simply write the stream to a file before writing the same stream to a WPRichText object for embedding. Both the embedded and linked image of the same stream show up as transparent in the editor and in the print preview, but only the linked image version is transparent in the PDF, the embedded version is not.

#25 ♥ 0
This category is archived. No new posts are possible.