int main(void) { try { /* This is where the data files are. Adjust as necessary. */ const wstring searchpath = L"../data"; const wstring outfile = L"starter_graphics.pdf"; PDFlib p; double xt=20, x = 210, y=770, dy=90; int font; p.set_parameter(L"SearchPath", searchpath); /* This means we must check return values of load_font() etc. */ p.set_parameter(L"errorpolicy", L"return"); if (p.begin_document(outfile, L"") == -1) { wcerr << L"Error: " << p.get_errmsg() << endl; return 2; } p.set_info(L"Creator", L"PDFlib starter sample"); p.set_info(L"Title", L"starter_graphics"); font = p.load_font(L"Helvetica", L"winansi", L""); if (font == -1) { wcerr << L"Error: " << p.get_errmsg() << endl; return 2; } /* Start an A4 page */ p.begin_page_ext(0, 0, L"width=a4.width height=a4.height"); /* Set the font */ p.setfont(font, 14); /* Set the line width */ p.setlinewidth(2.0); /* Set the stroke color */ p.setcolor(L"stroke", L"rgb", 0.0, 0.5, 0.5, 0.0); /* Set the fill color */ p.setcolor(L"fill", L"rgb", 0.0, 0.85, 0.85, 0.0); /* ------------- * Stroke a line * ------------- */ /* Set the current point for graphics output */ p.moveto(x, y); /* Draw a line from the current point to the supplied point */ p.lineto(x+300, y+50); /* Stroke the path using the current line width and stroke color, and * clear it */ p.stroke(); /* Output some descriptive black text */ p.fit_textline(L"lineto() and stroke()", xt, y, L"fillcolor={gray 0}"); /* -------------- * Stroke a curve * -------------- */ /* Set the current point for graphics output */ p.moveto(x, y-=dy); /* Draw a Bézier curve from the current point to (x3, y3), using three * control points */ p.curveto(x+50, y+40, x+200, y+80, x+300, y+30); /* Stroke the path using the current line width and stroke color, and * clear it */ p.stroke(); /* Output some descriptive black text */ p.fit_textline(L"curveto() and stroke()", xt, y, L"fillcolor={gray 0}"); /* --------------- * Stroke a circle * --------------- */ /* Draw a circle at position (x, y) with a radius of 40 */ p.circle(x, y-=dy, 40); /* Stroke the path using the current line width and stroke color, and * clear it */ p.stroke(); /* Output some descriptive black text */ p.fit_textline(L"circle() and stroke()", xt, y, L"fillcolor={gray 0}"); /* --------------------- * Stroke an arc segment * --------------------- */ /* Draw an arc segment counterclockwise at position (x, y) with a radius * of 40 starting at an angle of 90 degrees and ending at 180 degrees */ p.arc(x, y-=dy+20, 40, 90, 180); /* Stroke the path using the current line width and stroke color, and * clear it */ p.stroke(); /* Output some descriptive black text */ p.fit_textline(L"arc() and stroke()", xt, y, L"fillcolor={gray 0}"); /* ------------------ * Stroke a rectangle * ------------------ */ /* Draw a rectangle at position (x, y) with a width of 200 and a height * of 50 */ p.rect(x, y-=dy, 200, 50); /* Stroke the path using the current line width and stroke color, and * clear it */ p.stroke(); /* Output some descriptive black text */ p.fit_textline(L"rect() and stroke()", xt, y, L"fillcolor={gray 0}"); /* ---------------- * Fill a rectangle * ---------------- */ /* Draw a rectangle at position (x, y) with a width of 200 and a height * of 50 */ p.rect(x, y-=dy, 200, 50); /* Fill the path using current fill color, and clear it */ p.fill(); /* Output some descriptive black text */ p.fit_textline(L"rect() and fill()", xt, y, L"fillcolor={gray 0}"); /* --------------------------- * Fill and stroke a rectangle * --------------------------- */ /* Draw a rectangle at position (x, y) with a width of 200 and a height * of 50 */ p.rect(x, y-=dy, 200, 50); /* Fill and stroke the path using the current line width, fill color, * and stroke color, and clear it */ p.fill_stroke(); /* Output some descriptive black text */ p.fit_textline(L"rect() and fill_stroke()", xt, y, L"fillcolor={gray 0}"); /* ------------------------------------------------------------- * Draw a line and an arc, close the path and fill and stroke it * ------------------------------------------------------------- */ /* Set the current point for graphics output */ p.moveto(x-40, y-=dy); /* Draw a line from the current point to the supplied point */ p.lineto(x, y); /* Draw an arc segment counterclockwise at position (x, y) with a radius * of 40 starting at an angle of 90 degrees and ending at 180 degrees */ p.arc(x, y, 40, 90, 180); /* Close the path and stroke and fill it, i.e. close the current subpath * (add a straight line segment from the current point to the starting * point of the path), and stroke and fill the complete current path */ p.closepath_fill_stroke(); /* Output some descriptive black text */ p.fit_textline(L"lineto(), arc(), and", xt, y+20, L"fillcolor={gray 0}"); p.fit_textline(L"closepath_fill_stroke()", xt, y, L"fillcolor={gray 0}"); /* ----------------------------------------------------------------- * Draw a rectangle and use it as the clipping a path. Draw and fill * a circle and clip it according to the clipping path defined. * ----------------------------------------------------------------- */ /* Save the current graphics state including the current clipping * path which is set to the entire page by default */ p.save(); /* Draw a rectangle at position (x, y) with a width of 200 and a height * of 50 */ p.rect(x, y-=dy, 200, 50); /* Use the current path as the clipping path for subsequent operations */ p.clip(); /* Draw a circle at position (x, y) with a radius of 100 */ p.circle(x, y, 80); /* Fill the path with the current fill color and clear it */ p.fill(); /* Restore the graphics state which has been saved above */ p.restore(); /* Output some descriptive black text */ p.fit_textline(L"rect(), clip(),", xt, y+20, L"fillcolor={gray 0}"); p.fit_textline(L"circle(), and fill()", xt, y, L"fillcolor={gray 0}"); p.end_page_ext(L""); p.end_document(L""); } catch (PDFlib::Exception &ex) { wcerr << L"PDFlib exception occurred:" << endl << L"[" << ex.get_errnum() << L"] " << ex.get_apiname() << L": " << ex.get_errmsg() << endl; return 2; } return 0; }
int main(void) { try { /* This is where the data files are. Adjust as necessary. */ const wstring searchpath = L"../data"; const wstring outfile = L"starter_color.pdf"; PDFlib p; int font, spot; int y = 800, x = 50, xoffset1=80, xoffset2 = 100, yoffset = 70, r = 30; double icchandle; p.set_parameter(L"SearchPath", searchpath); /* This means we must check return values of load_font() etc. */ p.set_parameter(L"errorpolicy", L"return"); if (p.begin_document(outfile, L"") == -1) { wcerr << L"Error: " << p.get_errmsg() << endl; return 2; } p.set_info(L"Creator", L"PDFlib starter sample"); p.set_info(L"Title", L"starter_color"); /* Load the font */ font = p.load_font(L"Helvetica", L"winansi", L""); if (font == -1) { wcerr << L"Error: " << p.get_errmsg() << endl; return 2; } /* Start the page */ p.begin_page_ext(0, 0, L"width=a4.width height=a4.height"); p.setfont(font, 14); /* ------------------------------------------------------------------- * Use default colors * * If no special color is set the default values will be used. The * default values are restored at the beginning of the page. * 0=black in the Gray color space is the default fill and stroke * color in many cases, as shown in our sample. * ------------------------------------------------------------------- */ /* Fill a circle with the default black fill color */ p.circle(x, y-=yoffset, r); p.fill(); /* Output text with default black fill color */ p.fit_textline( L"Circle and text filled with default color {gray 0}", x+xoffset2, y, L""); p.fit_textline(L"1.", x+xoffset1, y, L""); /* ------------------------------------------------------------------- * Use the Gray color space * * Gray color is defined by Gray values between 0=black and 1=white. * ------------------------------------------------------------------- */ /* Using setcolor(), set the current fill color to a light gray * represented by (0.5, 0, 0, 0) which defines 50% gray. Since gray * colors are defined by only one value, the last three function * parameters must be set to 0. */ p.setcolor(L"fill", L"gray", 0.5, 0, 0, 0); /* Fill a circle with the current fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); /* Output text with the current fill color */ p.fit_textline(L"Circle and text filled with {gray 0.5}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"2.", x+xoffset1, y, L"fillcolor={gray 0.5}"); /* -------------------------------------------------------------------- * Use the RGB color space * * RGB color is defined by RGB triples, i.e. three values between 0 and * 1 specifying the percentage of red, green, and blue. * (0, 0, 0) is black and (1, 1, 1) is white. The commonly used RGB * color values in the range 0...255 must be divided by 255 in order to * scale them to the range 0...1 as required by PDFlib. * -------------------------------------------------------------------- */ /* Use setcolor() to set the fill color to a grass-green * represented by (0.1, 0.95, 0.3, 0) which defines 10% red, 95% green, * 30% blue. Since RGB colors are defined by only three values, the last * function parameter must be set to 0. */ p.setcolor(L"fill", L"rgb", 0.1, 0.95, 0.3, 0); /* Draw a circle with the current fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); /* Output a text line with the RGB fill color defined above */ p.fit_textline(L"Circle and text filled with {rgb 0.1 0.95 0.3}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"3.", x+xoffset1, y, L"fillcolor={rgb 0.1 0.95 0.3}"); /* -------------------------------------------------------------------- * Use the CMYK color space * * CMYK color is defined by four CMYK values between 0 = no color and * 1 = full color representing cyan, magenta, yellow, and black values; * (0, 0, 0, 0) is white and (0, 0, 0, 1) is black. * -------------------------------------------------------------------- */ /* Use setcolor() to set the current fill color to a pale * orange, represented by (0.1, 0.7, 0.7, 0.1) which defines 10% Cyan, * 70% Magenta, 70% Yellow, and 10% Black. */ p.setcolor(L"fill", L"cmyk", 0.1, 0.7, 0.7, 0.1); /* Fill a circle with the current fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); /* Output a text line with the CMYK fill color defined above */ p.fit_textline(L"Circle and text filled with {cmyk 0.1 0.7 0.7 0.1}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"4.", x+xoffset1, y, L"fillcolor={cmyk 0.1 0.7 0.7 0.1}"); /* -------------------------------------------------------------------- * Use a Lab color * * Device-independent color in the CIE L*a*b* color space is specified * by a luminance value in the range 0-100 and two color values in the * range -127 to 128. The first value contains the green-red axis, * while the second value contains the blue-yellow axis. * -------------------------------------------------------------------- */ /* Set the current fill color to a loud blue, represented by * (100, -127, -127, 0). Since Lab colors are defined by only three * values, the last function parameter must be set to 0. */ p.setcolor(L"fill", L"lab", 100, -127, -127, 0); /* Fill a circle with the fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); /* Output a text line with the Lab fill color defined above */ p.fit_textline(L"Circle and text filled with {lab 100 -127 -127}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"5.", x+xoffset1, y, L"fillcolor={lab 100 -127 -127}"); /* --------------------------------------------------------------- * Use an ICC based color * * ICC-based colors are specified with the help of an ICC profile. * --------------------------------------------------------------- */ /* Load the sRGB profile. sRGB is guaranteed to be always available */ icchandle = p.load_iccprofile(L"sRGB", L"usage=iccbased"); /* Set the sRGB profile. (Accordingly, you can use * L"setcolor:iccprofilergb" or L"setcolor:iccprofilegray" with an * appropriate profile) */ p.set_value(L"setcolor:iccprofilergb", icchandle); /* Use setcolor() with the L"iccbasedrgb" color space to set the current * fill and stroke color to a grass-green, represented * by the RGB color values (0.1 0.95 0.3 0) which define 10% Red, * 95% Green, and 30% Blue. Since iccbasedrgb colors are defined by only * three values, the last function parameter must be set to 0. */ p.setcolor(L"fill", L"iccbasedrgb", 0.1, 0.95, 0.3, 0); /* Fill a circle with the ICC based RGB fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); /* Output a text line with the ICC based RGB fill color defined above */ p.fit_textline( L"Circle and text filled with {iccbasedrgb 0.1 0.95 0.3}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"6.", x+xoffset1, y, L"fillcolor={iccbasedrgb 0.1 0.95 0.3}"); /* -------------------------------------------------------------------- * Use a spot color * * Spot color (separation color space) is a predefined or arbitrarily * named custom color with an alternate representation in one of the * other color spaces above; this is generally used for preparing * documents which are intended to be printed on an offset printing * machine with one or more custom colors. The tint value (percentage) * ranges from 0 = no color to 1 = maximum intensity of the spot color. * -------------------------------------------------------------------- */ /* Define the spot color L"PANTONE 281 U" from the builtin color * library PANTONE */ spot = p.makespotcolor(L"PANTONE 281 U"); /* Set the spot color L"PANTONE 281 U" with a tint value of 1 (=100%) * and output some text. Since spot colors are defined by only two * values, the last two function parameters must be set to 0. */ p.setcolor(L"fill", L"spot", spot, 1.0, 0, 0); /* Fill a circle with the ICC based RGB fill color defined above */ p.circle(x, y-=yoffset, r); p.fill(); p.fit_textline( L"Circle and text filled with {spotname {PANTONE 281 U} 1}", x+xoffset2, y, L""); /* Alternatively, you can set the fill color in the call to * fit_textline() using the L"fillcolor" option. This case applies the * fill color just the single function call. The current fill color * won't be affected. */ p.fit_textline(L"7.", x+xoffset1, y, L"fillcolor={spotname {PANTONE 281 U} 1}"); /* or */ wostringstream buf; buf.str(L""); buf << L"fillcolor={spot " << spot << L" 1}"; p.fit_textline(L"7.", x+xoffset1, y, buf.str()); /* ---------------------------------------------------------- * For using the Pattern color space, see the Cookbook topics * graphics/fill_pattern and images/background_pattern. * ---------------------------------------------------------- */ /* --------------------------------------------------------- * For using the Shading color space, see the Cookbook topic * color/color_gradient. * --------------------------------------------------------- */ p.end_page_ext(L""); p.end_document(L""); } catch (PDFlib::Exception &ex) { wcerr << L"PDFlib exception occurred:" << endl << L"[" << ex.get_errnum() << L"] " << ex.get_apiname() << L": " << ex.get_errmsg() << endl; return 2; } return 0; }