Skip to content

Instantly share code, notes, and snippets.

@okkez
Created March 7, 2018 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okkez/32bc15544caf0750ff5d5e11db57ddaf to your computer and use it in GitHub Desktop.
Save okkez/32bc15544caf0750ff5d5e11db57ddaf to your computer and use it in GitHub Desktop.
A link in PDF
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <cairo/cairo.h>
#include <cairo/cairo-pdf.h>
void main()
{
// Creating a cairo PDF Surface
cairo_surface_t *csurface = cairo_pdf_surface_create("/home/kenji/a.pdf", 500, 400);
// Creating a cairo context
cairo_t *ctx = cairo_create(csurface);
// Creating rectangle in PDF
cairo_rectangle(ctx, 0.0, 0.0, 400, 300);
// Changing rectangle bacground color to Blue
cairo_set_source_rgb(ctx, 0.0, 0.0, 0.5);
cairo_fill(ctx);
// Moving to (10, 10) position in PDF
cairo_move_to(ctx, 10.0, 10.0);
// Changing text color to Yellow
cairo_set_source_rgb(ctx, 1.0, 1.0, 0.0);
// Writing some text to PDF
cairo_tag_begin(ctx, CAIRO_TAG_LINK, "uri='https://google.com/'");
cairo_show_text(ctx, "Hello PDF :-)");
cairo_tag_end(ctx, CAIRO_TAG_LINK);
cairo_show_page(ctx);
// Destroying cairo context
cairo_destroy(ctx);
cairo_surface_flush(csurface);
// Destroying PDF surface
cairo_surface_destroy(csurface);
// Opening PDF File
if (!fork()) {
execlp("xdg-open", "xdg-open", "/home/kenji/a.pdf", NULL);
exit(0);
}
}
@okkez
Copy link
Author

okkez commented Mar 7, 2018

$ gcc -l cairo create-pdf.c
$ ./a.out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment