예제 #1
0
bool XojPopplerDocument::load(const char * filename, const char * password, GError ** error) {
	XOJ_CHECK_TYPE(XojPopplerDocument);

	PDFDoc * newDoc;
	GooString * filename_g;
	GooString * password_g;

	if (!globalParams) {
		globalParams = new GlobalParams();
	}

	if (!filename) {
		return false;
	}

	password_g = NULL;
	if (password != NULL) {
		if (g_utf8_validate(password, -1, NULL)) {
			gchar *password_latin;

			password_latin = g_convert(password, -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL);
			password_g = new GooString(password_latin);
			g_free(password_latin);
		} else {
			password_g = new GooString(password);
		}
	}

#ifdef G_OS_WIN32
	wchar_t *filenameW;
	int wlen;

	wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);

	filenameW = new WCHAR[wlen];
	if (!filenameW)
	return NULL;

	wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, wlen);

	newDoc = new PDFDoc(filenameW, wlen, password_g, password_g);
	delete filenameW;
#else
	filename_g = new GooString(filename);
	newDoc = new PDFDoc(filename_g, password_g, password_g);
#endif
	delete password_g;

	if (!newDoc->isOk()) {
		int fopen_errno;
		switch (newDoc->getErrorCode()) {
		case errOpenFile:
			// If there was an error opening the file, count it as a G_FILE_ERROR
			// and set the GError parameters accordingly. (this assumes that the
			// only way to get an errOpenFile error is if newDoc was created using
			// a filename and thus fopen was called, which right now is true.
			fopen_errno = newDoc->getFopenErrno();
			g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(fopen_errno), "%s", g_strerror(fopen_errno));
			break;
		case errBadCatalog:
			g_set_error(error, 0, 0, "Failed to read the document catalog");
			break;
		case errDamaged:
			g_set_error(error, 0, 0, "PDF document is damaged");
			break;
		case errEncrypted:
			g_set_error(error, 0, 0, "Document is encrypted");
			break;
		default:
			g_set_error(error, 0, 0, "Failed to load document");
		}

		delete newDoc;
		return false;
	}

	if (this->data) {
		this->data->unreference();
	}
	this->data = new _IntPopplerDocument(newDoc);

	return true;
}
예제 #2
0
int extract_images_from_pdf(char* filename,
                            char* target,
                            char* owner_password,
                            char* user_password,
                            char* range,
                            char* format,
                            int jpg_quality,
                            GBool dump_jpg,
                            GBool tiff_jpg)
{
    if (user_cancelled)
        return gpret_user_cancelled;

    // load config
    xpdf_rc xrc;

    // open file
    xpdf_doc xdoc(filename, owner_password, user_password);
    PDFDoc* doc = xdoc.get_doc();
    if (!doc->isOk())
        return doc->getErrorCode() == errEncrypted ?
               gpret_pdf_encrypted :
               gpret_cant_open_pdf;

    // check for copy permission
    // if (!doc->okToCopy())
    //	 return gpret_dont_allow_copy;

    // get page range
    page_range range_list(range);
    if (*range == '\0')
    {
        range_list.add_item(range_item(1, doc->getNumPages()));
    }

    if (user_cancelled)
        return gpret_user_cancelled;

    // write image files
    fi_loader fi;
    int progress = 0;
    image_extractor img_out(target, dump_jpg, format, jpg_quality, tiff_jpg);
    for (int i = 0; i < range_list.item_count(); i++)
    {
        range_item& item = range_list.get_item(i);
        for (int pg = item.first;
                pg <= min(item.last, doc->getNumPages());
                pg++)
        {
            if (user_cancelled)
                return gpret_user_cancelled;
            doc->displayPage(&img_out, pg, 72, 72, 0, gFalse, gTrue, gFalse);
            printf("progress: %d\n",
                   ++progress * 100 / range_list.page_count());
        }
    }

    printf("image count: %d\n", img_out.get_image_number());

    return gpret_success;
}