try to insert an image to a PDF page, but failed

1,291 views
Skip to first unread message

Steven Lee

unread,
Jul 15, 2014, 11:52:26 PM7/15/14
to pdf...@googlegroups.com
Here is an example, I just found it does not call ProcessImage method at all, any wrong with my code, thanks.

FPDF_InitLibrary(NULL);
FPDF_DOCUMENT doc=FPDF_LoadDocument("d:\\PDFCreator_op.pdf",NULL);
FPDF_DOCUMENT newDoc=FPDF_CreateNewDocument();

int page_count = FPDF_GetPageCount(doc);
for (int i = 0; i < page_count; ++i) {
FPDF_PAGE page = FPDF_LoadPage(doc, i);
int width = static_cast<int>(FPDF_GetPageWidth(page));
int height = static_cast<int>(FPDF_GetPageHeight(page));
FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);

FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);

FPDF_PAGE output_page= FPDFPage_New(newDoc, i, width, height);

double ratio_x=1;
double ratio_y=1;
  // Add the bitmap to an image object and add the image object to the output page.
FPDF_PAGEOBJECT img_obj = FPDFPageObj_NewImgeObj(newDoc);
FPDFImageObj_SetBitmap(&output_page, 1, img_obj, bitmap);
FPDFImageObj_SetMatrix(img_obj, ratio_x, 0, 0, ratio_y, 0, 0);
FPDFPage_InsertObject(output_page, img_obj);
FPDFPage_GenerateContent(output_page);
//CPDF_PageContentGenerate tmp((CPDF_Page*)output_page);
//tmp.InsertPageObject((CPDF_PageObject*)img_obj);
//tmp.GenerateContent();
FPDFBitmap_Destroy(bitmap);
FPDF_ClosePage(page);
}

saveFile(newDoc,"d:\\zp11.pdf");

FPDF_CloseDocument(doc);
FPDF_CloseDocument(newDoc);
FPDF_DestroyLibrary();

richard....@gmail.com

unread,
Jul 21, 2014, 6:24:07 PM7/21/14
to pdf...@googlegroups.com
Can you see the image object when you view the raw PDF?

Steven Lee

unread,
Jul 21, 2014, 10:16:27 PM7/21/14
to pdf...@googlegroups.com

no. 
only in this way, image object can be insert, but can not show in pdf, seems need more job.
//CPDF_PageContentGenerate tmp((CPDF_Page*)output_page);
//tmp.InsertPageObject((CPDF_PageObject*)img_obj);
//tmp.GenerateContent();

Mihai Matei

unread,
Jan 8, 2015, 11:51:49 AM1/8/15
to pdf...@googlegroups.com
Hello

This works for me...you need to resize the image
FPDF_PAGEOBJECT pdfImage = FPDFPageObj_NewImgeObj(mPdfiumDoc);

    FPDF_BITMAP bitmap = FPDFBitmap_CreateEx(img.width(), img.height(), FPDFBitmap_BGRA, img.bits(), img.bytesPerLine());
    FPDFImageObj_SetBitmap(&mCurrentPage, 1, pdfImage, bitmap);

    FPDFPageObj_Transform(pdfImage, img.width(), 0, 0, img.height(), pos.x(), mCurrentHeight - pos.y() - img.height());

    FPDFPage_InsertObject(mCurrentPage, pdfImage);

and finally do not forget generateContent at the end.
Reply all
Reply to author
Forward
0 new messages