Retrieve Images along with text in RTF format

  • var

    listImage : TWPTextObjList;

    count:Integer;

    WPRichText : TWPCustomRtfEdit //( Passed in function call)

    Record : IXMLNode;


    begin

    listImage := TWPTextObjList.Create;

    WPRichText.ImageGetList(listImage,true);

    count:=listImage.Count; //Returns Image count


    Record.ChildNodes['RTF'].text := AWPRichText.RtfText.AsString;

    Record.ChildNodes['RTF1'].text :=AWPRichText.AsANSIString('RTF');

    //When saved as file, the above two node has only text and not images

  • AsANSIString

    -> Tried AsANSIString('RZF-nobinary')-> Returned empty string

    -> Tried AsANSIString('RTF-nobinary')-> Only text are returned not images

    Tried SaveToFile in both format RTF and RTF-nobinary but still images are not saved in the file.

  • Yes Images are present in document.

    -->Consider below sample code for reference

    WPRichTextDocument (TWPRichText)--> Images are added in OnRequestHTTPImage event ( procedure WPRichTextRequestHTTPImage )

    CreateRtfDocument(WPRichTextDocument);

    procedure CreateRtfDocument(WPRichText : TWPCustomRtfEdit)

    {

    begin

    WPRichText.OnRequestHTTPImage := WPRichTextRequestHTTPImage;// Tried this now but still no images are add in the documents

    WPRichText.SaveToFile('D:\RTF\SampleDocument.rtf',FALSE,'RTF-nobinary');

    WPRichText.SaveToFile('D:\RTF\SampleDocument1.rtf',FALSE,'RTF');

    end

    }

    Need Clarification:

    While passing WPRichTextDocument as argument whether the images also will be passed or not?

    Is there any way to directly call OnRequestHTTPImage event, so that images may added in WPRichText.

    • Offizieller Beitrag

    WPRichText.OnRequestHTTPImage

    is called when a file is loaded which contains links to images. This can happen in RTF or HTML files. If the images are defined with a real filename they can be loaded if they are found. The event makes it possible to load images which are represented by an id. They can then also be loaded from a database.

    Your code CreateRtfDocument does not necessarily create a document with images. WPRichText.OnRequestHTTPImage := WPRichTextRequestHTTPImage must happen before loading a file.

    If you want to process images before they are saved, use the event OnPrepareImageforSaving.

    If WPRichText.SaveToFile('D:\RTF\SampleDocument1.rtf',FALSE,'RTF') does not save images, then there are no images in the file, at least no images which contain PNG, BMP or JPEG data. Image objects which are painted through event or use a different TGraphic class do not necessarily contain such data.

  • Tried today,

    1) WPRichText.Print() -> Here it prints the document with images.

    2) Also in PDF export the images are exported with PDF Export component.

    Consider below sample code for reference

    var

    pdf : TWPPDFExport;

    pdfStream : TMemoryStream;

    pdfString : string;

    begin

    pdfStream := TMemoryStream.Create;

    WPDF_Start('', ''); -> Application related strings are passed

    pdf.InMemoryMode := True;

    pdf.Stream := pdfStream;

    pdf.Source := WPRichText;

    pdf.FontMode := wpEmbedSymbolTrueTypeFonts;

    pdf.CompressStreamMethod := wpCompressFlate;

    pdf.JPEGQuality := wpJPEG_50;

    pdf.Print;

    pdfString := MemoryStreamToString(pdfStream)

    Is there any component like TWPPDFExport for exporting in RTF format?

  • Im new to delphi, so its hard for me to create a similar test project. I understood its difficult for your team without sample test project but kindly guide me. After the property TWPRichText.WriteObjectMode is changed from wobStandardNoBinary to wobRTF.

    1) TWPRichText.SaveToFile() --> saves RTF file with images but not all images.

    -> Only Images which are loaded through InputMergeField (image added at head and end of document) are displayed.

    -> other images added at event OnRequestHTTPImage are not displayed. In RTF document getting message as "The linked image cannot be displayed. The file may have been moved, renamed or deleted. Verify that link points to correct file and location "

    2)Tried TWPRichText.Print() before SaveToFile() method and here all the images are printed

    3) TriedTWPRichText.AsANSIString('RTF-nobinary');

    \pard\plain\plain\f0\fs24{\field{\*\fldinst{MERGEFIELD logo-Header \{6A7A1AD2-A099-4006-AD33-4BFE17803F4A\}}}{\*\wpfldparam{logo-Header}}{\fldrslt{{\pict\jpegblip\picw12240\pich15840\picwgoal12240\pichgoal15840 --> After this encoded image value is present

    \pard\plain\s0\plain\fs24

    {\field{\*\fldinst{INCLUDEPICTURE "{587CEBEE-A031-40F3-943E-329D4A3534DC}" MERGEFORMAT \\d \\w1920 \\h2880 \\pm1 \\px9600 \\py0 \\pw0}}}\par

    \pard\plain\par --> No encoded image value. just empty after this

    \plain\fs14{\field{\*\fldinst{MERGEFIELD logo-Footer \{18E1A3CC-EEC9-473F-87E9-F5A05C666A1C\}}}{\*\wpfldparam{logo-Footer}}{\fldrslt{\f0\fs24{\pict\jpegblip\picw28305\pich1065\picwgoal28305\pichgoal1065 --> After this encoded image value is present

    4) In debug mode, value of TextObject(Images)

    (280, wpobjImage, $D565F20, [], [], $BEB0020, -1, '', '', '', nil, nil, '', 0, 0, 0, 12240, 15840, (nil,nil), (nil,nil), (nil,nil), 0, 0, wpwrAutomatic, nil)

    (534, wpobjImage, $D566C40, [wpobjRelativeToParagraph], [], $1437E920, -1, '', '', '', nil, nil, '', 0, 0, 0, 1920, 2880, (nil,nil), (nil,nil), (nil,nil), 9600, 0, wpwrAutomatic, nil) --> Missing image

    (485, wpobjImage, $D566970, [], [], $14957F80, -1, '', '', '', nil, nil, '', 0, 0, 0, 10930, 1042, (nil,nil), (nil,nil), (nil,nil), 0, 0, wpwrAutomatic, nil).

    Is there any other values or details you need, kindly let me know.