Exemplo n.º 1
0
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, "images");
    strcat(filename1, FILE_SEPARATOR);
    strcat(filename1, filename);

    image = HPDF_LoadJpegImageFromFile (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);
}
Exemplo n.º 2
0
bool pdf_plot_bitmap_tile(int x, int y, int width, int height,
		struct bitmap *bitmap, colour bg,
  		bitmap_flags_t flags)
{
	HPDF_Image image;
	HPDF_REAL current_x, current_y ;
	HPDF_REAL max_width, max_height;

#ifdef PDF_DEBUG
	NSLOG(netsurf, INFO, "%d %d %d %d %p 0x%x", x, y, width, height,
	      bitmap, bg);
#endif
 	if (width == 0 || height == 0)
 		return true;

	apply_clip_and_mode(false, NS_TRANSPARENT, NS_TRANSPARENT, 0., DashPattern_eNone);

	image = pdf_extract_image(bitmap);
	if (!image)
		return false;

	/*The position of the next tile*/
	max_width =  (flags & BITMAPF_REPEAT_X) ? page_width : width;
	max_height = (flags & BITMAPF_REPEAT_Y) ? page_height : height;

	for (current_y = 0; current_y < max_height; current_y += height)
		for (current_x = 0; current_x < max_width; current_x += width)
			HPDF_Page_DrawImage(pdf_page, image,
					current_x + x,
					page_height - current_y - y - height,
					width, height);

	return true;
}
/*
 * Class:     org_libharu_PdfPage
 * Method:    drawPngImage
 * Signature: ([BFFFF)V
 */
JNIEXPORT void JNICALL
Java_org_libharu_PdfPage_drawPngImage(JNIEnv *env, jobject obj, jbyteArray imageData, jfloat x,
        jfloat y, jfloat width, jfloat height) {
    jint page, pdf;
    jbyte* buffer; /* The image data as a jbyte[] (really signed char[]) */
    jsize len; /* The number of elements in the image byte array */

    /* Get mHPDFPagePointer */
    page = (*env)->GetIntField(env, obj, mHPDFPagePointer);
    /* Get mParentHPDFDocPointer */
    pdf = (*env)->GetIntField(env, obj, mParentHPDFDocPointer);

    /* Get the image data as a native byte array */
    buffer = (*env)->GetByteArrayElements(env, imageData, NULL);
    /* Get the number of elements in the image byte array */
    len = (*env)->GetArrayLength(env, imageData);

    /* Load an HPDF_Image from the image byte array */
    HPDF_Image image = HPDF_LoadPngImageFromMem((HPDF_Doc) pdf, (HPDF_BYTE*) buffer,
            (HPDF_UINT) (len * sizeof(jbyte)));

    /* 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 byte array */
    (*env)->ReleaseByteArrayElements(env, imageData, buffer, JNI_ABORT);
}
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfImage_drawImage
  (JNIEnv *env, jobject obj, jfloat x, jfloat y, jfloat width, jfloat height) {
  
    haru_setup_error_handler(env, __func__);
    HPDF_Image image = get_HPDF_Image(env, obj); 
    HPDF_Page page = HPDF_GetCurrentPage(current_pdf); 
    HPDF_Page_DrawImage(page, image, x, y, width, height);
    haru_clear_error_handler();
}
Exemplo n.º 5
0
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = 0;

  if (Uri::isDataUri(imgUrl)) {
    Uri::Uri uri = Uri::parseDataUri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)uri.data.c_str(), 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)uri.data.c_str(), 
				      uri.data.size()); 
  } else {
    std::string mimeType = Image::identifyImageFileMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}
Exemplo n.º 6
0
JNIEXPORT void JNICALL Java_org_libharu_Page_image
  (JNIEnv *env, jobject obj_page, jobject obj_image, 
   jfloat x, jfloat y, jfloat width, jfloat height) 
{
  haru_setup_error_handler(env, __func__);
  HPDF_Page page = get_HPDF_Page(env, obj_page); 
  HPDF_Image image = get_HPDF_Image(env, obj_image);
  HPDF_Page_DrawImage(page, image, x, y, width, height);
  haru_clear_error_handler();
}
void PDFGenerator::pdfDoc_AddImage(float x, float y, float width, float height, mlPDF::IMAGE image, bool ignoreMargins)
{
  if ((pdfDocCurrentPage) && (image))
  {
    HPDF_Rect imageRect = _getPageRect(x, y, width, height, ignoreMargins);

    HPDF_STATUS result = HPDF_Page_DrawImage(pdfDocCurrentPage, image, imageRect.left, imageRect.bottom, width, height);
  
    if (result != HPDF_OK)
    {
      _handleError("pdfDoc_AddImage(float x, float y, float width, float height, mlPDF::IMAGE image, bool ignoreMargins)");
    }
  }
}
Exemplo n.º 8
0
void hpdf_doc::place_image(int x0, int y0, int x1, int y1, int ev, wstring filename)
{
	string filepath = WstringToString(filename);

	HPDF_Image img = HPDF_LoadJpegImageFromFile(h_pdf, filepath.c_str());
	if (img == NULL) return;

	HPDF_REAL fpx = ((HPDF_REAL)x0 * 72.0 / 1000.0);
	HPDF_REAL fpy = ((HPDF_REAL)y0 * 72.0 / 1000.0);
	HPDF_REAL fwidth = ev * ((HPDF_REAL)(x1 - x0) * 72.0 / 1000.0);
	HPDF_REAL fheight = ev * ((HPDF_REAL)(y1 - y0) * 72.0 / 1000.0);
	HPDF_REAL f_ypos = f_length - fpy - fheight;

	HPDF_Page_DrawImage(h_current_page, img, fpx, f_ypos, fwidth, fheight);

}
Exemplo n.º 9
0
	bool addJpgPage(HPDF_Doc pdf, char const* jpgname) {
	    HPDF_Page 	page = HPDF_AddPage (pdf);
		if (!page)
			return false;
	    HPDF_Image	image	= HPDF_LoadJpegImageFromFile (pdf, jpgname);
		if (!image) {
			printf("load error %s\n", jpgname);
			return false;
		}
		HPDF_REAL img_w = HPDF_REAL(HPDF_Image_GetWidth(image));
		HPDF_REAL img_h = HPDF_REAL(HPDF_Image_GetHeight(image));
	    HPDF_Page_SetWidth (page, img_w);
	    HPDF_Page_SetHeight(page, img_h);
	    HPDF_Page_DrawImage (page, image, 0, 0, img_w, img_h);
		return true;
	}
void PDFGenerator::pdfDoc_AddImage(float x, float y, float width, float height, std::string imageFilename, bool ignoreMargins)
{
  if (pdfDocCurrentPage)
  {
    HPDF_Rect imageRect = _getPageRect(x, y, width, height, ignoreMargins);

    HPDF_Image image = pdfDoc_LoadImageFromFile(imageFilename);

    if (image)
    {
      HPDF_STATUS result = HPDF_Page_DrawImage(pdfDocCurrentPage, image, imageRect.left, imageRect.bottom, width, height);

      if (result != HPDF_OK)
      {
        _handleError("pdfDoc_AddImage(float x, float y, float width, float height, std::string imageFilename, bool ignoreMargins)");
      }
    }
  }
}
/*
 * 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);
}
Exemplo n.º 12
0
int main (int argc, char **argv)
{
    HPDF_Doc  pdf;
    HPDF_Font font;
    HPDF_Page page;
    char fname[256];
    HPDF_Image image;
	char *png_image_data;
	FILE *fp;

    HPDF_REAL x, y;

    strcpy (fname, argv[0]);
    strcat (fname, ".pdf");

    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;
    }

    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

    /* create default-font */
    font = HPDF_GetFont (pdf, "Helvetica", NULL);

    /* add a new page object. */
    page = HPDF_AddPage (pdf);

    HPDF_Page_BeginText (page);
    HPDF_Page_SetFontAndSize (page, font, 20);
    HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
    HPDF_Page_ShowText (page, "RawImageDemo");
    HPDF_Page_EndText (page);

	int n;

	fp = fopen("2.png", "r");
	png_image_data = malloc(1024*1024);
	n = fread(png_image_data, 1, 1024*1024, fp);
	fclose(fp);

    /* load GrayScale raw-image (1bit) file from memory. */
    image = HPDF_LoadPngImageFromMem (pdf, png_image_data, n);

    /* Draw image to the canvas. (normal-mode with actual size.)*/
    HPDF_Page_DrawImage (page, image, 20, 20, 1024, 768);

	free(png_image_data);

    /* save the document to a file */
    HPDF_SaveToFile (pdf, fname);

    /* clean up */
    HPDF_Free (pdf);

    return 0;
}
Exemplo n.º 13
0
void WPdfImage::drawImage(const WRectF& rect, const std::string& imgUrl,
			  int imgWidth, int imgHeight,
			  const WRectF& srect)
{
  HPDF_Image img = nullptr;

  if (DataUri::isDataUri(imgUrl)) {
#define HAVE_LOAD_FROM_MEM HPDF_MAJOR_VERSION > 2 || (HPDF_MAJOR_VERSION == 2 && (HPDF_MINOR_VERSION >= 2))

#if HAVE_LOAD_FROM_MEM
    DataUri uri(imgUrl);
    if ("image/png" == uri.mimeType)
      img = HPDF_LoadPngImageFromMem(pdf_, 
				     (HPDF_BYTE*)&uri.data[0], 
				     uri.data.size()); 
    else if ("image/jpeg" == uri.mimeType)
      img = HPDF_LoadJpegImageFromMem(pdf_, 
				      (HPDF_BYTE*)&uri.data[0], 
				      uri.data.size());
#else
      LOG_ERROR("drawImage: data URI support requires libharu 2.2.0 or later");
#endif
  } else {
    std::string mimeType = ImageUtils::identifyMimeType(imgUrl);
    if ("image/png" == mimeType)
      img = HPDF_LoadPngImageFromFile2(pdf_, imgUrl.c_str());
    else if ("image/jpeg" == mimeType)
      img = HPDF_LoadJpegImageFromFile(pdf_, imgUrl.c_str());
  } 

  if (!img)
    throw WException("WPdfImage::drawImage(): cannot load image: " + imgUrl);

  double x = rect.x();
  double y = rect.y();
  double width = rect.width();
  double height = rect.height();

  HPDF_Page_GSave(page_);

  if (srect.x() != 0
      || srect.y() != 0
      || srect.width() != imgWidth
      || srect.height() != imgHeight) {
    double scaleX = width / imgWidth;
    double scaleY = height / imgHeight;

    x -= srect.x() * scaleX;
    y -= srect.y() * scaleY;
    width *= scaleX;
    height *= scaleY;

    HPDF_Page_Rectangle(page_, rect.x(), rect.y(), rect.width(), rect.height());
    HPDF_Page_Clip(page_);
  }

  HPDF_Page_Concat(page_, 1, 0, 0, -1, x, y + height); // revert upside-down

  HPDF_Page_DrawImage(page_, img, 0, 0, width, height);

  HPDF_Page_GRestore(page_);
}
Exemplo n.º 14
0
int main (int argc, char **argv)
{
    HPDF_Doc  pdf;
    HPDF_Font font;
    HPDF_Page page;
    char fname[256];
    HPDF_Image image;

    HPDF_REAL x, y;

    strcpy (fname, argv[0]);
    strcat (fname, ".pdf");

    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;
    }

    HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);

    /* create default-font */
    font = HPDF_GetFont (pdf, "Helvetica", NULL);

    /* add a new page object. */
    page = HPDF_AddPage (pdf);

    HPDF_Page_SetWidth (page, 172);
    HPDF_Page_SetHeight (page, 80);

    HPDF_Page_BeginText (page);
    HPDF_Page_SetFontAndSize (page, font, 20);
    HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
    HPDF_Page_ShowText (page, "RawImageDemo");
    HPDF_Page_EndText (page);

    /* load RGB raw-image file. */
    #ifndef __WIN32__
    image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_rgb.dat",
            32, 32, HPDF_CS_DEVICE_RGB);
    #else
    image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_rgb.dat",
            32, 32, HPDF_CS_DEVICE_RGB);
    #endif

    x = 20;
    y = 20;

    /* Draw image to the canvas. (normal-mode with actual size.)*/
    HPDF_Page_DrawImage (page, image, x, y, 32, 32);

    /* load GrayScale raw-image file. */
    #ifndef __WIN32__
    image = HPDF_LoadRawImageFromFile (pdf, "rawimage/32_32_gray.dat",
            32, 32, HPDF_CS_DEVICE_GRAY);
    #else
    image = HPDF_LoadRawImageFromFile (pdf, "rawimage\\32_32_gray.dat",
            32, 32, HPDF_CS_DEVICE_GRAY);
    #endif

    x = 70;
    y = 20;

    /* Draw image to the canvas. (normal-mode with actual size.)*/
    HPDF_Page_DrawImage (page, image, x, y, 32, 32);

    /* load GrayScale raw-image (1bit) file from memory. */
    image = HPDF_LoadRawImageFromMem (pdf, RAW_IMAGE_DATA, 32, 32,
                HPDF_CS_DEVICE_GRAY, 1);

    x = 120;
    y = 20;

    /* Draw image to the canvas. (normal-mode with actual size.)*/
    HPDF_Page_DrawImage (page, image, x, y, 32, 32);

    /* save the document to a file */
    HPDF_SaveToFile (pdf, fname);

    /* clean up */
    HPDF_Free (pdf);

    return 0;
}
Exemplo n.º 15
0
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;
}