int main(void) { PDF *p; int font; p = PDF_new(); /* open new PDF file */ if (PDF_open_file(p, "hello_c.pdf") == -1) { fprintf(stderr, "Error: cannot open PDF file hello_c.pdf.\n"); exit(2); } PDF_set_info(p, "Creator", "hello.c"); PDF_set_info(p, "Author", "Thomas Merz"); PDF_set_info(p, "Title", "Hello, world (C)!"); PDF_begin_page(p, a4_width, a4_height); /* start a new page */ font = PDF_findfont(p, "Helvetica-Bold", "default", 0); if (font == -1) { fprintf(stderr, "Couldn't set font!\n"); exit(3); } PDF_setfont(p, font, 24); PDF_set_text_pos(p, 50, 700); PDF_show(p, "Hello, world!"); PDF_continue_text(p, "(says C)"); PDF_end_page(p); /* close page */ PDF_close(p); /* close PDF document */ exit(0); }
int main(int argc, char *argv[]) { char buf[BUFLEN], *s; char *pdffilename = NULL; FILE *textfile = stdin; PDF *p; int opt; int font; char *fontname, *encoding; double fontsize; double x, y, width = a4_width, height = a4_height, margin = 20; char ff, nl; fontname = "Courier"; fontsize = 12.0; encoding = "host"; nl = '\n'; ff = '\f'; while ((opt = getopt(argc, argv, "e:f:h:m:o:s:w:")) != -1) switch (opt) { case 'e': encoding = optarg; break; case 'f': fontname = optarg; break; case 'h': height = atof(optarg); if (height < 0) { fprintf(stderr, "Error: bad page height %f!\n", height); usage(); } break; case 'm': margin = atof(optarg); if (margin < 0) { fprintf(stderr, "Error: bad margin %f!\n", margin); usage(); } break; case 'o': pdffilename = optarg; break; case 's': fontsize = atof(optarg); if (fontsize < 0) { fprintf(stderr, "Error: bad font size %f!\n", fontsize); usage(); } break; case 'w': width = atof(optarg); if (width < 0) { fprintf(stderr, "Error: bad page width %f!\n", width); usage(); } break; case '?': default: usage(); } if (!strcmp(encoding, "ebcdic")) { /* form feed is 0x0C in both ASCII and EBCDIC */ nl = 0x15; } if (pdffilename == NULL) usage(); if (optind < argc) { if ((textfile = fopen(argv[optind], READMODE)) == NULL) { fprintf(stderr, "Error: cannot open input file %s.\n",argv[optind]); exit(2); } } else textfile = stdin; p = PDF_new(); if (p == NULL) { fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename); exit(1); } PDF_begin_document(p, pdffilename, 0, ""); PDF_set_info(p, "Title", "Converted text"); PDF_set_info(p, "Creator", "text2pdf"); x = margin; y = height - margin; while ((s = fgets(buf, BUFLEN, textfile)) != NULL) { if (s[0] == ff) { if (y == height - margin) PDF_begin_page_ext(p, width, height, ""); PDF_end_page_ext(p, ""); y = height - margin; continue; } if (s[0] != '\0' && s[strlen(s) - 1] == nl) s[strlen(s) - 1] = '\0'; /* remove newline character */ if (y < margin) { /* page break necessary? */ y = height - margin; PDF_end_page_ext(p, ""); } if (y == height - margin) { PDF_begin_page_ext(p, width, height, ""); font = PDF_load_font(p, fontname, 0, encoding, ""); PDF_setfont(p, font, fontsize); PDF_set_text_pos(p, x, y); y -= fontsize; } PDF_continue_text(p, s); y -= fontsize; } if (y != height - margin) PDF_end_page_ext(p, ""); PDF_end_document(p, ""); PDF_delete(p); exit(0); }