Introduction
Rendering or reading PDF files can be tricky sometimes because of abnormal PDF resource size and device memory limitation constraint overlapping with each other. The most common problem is having a huge image inside the PDF file that requires a significant amount of memory to be allocated for proper processing.
This problem can arise on mobile devices (e.g. in Xamarin apps), but can also be observed on PCs if you have not enough memory to hold the resources stored in PDF file and there are really big. To solve this problem we introduced the PDF engine setting controlling the resource loading strategy. See the code sample in next section to learn how it works.
Code sample
// open the document and render first page
using (Stream fileStream = File.Open("document.pdf",FileMode.Open))
{
// You can turn on the low memory mode globally:
// EngineSettings.GlobalSettings.MemoryAllocationMode =
// MemoryAllocationMode.ResourcesLowMemory;
// or apply this setting only to the specific file as shown below.
// This mode can be useful in situations where you have PDF files containing huge
// image resources or embedded fonts, and running out of memory because of this.
EngineSettings engineSettings = newEngineSettings()
{
// set the resource size limit to 2MB,
// everything bigger than that will be saved to disk
ResourceSizeLimit = 2097152,
MemoryAllocationMode = MemoryAllocationMode.ResourcesLowMemory
};
using (Document doc = newDocument(fileStream, engineSettings))
{
// render page and save the result
Bitmap bitmap = doc.Pages[0].Render(newResolution(72, 72),
newRenderingSettings());
bitmap.Save("page0.png");
}
}
Conclusion
You can render resource demanding and complex PDF documents using Apitron PDF Rasterizer.NET component, download it from our website and enjoy the top quality PDF rendering results and its easy to use API. Contact us if you need any help or have questions, we guarantee the response within the same business day.
Downloadable version of this article can be found by the following link [PDF].
Downloadable version of this article can be found by the following link [PDF].