|
|
#include "public/cpp/fpdf_scopers.h"
|
#include "public/fpdf_ext.h"
|
#include "samples/pdfium_test_write_helper.h"
|
#include "testing/test_support.h"
|
|
|
|
|
bool RenderPage(const std::string& name,
|
FPDF_DOCUMENT doc,
|
const int page_index)
|
{
|
|
FPDF_PAGE page = FPDF_LoadPage ( doc, page_index );
|
|
if ( ! page )
|
{
|
printf ( "FPDF_LoadPage failed\n" );
|
return false;
|
}
|
|
double scale = .5;
|
|
auto width = static_cast<int>(FPDF_GetPageWidth ( page ) * scale);
|
auto height = static_cast<int>(FPDF_GetPageHeight ( page ) * scale);
|
int alpha = FPDFPage_HasTransparency ( page ) ? 1 : 0;
|
|
|
ScopedFPDFBitmap bitmap ( FPDFBitmap_Create ( width, height, alpha ));
|
|
if (bitmap)
|
{
|
FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
|
FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
|
|
FPDF_RenderPageBitmap(bitmap.get(), page, 0, 0, width, height, 0, FPDF_ANNOT);
|
|
|
int stride = FPDFBitmap_GetStride(bitmap.get());
|
const char* buffer =
|
reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get()));
|
|
std::string image_file_name;
|
image_file_name = WritePng(name.c_str(), page_index, buffer, stride, width, height);
|
}
|
|
FPDF_ClosePage ( page );
|
page = NULL;
|
|
|
return !!bitmap;
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RenderPdf ( const char *name )
|
{
|
|
#if 0
|
size_t file_length = 0;
|
std::unique_ptr<char, pdfium::FreeDeleter> file_contents = GetFileContents ( name, &file_length );
|
|
TestLoader loader ( file_contents.get(), file_length );
|
|
FPDF_FILEACCESS file_access = {};
|
file_access.m_FileLen = static_cast<unsigned long>( file_length );
|
file_access.m_GetBlock = TestLoader::GetBlock;
|
file_access.m_Param = &loader;
|
|
FPDF_DOCUMENT doc = FPDF_LoadCustomDocument ( &file_access, nullptr );
|
#else
|
|
FPDF_DOCUMENT doc = FPDF_LoadDocument ( name, NULL );
|
#endif
|
|
|
if ( ! doc )
|
return;
|
|
|
int page_count = FPDF_GetPageCount ( doc );
|
|
int rendered_pages = 0;
|
int bad_pages = 0;
|
|
for (int i = 0; i < page_count; ++i)
|
{
|
if ( RenderPage ( name, doc, i ))
|
{
|
++rendered_pages;
|
}
|
else
|
{
|
++bad_pages;
|
}
|
}
|
|
fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
|
if (bad_pages)
|
fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
|
|
|
FPDF_CloseDocument(doc);
|
|
}
|
|
|
|
|
|
|
|
int main ( int argc, const char* argv[] )
|
{
|
FPDF_InitLibrary ( );
|
|
char fname[] = "bad_pg_10.pdf";
|
fprintf(stderr, "Rendering PDF file %s.\n", fname );
|
|
RenderPdf ( fname );
|
|
FPDF_DestroyLibrary();
|
|
return 0;
|
}
|
|
|