Пример #1
0
void openxref(char *filename, char *password, int dieonbadpass)
{
	fz_error error;
	int okay;

	basename = strrchr(filename, '/');
	if (!basename)
		basename = filename;
	else
		basename++;

	xref = pdf_newxref();
	error = pdf_loadxref(xref, filename);
	if (error)
	{
		fz_catch(error, "trying to repair");
		error = pdf_repairxref(xref, filename);
		if (error)
			die(error);
	}

	error = pdf_decryptxref(xref);
	if (error)
		die(error);

	if (pdf_needspassword(xref))
	{
		okay = pdf_authenticatepassword(xref, password);
		if (!okay && !dieonbadpass)
			fz_warn("invalid password, attempting to continue.");
		else if (!okay && dieonbadpass)
			die(fz_throw("invalid password"));
	}

	xref->root = fz_dictgets(xref->trailer, "Root");
	if (xref->root)
		fz_keepobj(xref->root);

	xref->info = fz_dictgets(xref->trailer, "Info");
	if (xref->info)
		fz_keepobj(xref->info);

	pagecount = pdf_getpagecount(xref);
}
Пример #2
0
Epdf_Document* epdf_document_new(const char* filename)
{
    Epdf_Document* doc;
    fz_error error;

    doc = (Epdf_Document*)malloc(sizeof(Epdf_Document));
    if(!doc)
        return NULL;

    doc->xref = NULL;
    doc->outline = NULL;
    doc->rast = NULL;
    doc->pagecount = 0;
    doc->zoom = 1.0;
    doc->rotate = 0;
    doc->locked = false;

    error = fz_newrenderer(&doc->rast, pdf_devicergb, 0, 1024 * 512);
    if(error)
        return pdfdoc_error(doc);

    fz_obj* obj;

    // Open PDF and load xref table

    doc->xref = pdf_newxref();
    if(!doc->xref)
        return pdfdoc_error(doc);

    error = pdf_loadxref(doc->xref, (char*)filename);
    if(error)
    {
        fz_catch(error, "trying to repair");
        fprintf(stderr, "There was a problem with file \"%s\".\nIt may be corrupted or generated by faulty software.\nTrying to repair the file.\n", filename);
        error = pdf_repairxref(doc->xref, (char*)filename);
        if(error)
            return pdfdoc_error(doc);
    }

    error = pdf_decryptxref(doc->xref);
    if(error)
        pdfdoc_error(doc);

    // Handle encrypted PDF files

    if(pdf_needspassword(doc->xref))
        doc->locked = true;

    /*
     * Load meta information
     * TODO: move this into mupdf library
     */

    obj = fz_dictgets(doc->xref->trailer, (char*)"Root");
    doc->xref->root = fz_resolveindirect(obj);
    if(!doc->xref->root)
    {
        fz_throw("syntaxerror: missing Root object");
        pdfdoc_error(doc);
    }
    fz_keepobj(doc->xref->root);

    obj = fz_dictgets(doc->xref->trailer, "Info");
    doc->xref->info = fz_resolveindirect(obj);
    if(!doc->xref->info)
        fprintf(stderr, "Could not load PDF meta information.\n");
    if(doc->xref->info)
        fz_keepobj(doc->xref->info);

    doc->outline = pdf_loadoutline(doc->xref);

    doc->filename = strdup(filename);
    if(doc->xref->info)
    {
        obj = fz_dictgets(doc->xref->info, "Title");
        if(obj)
            doc->doctitle = pdf_toutf8(obj);
    }

    /*
     * Start at first page
     */

    doc->pagecount = pdf_getpagecount(doc->xref);

    if(doc->zoom < 0.1)
        doc->zoom = 0.1;
    if(doc->zoom > 3.0)
        doc->zoom = 3.0;

    return doc;
}
Пример #3
0
void pdfmoz_open(pdfmoz_t *moz, char *filename)
{
    SCROLLINFO si;
    fz_error error;
    fz_obj *obj;
    fz_irect bbox;
    int rot;
    int i;

    strcpy(moz->error, "");

    error = fz_newrenderer(&moz->rast, pdf_devicergb, 0, 1024 * 512);
    if (error)
	pdfmoz_error(moz, error);

    /*
     * Open PDF and load xref table
     */

    moz->filename = filename;

    moz->xref = pdf_newxref();
    error = pdf_loadxref(moz->xref, filename);
    if (error)
    {
	error = pdf_repairxref(moz->xref, filename);
	if (error)
	    pdfmoz_error(moz, error);
    }

    /*
     * Handle encrypted PDF files
     */

    error = pdf_decryptxref(moz->xref);
    if (error)
	pdfmoz_error(moz, error);

    if (pdf_needspassword(moz->xref))
    {
	pdfmoz_warn(moz, "PDF file is encrypted and needs a password.");
    }

    moz->pagecount = pdf_getpagecount(moz->xref);
    moz->pages = fz_malloc(sizeof(page_t) * moz->pagecount);

    for (i = 0; i < moz->pagecount; i++)
    {
	fz_obj *pageobj;
	pageobj = pdf_getpageobject(moz->xref, i);
	moz->pages[i].obj = fz_keepobj(pageobj);
	moz->pages[i].page = nil;
	moz->pages[i].image = nil;

	obj = fz_dictgets(moz->pages[i].obj, "CropBox");
	if (!obj)
	    obj = fz_dictgets(moz->pages[i].obj, "MediaBox");
	bbox = fz_roundrect(pdf_torect(obj));
	moz->pages[i].w = bbox.x1 - bbox.x0;
	moz->pages[i].h = bbox.y1 - bbox.y0;

	rot = fz_toint(fz_dictgets(moz->pages[i].obj, "Rotate"));
	if ((rot / 90) % 2)
	{
	    int t = moz->pages[i].w;
	    moz->pages[i].w = moz->pages[i].h;
	    moz->pages[i].h = t;
	}

	moz->pages[i].px = 1 + PAD;
    }

    /*
     * Load meta information
     * TODO: move this into mupdf library
     */

    obj = fz_dictgets(moz->xref->trailer, "Root");
    moz->xref->root = fz_resolveindirect(obj);
    if (!moz->xref->root)
	pdfmoz_error(moz, fz_throw("syntaxerror: missing Root object"));
    if (moz->xref->root)
	fz_keepobj(moz->xref->root);

    obj = fz_dictgets(moz->xref->trailer, "Info");
    moz->xref->info = fz_resolveindirect(obj);
    if (moz->xref->info)
	fz_keepobj(moz->xref->info);

    moz->doctitle = filename;
    if (strrchr(moz->doctitle, '\\'))
	moz->doctitle = strrchr(moz->doctitle, '\\') + 1;
    if (strrchr(moz->doctitle, '/'))
	moz->doctitle = strrchr(moz->doctitle, '/') + 1;
    if (moz->xref->info)
    {
	obj = fz_dictgets(moz->xref->info, "Title");
	if (obj)
	    moz->doctitle = pdf_toutf8(obj);
    }

    /*
     * Start at first page
     */

    si.cbSize = sizeof(si);
    si.fMask = SIF_POS | SIF_RANGE; // XXX | SIF_PAGE;
    si.nPos = 0;
    si.nMin = 0;
    si.nMax = 1;
    si.nPage = 1;
    SetScrollInfo(moz->hwnd, SB_VERT, &si, TRUE);

    moz->scrollpage = 0;
    moz->scrollyofs = 0;

    InvalidateRect(moz->hwnd, NULL, FALSE);
}
fz_error
pdf_openxrefwithstream(pdf_xref **xrefp, fz_stream *file, char *password)
{
    pdf_xref *xref;
    fz_error error;
    fz_obj *encrypt;
    fz_obj *id;

    xref = fz_malloc(sizeof(pdf_xref));

    memset(xref, 0, sizeof(pdf_xref));

    pdf_logxref("openxref %p\n", xref);

    xref->file = fz_keepstream(file);

    error = pdf_loadxref(xref, xref->scratch, sizeof xref->scratch);
    if (error)
    {
        fz_catch(error, "trying to repair");
        if (xref->table)
        {
            fz_free(xref->table);
            xref->table = NULL;
            xref->len = 0;
            xref->cap = 0;
        }
        error = pdf_repairxref(xref, xref->scratch, sizeof xref->scratch);
        if (error)
        {
            pdf_freexref(xref);
            return fz_rethrow(error, "cannot repair document");
        }
    }

    encrypt = fz_dictgets(xref->trailer, "Encrypt");
    id = fz_dictgets(xref->trailer, "ID");
    if (fz_isdict(encrypt))
    {
        error = pdf_newcrypt(&xref->crypt, encrypt, id);
        if (error)
        {
            pdf_freexref(xref);
            return fz_rethrow(error, "cannot decrypt document");
        }
    }

    if (pdf_needspassword(xref))
    {
        /* Only care if we have a password */
        if (password)
        {
            int okay = pdf_authenticatepassword(xref, password);
            if (!okay)
            {
                pdf_freexref(xref);
                return fz_throw("invalid password");
            }
        }
    }

    *xrefp = xref;
    return fz_okay;
}
Пример #5
0
void pdfapp_open(pdfapp_t *app, char *filename)
{
    fz_obj *obj;
    fz_obj *info;
    char *password = "";

    /*
     * Open PDF and load xref table
     */

    app->filename = filename;

    app->xref = pdf_openxref(filename);
    if (!app->xref)
        pdfapp_error(app, -1);

    /*
     * Handle encrypted PDF files
     */

    if (pdf_needspassword(app->xref))
    {
        int okay = pdf_authenticatepassword(app->xref, password);
        while (!okay)
        {
            password = winpassword(app, filename);
            if (!password)
                exit(1);
            okay = pdf_authenticatepassword(app->xref, password);
            if (!okay)
                pdfapp_warn(app, "Invalid password.");
        }
    }

    /*
     * Load meta information
     */

    app->outline = pdf_loadoutline(app->xref);

    app->doctitle = filename;
    if (strrchr(app->doctitle, '\\'))
        app->doctitle = strrchr(app->doctitle, '\\') + 1;
    if (strrchr(app->doctitle, '/'))
        app->doctitle = strrchr(app->doctitle, '/') + 1;
    info = fz_dictgets(app->xref->trailer, "Info");
    if (info)
    {
        obj = fz_dictgets(info, "Title");
        if (obj)
            app->doctitle = pdf_toutf8(obj);
    }

    /*
     * Start at first page
     */

    app->pagecount = pdf_getpagecount(app->xref);

    app->shrinkwrap = 1;
    if (app->pageno < 1)
        app->pageno = 1;
    if (app->zoom < 0.1)
        app->zoom = 0.1;
    if (app->zoom > 3.0)
        app->zoom = 3.0;
    app->rotate = 0;
    app->panx = 0;
    app->pany = 0;

    //code change by kakai
    //highlight color settings
    kno_setHighlightColor(app, 0x00ffff00);
    //code change by kakai

    pdfapp_showpage(app, 1, 1);
}