int main (int argc, char **argv) { HPDF_Doc pdf; HPDF_Image image; HPDF_Stream stream; HPDF_UINT iw; HPDF_UINT ih; HPDF_UINT bits_per_comp; const char *cs; if (argc < 2) { printf ("usage: make_rawimage <in-file-name> <out-file-name>\n"); return 1; } pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } /* error-handler */ if (setjmp(env)) { HPDF_Free (pdf); return 1; } /* load image file. */ image = HPDF_LoadPngImageFromFile (pdf, argv[1]); iw = HPDF_Image_GetWidth (image); ih = HPDF_Image_GetHeight (image); bits_per_comp = HPDF_Image_GetBitsPerComponent (image); cs = HPDF_Image_GetColorSpace (image); printf ("width=%u\n", iw); printf ("height=%u\n", ih); printf ("bits_per_comp=%u\n", bits_per_comp); printf ("color_space=%s\n", cs); /* save raw-data to file */ stream = HPDF_FileWriter_New (pdf->mmgr, argv[2]); if (!stream) printf ("cannot open %s\n", argv[2]); else HPDF_Stream_WriteToStream(image->stream, stream, 0, NULL); HPDF_Stream_Free (stream); /* clean up */ HPDF_Free (pdf); return 0; }
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfImage_constructPng (JNIEnv *env, jobject obj, jobject document, jstring fileName) { haru_setup_error_handler(env, __func__); const char* file_str = env->GetStringUTFChars(fileName, 0); current_pdf = get_HPDF_Doc(env, document); current_page = HPDF_GetCurrentPage(current_pdf); HPDF_Image image = HPDF_LoadPngImageFromFile(current_pdf, file_str); set_HPDF_Image(env, obj, image); env->ReleaseStringUTFChars(fileName, file_str); haru_clear_error_handler(); }
ML_START_NAMESPACE //---------------------------------------------------------------------------------- mlPDF::IMAGE PDFGenerator::pdfDoc_LoadImageFromFile(std::string filename) { mlPDF::IMAGE newImage = NULL; if (pdfDocument) { const unsigned int filenameLength = static_cast<unsigned int>(filename.length()); std::string last4 = ""; if ((filenameLength > 4) && (mlPDF::fileExists(filename))) { last4 = filename.substr(filenameLength - 4, 4); mlPDF::PDFTools::stringLower(last4); if (last4 == ".png") { newImage = HPDF_LoadPngImageFromFile(pdfDocument, filename.c_str()); } else if ((last4 == ".jpg") || (last4 == ".jpeg")) { newImage = HPDF_LoadJpegImageFromFile(pdfDocument, filename.c_str()); } if (newImage) { pdfDocImages.push_back(newImage); } else { _handleError("pdfDoc_LoadImageFromFile(std::string filename)"); } } // if ( (filenameLength > 4) && (fileExists(filename)) ) } // if (pdfDocument) return newImage; }
/* * Class: org_libharu_PdfPage * Method: drawPngImageFromFile * Signature: (Ljava/lang/String;FFFF)V */ JNIEXPORT void JNICALL Java_org_libharu_PdfPage_drawPngImageFromFile(JNIEnv *env, jobject obj, jstring path, jfloat x, jfloat y, jfloat width, jfloat height) { jint page, pdf; const char* filename; /* The path of the file to load the image from */ /* Get mHPDFPagePointer */ page = (*env)->GetIntField(env, obj, mHPDFPagePointer); /* Get mParentHPDFDocPointer */ pdf = (*env)->GetIntField(env, obj, mParentHPDFDocPointer); /* Get the filename as a native char array */ filename = (*env)->GetStringUTFChars(env, path, NULL); /* Load an HPDF_Image from the file */ HPDF_Image image = HPDF_LoadPngImageFromFile((HPDF_Doc) pdf, filename); /* Actually draw the image */ HPDF_Page_DrawImage((HPDF_Page) page, image, (HPDF_REAL) x, (HPDF_REAL) y, (HPDF_REAL) width, (HPDF_REAL) height); /* Release (free) the native char array */ (*env)->ReleaseStringUTFChars(env, path, filename); }
void draw_image (HPDF_Doc pdf, const char *filename, float x, float y, const char *text) { #ifdef __WIN32__ const char* FILE_SEPARATOR = "\\"; #else const char* FILE_SEPARATOR = "/"; #endif char filename1[255]; HPDF_Page page = HPDF_GetCurrentPage (pdf); HPDF_Image image; strcpy(filename1, "pngsuite"); strcat(filename1, FILE_SEPARATOR); strcat(filename1, filename); image = HPDF_LoadPngImageFromFile (pdf, filename1); /* Draw image to the canvas. */ HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image), HPDF_Image_GetHeight (image)); /* Print the text. */ HPDF_Page_BeginText (page); HPDF_Page_SetTextLeading (page, 16); HPDF_Page_MoveTextPos (page, x, y); HPDF_Page_ShowTextNextLine (page, filename); HPDF_Page_ShowTextNextLine (page, text); HPDF_Page_EndText (page); }
int main (int /* argc */, char **argv) { const char *page_title = "Font Demo"; HPDF_Doc pdf; char fname[256]; HPDF_Page page; HPDF_Font def_font; HPDF_REAL tw; HPDF_REAL height; HPDF_REAL width; HPDF_UINT i; HPDF_Image image; double x; double y; double iw; double ih; HPDF_Stream stream; strcpy (fname, argv[0]); strcat (fname, ".pdf"); pdf = HPDF_New (error_handler, NULL); if (!pdf) { printf ("error: cannot create PdfDoc object\n"); return 1; } if (setjmp(env)) { HPDF_Free (pdf); return 1; } /* Add a new page object. */ page = HPDF_AddPage (pdf); height = HPDF_Page_GetHeight (page); width = HPDF_Page_GetWidth (page); /* Print the lines of the page. */ HPDF_Page_SetLineWidth (page, 1); HPDF_Page_Rectangle (page, 50, 50, width - 100, height - 110); HPDF_Page_Stroke (page); /* Print the title of the page (with positioning center). */ def_font = HPDF_GetFont (pdf, "Helvetica", NULL); HPDF_Page_SetFontAndSize (page, def_font, 24); tw = HPDF_Page_TextWidth (page, page_title); HPDF_Page_BeginText (page); HPDF_Page_TextOut (page, (width - tw) / 2, height - 50, page_title); HPDF_Page_EndText (page); /* output subtitle. */ HPDF_Page_BeginText (page); HPDF_Page_SetFontAndSize (page, def_font, 16); HPDF_Page_TextOut (page, 60, height - 80, "<Standerd Type1 fonts samples>"); HPDF_Page_EndText (page); HPDF_Page_BeginText (page); HPDF_Page_MoveTextPos (page, 60, height - 105); i = 0; while (font_list[i]) { const char* samp_text = "abcdefgABCDEFG12345!#$%&+-@?"; HPDF_Font font = HPDF_GetFont (pdf, font_list[i], NULL); /* print a label of text */ HPDF_Page_SetFontAndSize (page, def_font, 9); HPDF_Page_ShowText (page, font_list[i]); HPDF_Page_MoveTextPos (page, 0, -18); /* print a sample text. */ HPDF_Page_SetFontAndSize (page, font, 20); HPDF_Page_ShowText (page, samp_text); HPDF_Page_MoveTextPos (page, 0, -20); i++; } HPDF_Page_EndText (page); /* Add a new page object. */ page = HPDF_AddPage (pdf); height = HPDF_Page_GetHeight (page); width = HPDF_Page_GetWidth (page); /* load image file. */ image = HPDF_LoadPngImageFromFile (pdf, "test.png"); x = 100; y = 100; iw = HPDF_Image_GetWidth (image); ih = HPDF_Image_GetHeight (image); /* Draw image to the canvas. (normal-mode with actual size.)*/ HPDF_Page_DrawImage (page, image, x, y, iw, ih); HPDF_SaveToFile (pdf, fname); /* write something via zlib */ stream = HPDF_FileWriter_New( pdf->mmgr, "test2.raw" ); HPDF_Stream_WriteToStream( image->stream, stream, HPDF_STREAM_FILTER_FLATE_DECODE, NULL ); HPDF_Stream_Free (stream); /* clean up */ HPDF_Free (pdf); return 0; }