Print Page | Close Window

PDF file crashes QuickPDF

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=3395
Printed Date: 02 May 24 at 5:02PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: PDF file crashes QuickPDF
Posted By: RossFaneuf
Subject: PDF file crashes QuickPDF
Date Posted: 07 Oct 16 at 3:48AM
The attached .pdf file crashes the following c# method. Using QuickPDF 13.11. The method attempts to construct a thumbnail image based on the first page in the .pdf file. The sequence is:
  - Method succeeds once
  - When called a second time with the same .pdf file QuickPDF dies in unmanaged code.

The code which attempts to create the image:

try
{
                PDFLibrary quickPDFLibrary = PDFTextExtractor.GetQuickPdfLibrary();

if (quickPDFLibrary.LoadFromString(pdfBytes, "") == 1) // Success
{
int docID = quickPDFLibrary.SelectedDocument(); // NEEDED per Debenu expert

                    //Declare as a double so that precision is not lost when determining the DPI based on the destination size. 
                    //If the pdf is large enough (PageWidth or PageHeight having a value greater than 7200) the result can have a value of 0.
                    //If this occurs then thumbnail will not be created.
double renderDpi = 72; 

// SPR 9989: Advance to first page, and compute resolution which will roughly match rendered bitmap to desired size.
quickPDFLibrary.SelectPage(1);


double pw = quickPDFLibrary.PageWidth() / renderDpi;
double ph = quickPDFLibrary.PageHeight() / renderDpi;

//Setup the render DPI based on the destination size
renderDpi = Math.Min(destSize / pw, destSize / ph); // fit both

int width = (int)(renderDpi * pw);
int height = (int)(renderDpi * ph);

Bitmap bitmap = new Bitmap(width, height);

using (Graphics g = Graphics.FromImage(bitmap))
{

// Render page into the bitmap
IntPtr dc = g.GetHdc();
quickPDFLibrary.RenderPageToDC(renderDpi, 1, dc);
g.ReleaseHdc(dc);

// Draw 1-pixel black border onto the bitmap
Pen bp = new Pen(Brushes.Silver, 1);
g.DrawLine(bp, 0, 0, width - 1, 0);
g.DrawLine(bp, width - 1, 0, width - 1, height - 1);
g.DrawLine(bp, width - 1, height - 1, 0, height - 1);
g.DrawLine(bp, 0, height - 1, 0, 0);
}

using (MemoryStream outStream2 = new MemoryStream())
{
//height greater then width
   double thmbHeight;
double thmbWidth;

//Get the dimension of the thumbnail
                        GetResizeImageDimensions(bitmap, destSize, out thmbHeight, out thmbWidth);

// Generate thumbnail image
Image thumbNailImg = bitmap.GetThumbnailImage(Convert.ToInt32(thmbWidth), Convert.ToInt32(thmbHeight), ThumbnailCallback, IntPtr.Zero);
thumbNailImg.Save(outStream2, ImageFormat.Png);
thumbNailImg.Dispose();
// Remove document from memory
quickPDFLibrary.RemoveDocument(docID);
return outStream2.ToArray();
}
}

}





Replies:
Posted By: RossFaneuf
Date Posted: 07 Oct 16 at 3:52AM
Need help attaching the .pdf file to this topic. How is this done???


Posted By: Ingo
Date Posted: 09 Oct 16 at 5:29PM
Hi Ross,

on which codeline you've determined the crash?
You can't attach the file here.
You should upload it anywhere with a free filehoster.
What about the security settings of this pdf... is there a user password... is the file write protected...?
If the pdf is already encrypted you should decrypt it first.

Cheers and welcome here,
Ingo



-------------
Cheers,
Ingo



Posted By: RossFaneuf
Date Posted: 18 Oct 16 at 8:02PM
The file is readable, not encrypted, and has no password. It appears to 64 pages of scanned images of a technical paper published in 1953. The file is readable with Adobe Reader.

Internally, the file can be successfully read once with the supplied code, but a crash occurs on the second attempt, and the crash is in unmanaged code.

The file is available in DropBox at: file:///C:/Users/ross.INMAGICINC/Dropbox/Leopold_and_Maddock_1953_Hydraulic_Geometry.pdf



Posted By: Ingo
Date Posted: 19 Oct 16 at 7:51PM
Hi Ross,

only a quick short answer...
The problem will be in your code.
I've downloaded a simmilar pdf (The Hydraulic Geometry of Stream Channels and Some Physiographic Implications.pdf) from the same authors. 64 scanned pages and text, too. 
It works fine with my app made by QuickPDF.
If you're writing about "unmanaged code" i think wrong string handling could be your problem?
Which is your dev-language... C#?
Perhaps you're using the wrong dll-version for your language?
You should read the descriptions on the debenu-page which dll shall be used by which language...



-------------
Cheers,
Ingo



Posted By: RossFaneuf
Date Posted: 19 Oct 16 at 11:45PM
Please read the first post in this thread, where the c# code in question is provided.

The crash occurs the second time the following line of code is executed for this pdf file:
quickPDFLibrary.RenderPageToDC(renderDpi, 1, dc)

I realize that including all the code in the try..catch is providing too much information, for which I apologize. Note that near the end of the code the following line attempts to free everything associated with the open document. Should there be additional calls first to remove something else created by the RenderPageToDC() method before calling this?

quickPDFLibrary.RemoveDocument(docID);

I am certain that:
  - there are no string handling issues
  - we are using the correct library - in fact, I have tested the code against this pdf file with 2 versions, 11.14 and 13.11
  - This code has worked correctly for 1000s of pdf files, including some strange ones.
  - I assume the fact that the crash is in unmanaged code is because some of the QuickPDF internals are implemented using unmanaged code (C++?)

If you would prefer to discuss this using Skype, send an invitation to ross.faneuf. I can, if you wish, share screens for a debugging session.


Posted By: Ingo
Date Posted: 20 Oct 16 at 8:45AM
Hi Ross,


QuickPDF wasn't written in C++ - it's 100% pure object pascal code (Delphi).
There are only less external calls inside and these calls are for external render engines like gdiplus, pdfium and cairo.
It depends on your used functionalities if an external call to a render engine will be activated.
gdiplus can be a problem 'cause not all windows versions still have it in the standard installation.
QuickPDF offers functionality to manage the calls/task to these render engines - please check the QuickPDF online reference.
To free memory there's not only a FreeDocument - you can free instances and many more (check the online reference).

If your code works with 99% other pdf-files and fails only with less pdfs you should debug/deal with similar pdfs.
Is your code working with the file i've mentioned (you can try google to get it)?
You should deal with the dpi-value.
What's in "dc" after "g.GetHdc()"?
What's in "renderDpi"?
You can reduce/remove code to check if the file is loaded in a proper way first.
You can check the returning value of each function if the function/step is okay.

Regarding your suggestions (skype, debug session, ...):
This is only a user-community here.
I like to give quick hints, post a code snippet out of my personal codings, post a link to a kb-ressource, ...
But not more - it's my free time i'm spending here ;-)



-------------
Cheers,
Ingo




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