Print Page | Close Window

How to read the string made by RenderPageToString

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3024
Printed Date: 03 Feb 26 at 5:40PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to read the string made by RenderPageToString
Posted By: Mitchell Hu
Subject: How to read the string made by RenderPageToString
Date Posted: 09 Dec 14 at 2:50AM
My requirement is try to use RenderPageToString to convert many PDFs to many BMP Strings for keeping all BMP Strings into a Text file, and then read each BMP Strings to memorystream for make bmp which can show in Image for user;
At fisrt try, I use the String  data from  RenderPageToString write in Memorystream, then show bmp in Image, It is fine, But if save the strings form RenderPageToString into files, and read the strings from then saved txt file, seems ansstring data were changed by strings, I can not show those BMP Strings to Image!!!!
Part of code:
//save BMP Strings to txt file
var : renderString : AnsiString;
renderString := ViewPrintQP.RenderPageToString(DPI, ViewPrintPageNum, 0);
MS.Write(PAnsiChar(renderString)^, Length(renderString));
MS.Seek(0,soFromBeginning);
Image1.Picture.Bitmap.LoadFromStream(MS); =======> Here show BMP is fine
if not FileExists('c:\my.txt') then
begin
     AssignFile(Myfile,'c:\my.txt');
      ReWrite(Myfile);
end
else
begin
    AssignFile(Myfile,'c:\my.txt');
    Append(Myfile);
end;
WriteLn(Myfile,renderString);  => write into txt file as Ansistring, but I think it will be                                                                       changed by string!!!
CloseFile(Myfile);

//read saved BMP string for txt file then  to show BMP
  AssignFile(Myfile,'c:\my.txt');
  ReSet(Myfile);
  ReadLn(Myfile,renderString);
  CloseFile(Myfile);
  Ms := TMemoryStream.Create;
  Ms.Write(PAnsiChar(renderString)^,Length(renderString));
  MS.Seek(0,soFromBeginning);
  Image2.Picture.Bitmap.LoadFromStream(MS);=> raise error: Bitmap image is not vaild!!!
  Ms.Free;

Thanks for Any suggestion

Mitchell Hu 




Replies:
Posted By: tfrost
Date Posted: 09 Dec 14 at 12:08PM
My suggestion is to stop using strings!  Render the page to stream instead, either direct to a filestream which you can create as a BMP file, or to a memorystream which you can either SaveToFile or load directly into a bitmap.  This will fix your problem; it works for me.


Posted By: Mitchell Hu
Date Posted: 09 Dec 14 at 3:15PM
Hi  tfrost:
   thanks for your replay!
 First, I should tell about my requirement! 
 There are  many PDF files ( name card size), user will pick up about 68 pdfs for make a new combine pdf file which size is about A1, so, in ui design , I set up a Pdf list for user pick up , once user pick up a PDF , program use RenderPageToStram let it became a BMP file and show it in TImage component
But, there are two problems:
1. the more big size of PDF, the  more time cost by Render, that means render 68 pdfs will cost more then 1 and half mins, user usually has thousands pdfs , it is unacceptable!!!
2. If user repeat click the 68 PDFs, the program will render those pdf again and again!!!
So, I thinks the only way to deal the requirement, is each PDF only Render to bmp once,  after the Redner the BMP should be stroe in some kind stroe container! and my client do accept save BMP files in a directory for some reason that it is can not ensure user maybe update/delete it!!! so, I think RenderPageToString is the best practice for rendering  PDFs to BMPs, and save each BMP string into a text file at very begging! later, once user pick up a PDF , program can get the PDF's BMP String form saved txt file, and write the string into stram, so the BMP will show in a TImage very soon, because skip rendering process !!!

 


Posted By: tfrost
Date Posted: 09 Dec 14 at 7:02PM
My advice to avoid strings was solely to avoid the issues of string conversions, Unicode, and code pages which are clearly causing your problem.  It does not change your workflow in any way.

If you render each one to stream (in BMP format as would have been in the string) and then savetofile the stream, you will end up with a file that contains exactly what you hoped to find there.  You can even name the file with a .TXT extension if you wish, though that would give its own problems.  Or use PMB instead of BMP if it has to be disguised, and only you will read it.  Or save the BMP content in a database blob.  It will contain the same content as the string but it will work, and will be faster, too.  Do try it!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk