Ejemplo 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);
}
Ejemplo n.º 2
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_);
}
JNIEXPORT void JNICALL Java_com_draekko_libharu_PdfImage_constructJpeg
  (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_LoadJpegImageFromFile(current_pdf, file_str);
    set_HPDF_Image(env, obj, image);

    env->ReleaseStringUTFChars(fileName, file_str);
    haru_clear_error_handler();
}
Ejemplo n.º 4
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);

}
Ejemplo n.º 5
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;
	}
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:    drawJpegImageFromFile
 * Signature: (Ljava/lang/String;FFFF)V
 */
JNIEXPORT void JNICALL
Java_org_libharu_PdfPage_drawJpegImageFromFile(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_LoadJpegImageFromFile((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);
}
Ejemplo n.º 8
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_);
}