Hi I work with DebenuPDFLibraryDLL1212, Visual Studio on Windows 10 Pro. I want to render pdf in a wpf image control.
I tried with :
internal Bitmap GetDAImage( int page, float resolution = 150 ) { if ( _isOK ) // license OK { // on ouvre le fichier _pdfInHandle = _dpl.DAOpenFileReadOnly( pdfIn, null );
// on récupère le nombre de page du fichier _pdfPageCount = _dpl.DAGetPageCount( _pdfInHandle ); // on récupère la référence de la page du fichier int pageRefNo = _dpl.DAFindPage( _pdfInHandle, page );
int Zoom = 72; _pageWidth = _dpl.DAGetPageWidth( _pdfInHandle, pageRefNo ) / Zoom; ******** NOT OK _pageHeight = _dpl.DAGetPageHeight( _pdfInHandle, pageRefNo ) / Zoom; ******** NOT OK
try { // on calcule les dimensions de l'image b = new Bitmap( Convert.ToInt32( _pageWidth * resolution ), Convert.ToInt32( _pageHeight * resolution ) );
// on dessine l'image using ( Graphics g = Graphics.FromImage( b ) ) { //int dc = (int)g.GetHdc(); //_dpl.DARenderPageToDC( _pdfInHandle, pageRefNo, resolution, dc ); //g.ReleaseHdc( (IntPtr)dc );
IntPtr dc = g.GetHdc(); _dpl.DARenderPageToDC( _pdfInHandle, pageRefNo, resolution, dc ); g.ReleaseHdc( dc ); } } catch { } }
return b; }
I convert the bitmap to BitmapImage.
It's ok but 1 file has a problem (it came from Ricoh Aficio multifuntion). It's a A4 format, and with pdf readers, I see it as A4 (no landscape) 21x29.7cm document. But _dpl.DAGetPageWidth and _dpl.DAGetPageHeight give me wrong values: 29.7x21cm, not 21x29.7cm, so in my image, document is truncated in the bottom. I inserted a new function in the beginning of my GetDAImage, to get real dimensions of the page :
internal int LoadPdf( string fileName ) { _fileName = fileName;
if ( _isOK ) // license OK { // on charge le fichier à copier _dpl.LoadFromFile( fileName, null ); _dpl.SelectedDocument();
// on redimensionne en A4 _dpl.SetPageSize( "A4" ); // on définit l'unité de mesure ( 1/72 inches per unit ) _dpl.SetMeasurementUnits( 0 ); // on définit l'origine en haut à gauche _dpl.SetOrigin( 1 );
// on récupère le nombre de page du fichier _pdfPageCount = _dpl.PageCount();
// on récupère les dimensions du document _pageWidth = _dpl.PageWidth(); ******** OK _pageHeight = _dpl.PageHeight(); ******** OK int rotation = _dpl.PageRotation(); int test = _dpl.GetOrigin(); }
return _pdfPageCount; }
Now it's ok. So I tried without DA functions to avoid 2 openings :
internal Bitmap GetImage( int page, float resolution = 150 ) { Bitmap b = null; LoadPdf("myfile");
if ( _isOK ) // license OK { // on fait les vérification de la plages des pages if ( page > _pdfPageCount ) return null;
int Zoom = 72; _pageWidth /= Zoom; _pageHeight /= Zoom;
try { // on calcule les dimensions de l'image b = new Bitmap( Convert.ToInt32( _pageWidth * resolution ), Convert.ToInt32( _pageHeight * resolution ) );
// on dessine l'image using ( Graphics g = Graphics.FromImage( b ) ) { //int dc = (int)g.GetHdc(); //_dpl.DARenderPageToDC( _pdfInHandle, pageRefNo, resolution, dc ); //g.ReleaseHdc( (IntPtr)dc );
IntPtr dc = g.GetHdc(); _dpl.RenderPageToDC( resolution, page, dc ); g.ReleaseHdc( dc ); } } catch { } }
return b; }
And now I have got a new problem : my left and top of the document is drawn with an surprising origin in my image control: left = +200 top = -200 so I can see only the left bottom of my document.
The file seems to have a rotation of 270 with 21x29.7cm dimensions. How can I fix this problem ?
Regards
Raph
|