Esempio n. 1
0
int
main(void)
{
    /* This is where the data files are. Adjust as necessary.*/
    const char * searchpath = "../data";
    const char *targetname = "x5target.pdf";

    PDF *p;
    char optlist[1024];

    int font, proxy;
    double linewidth=2;
    double width, height;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        PDF_set_parameter(p, "SearchPath", searchpath);

        if (PDF_begin_document(p, "starter_pdfx5g.pdf", 0, "pdfx=PDF/X-5g")
                == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_pdfx5g");

        /* Open the output intent profile */
        if (PDF_load_iccprofile(p, "ISOcoated.icc", 0,
                "usage=outputintent") == -1)
        {
            printf("Error: %s\n", PDF_get_errmsg(p));
            printf("Please install the ICC profile package from "
                   "www.pdflib.com to run the PDF/X-5g starter sample.\n");
            PDF_delete(p);
            return(2);
        }

        /* Font embedding is required for PDF/X */
        font = PDF_load_font(p, "LuciduxSans-Oblique", 0,
                "winansi", "embedding");

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Create a template which will serve as proxy. The referenced
         * page (the target) is attached to the proxy.
	 * The template width and height will be determined automatically,
	 * so we don't have to supply them.
         */
        sprintf(optlist, "reference={filename=%s pagenumber=1}", targetname);
        proxy = PDF_begin_template_ext(p, 0, 0, optlist);

        if (proxy == -1)
        {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

	width  = PDF_info_image(p, proxy, "imagewidth", "");
	height = PDF_info_image(p, proxy, "imageheight", "");

        /* Draw a crossed-out rectangle to visualize the proxy.
         * Attention: if we use the exact corner points, one half of the
         * linewidth would end up outside the template, and therefore be
         * clipped.
         */
        PDF_setlinewidth(p, linewidth);
        PDF_moveto(p, linewidth/2, linewidth/2);
        PDF_lineto(p, width-linewidth/2, linewidth/2);
        PDF_lineto(p, width-linewidth/2, height-linewidth/2);
        PDF_lineto(p, linewidth/2, height-linewidth/2);
        PDF_lineto(p, linewidth/2, linewidth/2);
        PDF_lineto(p, width-linewidth/2, height-linewidth/2);

        PDF_moveto(p, width-linewidth/2, linewidth/2);
        PDF_lineto(p, linewidth/2, height-linewidth/2);
        PDF_stroke(p);

        PDF_setfont(p, font, 24);

        sprintf(optlist, "fitmethod=auto position=center boxsize={%f %f}",
            width, height);
        PDF_fit_textline(p, "Proxy replaces target here", 0,
            0, 0, optlist);

	PDF_end_template_ext(p, 0, 0);


        /* Create the page */
        PDF_begin_page_ext(p, 595, 842, "");

        PDF_setfont(p, font, 18);

        PDF_fit_textline(p,
            "PDF/X-5 starter sample with reference to an external page", 0,
            50, 700, "");

        /* Place the proxy on the page */
        PDF_fit_image(p, proxy, 50, 50, "boxsize={500 500} fitmethod=meet");

        PDF_end_page_ext(p, "");
        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 2
0
int
main(void)
{
    PDF		*p;
    int		manual, page;
    int		font, row, col;
    const	int maxrow = 2;
    const	int maxcol = 2;
    char	optlist[128];
    int		startpage = 1, endpage = 4;
    const float	width = 500, height = 770;
    int		pageno;
    const char *infile = "reference.pdf";

    /* This is where font/image/PDF input files live. Adjust as necessary. */
    char *searchpath = "../data";

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* open new PDF file */
	if (PDF_open_file(p, "quickreference.pdf") == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_set_parameter(p, "SearchPath", searchpath);

	/* This line is required to avoid problems on Japanese systems */
	PDF_set_parameter(p, "hypertextencoding", "host");

	PDF_set_info(p, "Creator", "quickreference.c");
	PDF_set_info(p, "Author", "Thomas Merz");
	PDF_set_info(p, "Title", "mini imposition demo (C)");

	manual = PDF_open_pdi(p, infile, "", 0);
	if (manual == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	row = 0;
	col = 0;

	PDF_set_parameter(p, "topdown", "true");

	for (pageno = startpage; pageno <= endpage; pageno++) {
	    if (row == 0 && col == 0) {
		PDF_begin_page(p, width, height);
		font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");
		PDF_setfont(p, font, 18);
		PDF_set_text_pos(p, 24, 24);
		PDF_show(p, "PDFlib Quick Reference");
	    }

	    page = PDF_open_pdi_page(p, manual, pageno, "");

	    if (page == -1) {
		printf("Error: %s\n", PDF_get_errmsg(p));
		return(2);
	    }

	    sprintf(optlist, "scale %f", (float) 1/maxrow);
	    PDF_fit_pdi_page(p, page,
		width/maxcol*col, (row + 1) * height/maxrow, optlist);
	    PDF_close_pdi_page(p, page);

	    col++;
	    if (col == maxcol) {
		col = 0;
		row++;
	    }
	    if (row == maxrow) {
		row = 0;
		PDF_end_page(p);
	    }
	}

	/* finish the last partial page */
	if (row != 0 || col != 0)
	    PDF_end_page(p);

	PDF_close(p);
	PDF_close_pdi(p, manual);
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in quickreference sample:\n");
        printf("[%d] %s: %s\n",
	    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 3
0
int
main(void)
{

    /* This is where the data files are. Adjust as necessary. */
    const char * searchpath = "../data";

    PDF * p;
    double width, height;
    const char * infile = "boilerplate.pdf";
    int i, page, indoc, blockcount;
    char * optlist;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        PDF_set_parameter(p, "SearchPath", searchpath);

        if (PDF_begin_document(p, "starter_block.pdf", 0, "") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_block");

        /* Open a PDF containing blocks */
        indoc = PDF_open_pdi_document(p, infile, 0, "");
        if (indoc == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Open the first page */
        page = PDF_open_pdi_page(p, indoc, 1, "");
        if (page == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        width = PDF_pcos_get_number(p, indoc, "pages[0]/width");
        height = PDF_pcos_get_number(p, indoc, "pages[0]/height");

        PDF_begin_page_ext(p, width, height, "");

        /* Place the imported page on the output page */
        PDF_fit_pdi_page(p, page, 0, 0, "");

	/* Query the number of blocks on the first page */
        blockcount =
        (int) PDF_pcos_get_number(p, indoc, "length:pages[0]/blocks");

        if (blockcount == 0) {
            printf("Error: %s does not contain any PDFlib blocks", infile);
            PDF_delete(p);
            return(2);
        }

        /* Loop over all blocks on the page */
        for (i = 0; i <  blockcount; i++)
        {
            char buf[1024];
            const char * blockname;
            const char * blocktype;

            /* Fetch the name and type of the i-th block on the first page
             * (one of Text/Image/PDF)
             */
            sprintf(buf, "pages[0]/blocks[%d]/Name",i);
            blockname = PDF_pcos_get_string(p, indoc, buf);

            sprintf(buf, "pages[0]/blocks[%d]/Subtype",
                    i);
            blocktype = PDF_pcos_get_string(p, indoc, buf);

            /* Visualize all text blocks */
            if (!strcmp(blocktype, "Text"))
            {
                optlist =
                    "fontname=Helvetica encoding=winansi "
                    "fillcolor={rgb 1 0 0} "
                    "bordercolor={gray 0} linewidth=0.25";

                /* We simply use the blockname as content */
                if (PDF_fill_textblock(p, page, blockname,
                                    blockname, 0, optlist) == -1) {
                    printf("Warning: %s\n", PDF_get_errmsg(p));
                }
            }
        }

        PDF_end_page_ext(p, "");
        PDF_close_pdi_page(p, page);

        PDF_end_document(p, "");
        PDF_close_pdi_document(p, indoc);
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 4
0
int
main(void)
{
    PDF *p;
    int image;
    char *imagefile = "nesrin.jpg";

    /* This is where font/image/PDF input files live. Adjust as necessary. */
    char *searchpath = "../data";

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p){
	/* open new PDF file */
	if (PDF_open_file(p, "image.pdf") == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_set_parameter(p, "SearchPath", searchpath);

	/* This line is required to avoid problems on Japanese systems */
	PDF_set_parameter(p, "hypertextencoding", "host");

	PDF_set_info(p, "Creator", "image.c");
	PDF_set_info(p, "Author", "Thomas Merz");
	PDF_set_info(p, "Title", "image sample (C)");

	image = PDF_load_image(p, "auto", imagefile, 0, "");

	if (image == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(3);
	}

	/* dummy page size, will be adjusted by PDF_fit_image() */
	PDF_begin_page(p, 10, 10);
	PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "adjustpage");
	PDF_close_image(p, image);
	PDF_end_page(p);			/* close page		*/

	PDF_close(p);				/* close PDF document	*/
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in image sample:\n");
        printf("[%d] %s: %s\n",
	    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);				/* delete the PDFlib object */

    return 0;
}
Esempio n. 5
0
/*
** cg_create_document()
** Create the ouput PDF file.
*/
yerr_t cg_create_document(char *output, cg_t *carta)
{
  PDF *p;
  int i;
  yerr_t return_value = YENOERR;

  if (!(carta->p = p = PDF_new()))
    {
      YLOG_ADD(YLOG_ERR, "PDF allocation error.");
      return (YEUNDEF);
    }

  PDF_TRY(p)
    {
      ystr_t ys = ys_new("");
      /* document's initialization */
      ys_printf(&ys, "compatibility %s",
		(carta->pdf_version == PDF_13) ? "1.3" :
		(carta->pdf_version == PDF_15) ? "1.5" : "1.4");
      if (carta->master_password && carta->user_password)
	{
	  ys_cat(&ys, " masterpassword ");
	  ys_cat(&ys, carta->master_password);
	  ys_cat(&ys, " userpassword ");
	  ys_cat(&ys, carta->user_password);
	}
      if (PDF_begin_document(p, output, 0, ys) == -1)
	{
	  YLOG_ADD(YLOG_ERR, "Unable to create file '%s'", output);
	  PDF_delete(p);
	  ys_del(&ys);
	  return (YEUNDEF);
	}
      ys_del(&ys);
      PDF_set_info(p, "Creator", DEFAULT_CREATOR);
      if (carta->author)
	PDF_set_info(p, "Author", carta->author);
      if (carta->title)
	PDF_set_info(p, "Title", carta->title);
      if (carta->subject)
	PDF_set_info(p, "Subject", carta->subject);
      if (carta->keywords)
	PDF_set_info(p, "Keywords", carta->keywords);
      if (carta->copyright)
	PDF_set_info(p, "Copyright", carta->copyright);
      if (carta->version)
	PDF_set_info(p, "Version", carta->version);
      if (carta->language)
	PDF_set_info(p, "Language", carta->language);
      if (carta->note)
	PDF_set_info(p, "Note", carta->note);

      /* load variables, fonts and images */
      cg_load_variables(carta);
      cg_load_fonts(carta);
      cg_load_images(carta);

      /* process every decks */
      for (i = 0; i < yv_len(carta->decks); ++i)
	{
	  cg_deck_t *deck = carta->decks[i];

	  /* update variables */
	  cg_expr_set_scalar(carta, "DECK_INDEX", (double)i + 1);
	  cg_expr_set_element(carta, "PAGE", deck->paper_width,
			      deck->paper_height);
	  cg_expr_set_element(carta, "PAGE_DIM", deck->paper_width,
			      deck->paper_height);
	  cg_expr_set_element(carta, "PAGE_DIMENSION", deck->paper_width,
			      deck->paper_height);
	  cg_expr_set_element(carta, "CARD", deck->card_width,
			      deck->card_height);
	  cg_expr_set_element(carta, "CARD_DIM", deck->card_width,
			      deck->card_height);
	  cg_expr_set_element(carta, "CARD_DIMENSION", deck->card_width,
			      deck->card_height);
	  cg_expr_set_element(carta, "MARGIN", deck->paper_margin_w,
			      deck->paper_margin_h);
	  cg_expr_set_element(carta, "MARGIN_DIM", deck->paper_margin_w,
			      deck->paper_margin_h);
	  cg_expr_set_element(carta, "MARGIN_DIMENSION",
			      deck->paper_margin_w, deck->paper_margin_h);
	  cg_expr_set_element(carta, "SPACE", deck->space_width,
			      deck->space_height);
	  cg_expr_set_element(carta, "SPACE_DIM", deck->space_width,
			      deck->space_height);
	  cg_expr_set_element(carta, "SPACE_DIMENSION", deck->space_width,
			      deck->space_height);
	  cg_expr_set_scalar(carta, "CARD_DECK",
			     (double)cg_deck_get_nb_cards(carta, deck));
	  cg_expr_set_scalar(carta, "CARDS_IN_DECK",
			     (double)cg_deck_get_nb_cards(carta,deck));
	  cg_expr_set_scalar(carta, "NBR_CARDS_IN_DECK",
			     (double)cg_deck_get_nb_cards(carta,deck));
	  /* process deck */
	  cg_process_deck(carta, deck);
	}
      cg_unload_images(carta);
      PDF_end_document(p, "");
    }
  PDF_CATCH(p)
    {
      YLOG_ADD(YLOG_ERR, "PDFlib exception:\n[%d] %s: %s\n",
	       PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
      return_value = YEUNDEF;
    }
  PDF_delete(p);
  return (return_value);
}
Esempio n. 6
0
int
main(void)
{
    /* This is where the data files are. Adjust as necessary.*/
    const char * searchpath = "../data";

    PDF *p;
    const char * imagefile = "nesrin.jpg";
    char optlist[1024];

    int font, image, spot, icc;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        PDF_set_parameter(p, "SearchPath", searchpath);

        if (PDF_begin_document(p, "starter_pdfx.pdf", 0, "pdfx=PDF/X-3:2002")
                == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_pdfx");

        /*
         * You can use one of the Standard output intents (e.g. for SWOP
         * printing) which do not require an ICC profile:

        PDF_load_iccprofile(p, "CGATS TR 001", 0, "usage=outputintent");

         * However, if you use ICC or Lab color you must load an ICC
         * profile as output intent:
         */

        if (PDF_load_iccprofile(p, "ISOcoated.icc", 0,
                "usage=outputintent") == -1)
        {
            printf("Error: %s\n", PDF_get_errmsg(p));
            printf("Please install the ICC profile package from "
                   "www.pdflib.com to run the PDF/X starter sample.\n");
            PDF_delete(p);
            return(2);
        }

        PDF_begin_page_ext(p, 595, 842, "");

        /* Font embedding is required for PDF/X */
        font = PDF_load_font(p, "LuciduxSans-Oblique", 0,
                "winansi", "embedding");

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_setfont(p, font, 24);

        spot = PDF_makespotcolor(p, "PANTONE 123 C", 0);
        PDF_setcolor(p, "fill", "spot", spot, 1.0, 0.0, 0.0);
        PDF_fit_textline(p, "PDF/X-3:2002 starter", 0, 50, 700, "");

        /* The RGB image below needs an ICC profile; we use sRGB. */
        icc = PDF_load_iccprofile(p, "sRGB", 0, "");
        sprintf(optlist, "iccprofile=%d", icc);
        image = PDF_load_image(p, "auto", imagefile, 0, optlist);

        if (image == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "scale=0.5");

        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 7
0
int
main(void)
{

    /* This is where the data files are. Adjust as necessary. */
    const char* searchpath = "../data";
    const char* outfile = "starter_layer.pdf";

    PDF * p;
    const char* rgb = "nesrin.jpg";
    const char* gray = "nesrin_gray.jpg";

#define BUFLEN 1024
    char buf[BUFLEN];
    int font, imageRGB, imageGray, layerRGB, layerGray, layerEN, layerDE;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        PDF_set_parameter(p, "SearchPath", searchpath);

        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");


        /* Open the document with the "Layers" navigation tab visible */
        if (PDF_begin_document(p, outfile, 0, "openmode=layers") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_layer");

        /* Load the font */
        font = PDF_load_font(p, "Helvetica", 0, "winansi", "");

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Load the Grayscale image */
        imageGray = PDF_load_image(p, "auto", gray, 0, "");
        if (imageGray == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Load the RGB image */
        imageRGB = PDF_load_image(p, "auto", rgb, 0, "");
        if (imageRGB == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /*
         * Define all layers which will be used, and their relationships.
         * This should be done before the first page if the layers are
         * used on more than one page.
         */

        /* Define the layer "RGB" */
        layerRGB = PDF_define_layer(p, "RGB", 0, "");

        /* Define the layer "Grayscale" which is hidden when opening the
         * document or printing it. */
        layerGray = PDF_define_layer(p, "Grayscale", 0,
                    "initialviewstate=false initialprintstate=false");

        /* At most one of the "Grayscale" and "RGB" layers should be visible */
        sprintf(buf, "group={%d %d}", layerGray, layerRGB);
        PDF_set_layer_dependency(p, "Radiobtn", buf);

        /* Define the layer "English" */
        layerEN = PDF_define_layer(p, "English", 0, "");

        /* Define the layer "German" which is hidden when opening the document
         * or printing it. */
        layerDE = PDF_define_layer(p, "German", 0,
                    "initialviewstate=false initialprintstate=false");

        /* At most one of the "English" and "German" layers should be visible */
        sprintf(buf, "group={%d %d}", layerEN, layerDE);
        PDF_set_layer_dependency(p, "Radiobtn", buf);

        /* Start page */
        PDF_begin_page_ext(p, 0, 0, "width=a4.width height=a4.height");

	/* Place the RGB image on the layer "RGB" */
        PDF_begin_layer(p, layerRGB);
        PDF_fit_image(p, imageRGB, 100, 400,
                    "boxsize={400 300} fitmethod=meet");

	/* Place the Grayscale image on the layer "Grayscale" */
        PDF_begin_layer(p, layerGray);
        PDF_fit_image(p, imageGray, 100, 400,
                    "boxsize={400 300} fitmethod=meet");

	/* Place an English image caption on the layer "English" */
        PDF_begin_layer(p, layerEN);
        sprintf(buf, "font=%d fontsize=20", font);
        PDF_fit_textline(p, "This is the Nesrin image.", 0, 100, 370, buf);

	/* Place a German image caption on the layer "German".  */
        PDF_begin_layer(p, layerDE);
        sprintf(buf, "font=%d fontsize=20", font);
        PDF_fit_textline(p, "Das ist das Nesrin-Bild.", 0, 100, 370, buf);

        PDF_end_layer(p);

        PDF_end_page_ext(p, "");

        PDF_end_document(p, "");

    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);
    return 0;
}
Esempio n. 8
0
int
main(void)
{
    PDF		*p;
    float	alpha;
    time_t	timer;
    struct tm	ltime;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
	/* open new PDF file */
	if (PDF_open_file(p, "pdfclock.pdf") == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	/* This line is required to avoid problems on Japanese systems */
	PDF_set_parameter(p, "hypertextencoding", "host");

	PDF_set_info(p, "Creator", "pdfclock.c");
	PDF_set_info(p, "Author", "Thomas Merz");
	PDF_set_info(p, "Title", "PDF clock (C)");

	PDF_begin_page(p, (float) (2 * (RADIUS + MARGIN)),
			  (float) (2 * (RADIUS + MARGIN)));
	
	PDF_translate(p, RADIUS + MARGIN, RADIUS + MARGIN);
	PDF_setcolor(p, "fillstroke", "rgb", 0, 0, 1, 0);
	PDF_save(p);

	/* minute strokes */
	PDF_setlinewidth(p, 2);
	for (alpha = 0; alpha < 360; alpha += 6)
	{
	    PDF_rotate(p, 6);
	    PDF_moveto(p, RADIUS, 0);
	    PDF_lineto(p, (float) (RADIUS-MARGIN/3), 0);
	    PDF_stroke(p);
	}

	PDF_restore(p);
	PDF_save(p);

	/* 5 minute strokes */
	PDF_setlinewidth(p, 3);
	for (alpha = 0; alpha < 360; alpha += 30)
	{
	    PDF_rotate(p, 30);
	    PDF_moveto(p, RADIUS, 0);
	    PDF_lineto(p, RADIUS-MARGIN, 0);
	    PDF_stroke(p);
	}

	time(&timer);
	ltime = *localtime(&timer);

	/* draw hour hand */
	PDF_save(p);
	PDF_rotate(p, 
		(float)(-((ltime.tm_min/60.0) + ltime.tm_hour - 3.0) * 30.0));
	PDF_moveto(p, -RADIUS/10, -RADIUS/20);
	PDF_lineto(p, RADIUS/2, 0);
	PDF_lineto(p, -RADIUS/10, RADIUS/20);
	PDF_closepath(p);
	PDF_fill(p);
	PDF_restore(p);

	/* draw minute hand */
	PDF_save(p);
	PDF_rotate(p,
		(float) (-((ltime.tm_sec/60.0) + ltime.tm_min - 15.0) * 6.0));
	PDF_moveto(p, -RADIUS/10, -RADIUS/20);
	PDF_lineto(p, RADIUS * 0.8f, 0);
	PDF_lineto(p, -RADIUS/10, RADIUS/20);
	PDF_closepath(p);
	PDF_fill(p);
	PDF_restore(p);

	/* draw second hand */
	PDF_setcolor(p, "fillstroke", "rgb", 1, 0, 0, 0);
	PDF_setlinewidth(p, 2);
	PDF_save(p);
	PDF_rotate(p, (float) -((ltime.tm_sec - 15.0) * 6.0));
	PDF_moveto(p, -RADIUS/5, 0);
	PDF_lineto(p, RADIUS, 0);
	PDF_stroke(p);
	PDF_restore(p);

	/* draw little circle at center */
	PDF_circle(p, 0, 0, (float) RADIUS/30);
	PDF_fill(p);

	PDF_restore(p);

	PDF_end_page(p);

	PDF_close(p);
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in pdfclock sample:\n");
        printf("[%d] %s: %s\n",
	    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);				/* delete the PDFlib object */

    return 0;
}
Esempio n. 9
0
int
main(void)
{
    /* This is where the data files are. Adjust as necessary. */
    const char *searchpath = "../data";
    const char *outfile = "starter_block.pdf";
    const char *infile = "block_template.pdf";
    const char *imagefile = "new.jpg";

    PDF *p;
    int i, j, inpage, indoc, image;
    char optlist[1024], buf[1024];

    /* Names of the person-related blocks contained on the imported page */
    const char *addressblocks[] = {
        "name", "street", "city"
    };

    /* number of address blocks */
    const int nblocks = sizeof(addressblocks) / sizeof(addressblocks[0]);

    /* Data related to various persons used for personalization */
    const char *persons[][3] = {
        {"Mr Maurizio Moroni", "Strada Provinciale 124", "Reggio Emilia"},
        {"Ms Dominique Perrier", "25, rue Lauriston", "Paris"},
        {"Mr Liu Wong", "55 Grizzly Peak Rd.", "Butte"}
    };

    /* number of persons */
    const int npersons = sizeof(persons) / sizeof(persons[0]);

    /* Static text simulates database-driven variable contents */
    const char *intro = "Dear";
    const char *goodbye = "Yours sincerely,\nVictor Kraxi";
    const char *announcement =
        "Our <fillcolor=red>BEST PRICE OFFER<fillcolor=black> includes today:"
        "\n\n"
        "Long Distance Glider\nWith this paper rocket you can send all your "
        "messages even when sitting in a hall or in the cinema pretty near "
        "the back.\n\n"
        "Giant Wing\nAn unbelievable sailplane! It is amazingly robust and "
        "can even do aerobatics. But it is best suited to gliding.\n\n"
        "Cone Head Rocket\nThis paper arrow can be thrown with big swing. "
        "We launched it from the roof of a hotel. It stayed in the air a "
        "long time and covered a considerable distance.\n\n"
        "Super Dart\nThe super dart can fly giant loops with a radius of 4 "
        "or 5 meters and cover very long distances. Its heavy cone point is "
        "slightly bowed upwards to get the lift required for loops.\n\n"
        "Visit us on our Web site at www.kraxi.com!";

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        PDF_set_parameter(p, "SearchPath", searchpath);

        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        if (PDF_begin_document(p, outfile, 0,
                "destination={type=fitwindow} pagelayout=singlepage") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_block");

        /* Open the Block template which contains PDFlib Blocks */
        indoc = PDF_open_pdi_document(p, infile, 0, "");
        if (indoc == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Open the first page and clone the page size */
        inpage = PDF_open_pdi_page(p, indoc, 1, "cloneboxes");
        if (inpage == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        image = PDF_load_image(p, "auto", imagefile, 0, "");

        if (image == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            return(3);
        }

        /* Based on the imported page generate several pages with the blocks
         * being filled with data related to different persons
         */
        for (i = 0; i < npersons; i++)
        {
            /* Start the output page with a dummy size */
            PDF_begin_page_ext(p, 10, 10, "");

            /* Place the imported page on the output page, and clone all
             * page boxes which are present in the input page; this will
             * override the dummy size used in begin_page_ext().
             */
            PDF_fit_pdi_page(p, inpage, 0, 0, "cloneboxes");

            /* Option list for text blocks */
            strcpy(optlist, "encoding=winansi embedding");

            /* Loop over all person-related blocks. Fill the j-th block with the
             * corresponding entry of the persons array.
             */
            for (j = 0; j < nblocks; j++) {
                if (PDF_fill_textblock(p, inpage, addressblocks[j],
                        persons[i][j], 0, optlist) == -1)
                    printf("Warning: %s\n", PDF_get_errmsg(p));
            }

            /* Fill the "intro" block */
            sprintf(buf, "%s %s,", intro, persons[i][0]);
            if (PDF_fill_textblock(p, inpage, "intro", buf, 0, optlist) == -1)
                printf("Warning: %s\n", PDF_get_errmsg(p));

            /* Fill the "announcement" block */
            if (PDF_fill_textblock(p, inpage, "announcement", announcement, 0,
                    optlist) == -1)
                printf("Warning: %s\n", PDF_get_errmsg(p));

            /* Fill the "goodbye" block */
            if (PDF_fill_textblock(p, inpage, "goodbye", goodbye, 0,
                    optlist) == -1)
                printf("Warning: %s\n", PDF_get_errmsg(p));

            /* Fill the image block */
            strcpy(optlist, "");       /* Option list */
            if (PDF_fill_imageblock(p, inpage, "icon", image, optlist) == -1)
                printf("Warning: %s\n", PDF_get_errmsg(p));

            PDF_end_page_ext(p, "");
        }

        PDF_close_pdi_page(p, inpage);
        PDF_close_pdi_document(p, indoc);
        PDF_close_image(p, image);

        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 10
0
int
main(void)
{
    PDF 	*p;
    int 	i, blockcontainer, page;
    char 	*infile = "boilerplate.pdf";

    /*
     * This is where font/image/PDF input files live. Adjust as necessary.
     *
     * Note that this directory must also contain the LuciduxSans font outline
     * and metrics files.
     */
    char *searchpath = "../data";

    typedef struct { char *name; char *value; } blockdata;

    blockdata data[] = {
    { "name",			"Victor Kraxi" },
    { "business.title",		"Chief Paper Officer" },
    { "business.address.line1",	"17, Aviation Road" },
    { "business.address.city",	"Paperfield" },
    { "business.telephone.voice","phone +1 234 567-89" },
    { "business.telephone.fax",	"fax +1 234 567-98" },
    { "business.email",		"*****@*****.**" },
    { "business.homepage",	"www.kraxi.com" },
    };

#define BLOCKCOUNT (sizeof(data)/sizeof(data[0]))

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0)
    {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
	/* This means we must check return values of load_font() etc. */
	PDF_set_parameter(p, "errorpolicy", "return");

	/* Set the search path for fonts and PDF files */
	PDF_set_parameter(p, "SearchPath", searchpath);

	/* This line is required to avoid problems on Japanese systems */
	PDF_set_parameter(p, "hypertextencoding", "host");

	if (PDF_begin_document(p, "businesscard.pdf", 0, "") == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_set_info(p, "Creator", "businesscard.c");
	PDF_set_info(p, "Author", "Thomas Merz");
	PDF_set_info(p, "Title","PDFlib block processing sample (C)");

	blockcontainer = PDF_open_pdi_document(p, infile, 0, "");
	if (blockcontainer == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
        }

	page = PDF_open_pdi_page(p, blockcontainer, 1, "");
	if (page == -1) {
	    printf("Error: %s\n", PDF_get_errmsg(p));
	    return(2);
	}

	PDF_begin_page_ext(p, 20, 20, "");		/* dummy page size */

	/* This will adjust the page size to the block container's size. */
	PDF_fit_pdi_page(p, page, 0, 0, "adjustpage");

	/* Fill all text blocks with dynamic data */
	for (i = 0; i < (int) BLOCKCOUNT; i++) {
	    if (PDF_fill_textblock(p, page, data[i].name, data[i].value, 0,
		    "embedding encoding=host") == -1) {
		printf("Warning: %s\n", PDF_get_errmsg(p));
	    }
	}

	PDF_end_page_ext(p, "");
	PDF_close_pdi_page(p, page);

	PDF_end_document(p, "");
	PDF_close_pdi_document(p, blockcontainer);
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred in businesscard sample:\n");
        printf("[%d] %s: %s\n",
	    PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}
Esempio n. 11
0
int
main(void)
{

    /* This is where the data files are. Adjust as necessary. */
    const char* searchpath = "../data";

    PDF * p;
    const char* imagefile = "nesrin.jpg";
    char* optlist;
    int font, image;

    /* create a new PDFlib object */
    if ((p = PDF_new()) == (PDF *) 0) {
        printf("Couldn't create PDFlib object (out of memory)!\n");
        return(2);
    }

    PDF_TRY(p) {
        /* This means we must check return values of load_font() etc. */
        PDF_set_parameter(p, "errorpolicy", "return");

        PDF_set_parameter(p, "SearchPath", searchpath);

        if (PDF_begin_document(p, "starter_basic.pdf", 0, "") == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_set_info(p, "Creator", "PDFlib starter sample");
        PDF_set_info(p, "Title", "starter_basic");

        /* We load the image before the first page, and use it
         * on all pages
         */
        image = PDF_load_image(p, "auto", imagefile, 0, "");

        if (image == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        /* Page 1 */
        PDF_begin_page_ext(p, 595, 842, "");

        font = PDF_load_font(p, "Helvetica-Bold", 0, "winansi", "");

        if (font == -1) {
            printf("Error: %s\n", PDF_get_errmsg(p));
            PDF_delete(p);
            return(2);
        }

        PDF_setfont(p, font, 24);

        PDF_set_text_pos(p, 50, 700);
        PDF_show(p, "Hello world!");

        PDF_fit_image(p, image, (float) 0.0, (float) 0.0, "scale=0.25");

        PDF_end_page_ext(p, "");

        /* Page 2 */
        PDF_begin_page_ext(p, 595, 842, "");

        /* red rectangle */
        PDF_setcolor(p, "fill", "rgb", 1.0, 0.0, 0.0, 0.0);
        PDF_rect(p, 200, 200, 250, 150);
        PDF_fill(p);

        /* blue circle */
        PDF_setcolor(p, "fill", "rgb", 0.0, 0.0, 1.0, 0.0);
        PDF_arc(p, 400, 600, 100, 0, 360);
        PDF_fill(p);

        /* thick gray line */
        PDF_setcolor(p, "stroke", "gray", 0.5, 0.0, 0.0, 0.0);
        PDF_setlinewidth(p, 10);
        PDF_moveto(p, 100, 500);
        PDF_lineto(p, 300, 700);
        PDF_stroke(p);

        /* Using the same image handle means the data will be copied
         * to the PDF only once, which saves space.
         */
        PDF_fit_image(p, image, 150.0, 25.0, "scale=0.25");
        PDF_end_page_ext(p, "");

        /* Page 3 */
        PDF_begin_page_ext(p, 595, 842, "");

        /* Fit the image to a box of predefined size (without distortion) */
        optlist =
        "boxsize={400 400} position={center} fitmethod=meet";

        PDF_fit_image(p, image, 100, 200, optlist);

        PDF_end_page_ext(p, "");

        PDF_close_image(p, image);
        PDF_end_document(p, "");
    }

    PDF_CATCH(p) {
        printf("PDFlib exception occurred:\n");
        printf("[%d] %s: %s\n",
            PDF_get_errnum(p), PDF_get_apiname(p), PDF_get_errmsg(p));
        PDF_delete(p);
        return(2);
    }

    PDF_delete(p);

    return 0;
}