Haru Free PDF Library
text_demo2.c
/*
* << Alternative PDF Library 1.0.0 >> -- text_demo2.c
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "hpdf.h"
#include "grid_sheet.h"
#include "handler.h"
#include "utils.h"
int no = 0;
void
PrintText(HPDF_Page page)
{
char buf[512];
no++;
HPDF_snprintf (buf, 512, ".[%d]%0.2f %0.2f", no, pos.x, pos.y);
HPDF_Page_ShowText(page, buf);
}
int
main (int argc, char **argv)
{
HPDF_Doc pdf;
HPDF_Page page;
char fname[256];
HPDF_Font font;
float angle1;
float angle2;
float rad1;
float rad2;
HPDF_Rect rect;
int i;
const char* SAMP_TXT = "The quick brown fox jumps over the lazy dog. ";
strcpy (fname, argv[0]);
strcat (fname, ".pdf");
pdf = HPDF_New (demo_error_handler, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
if (setjmp(env)) {
HPDF_Free (pdf);
return 1;
}
/* add a new page object. */
page = HPDF_AddPage (pdf);
print_grid (pdf, page);
font = HPDF_GetFont (pdf, "Helvetica", NULL);
/* text_rect method */
/* HPDF_TALIGN_LEFT */
rect.left = 25;
rect.top = 545;
rect.right = 200;
rect.bottom = rect.top - 40;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_LEFT");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
/* HPDF_TALIGN_RIGTH */
rect.left = 220;
rect.right = 395;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_RIGTH");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_RIGHT, NULL);
/* HPDF_TALIGN_CENTER */
rect.left = 25;
rect.top = 475;
rect.right = 200;
rect.bottom = rect.top - 40;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_CENTER");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_CENTER, NULL);
/* HPDF_TALIGN_JUSTIFY */
rect.left = 220;
rect.right = 395;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "HPDF_TALIGN_JUSTIFY");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_JUSTIFY, NULL);
/* Skewed coordinate system */
angle1 = 5;
angle2 = 10;
rad1 = angle1 / 180 * HPDF_PI;
rad2 = angle2 / 180 * HPDF_PI;
HPDF_Page_Concat (page, 1, tan(rad1), tan(rad2), 1, 25, 350);
rect.left = 0;
rect.top = 40;
rect.right = 175;
rect.bottom = 0;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Skewed coordinate system");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
/* Rotated coordinate system */
angle1 = 5;
rad1 = angle1 / 180 * HPDF_PI;
HPDF_Page_Concat (page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), 220, 350);
rect.left = 0;
rect.top = 40;
rect.right = 175;
rect.bottom = 0;
HPDF_Page_Rectangle (page, rect.left, rect.bottom, rect.right - rect.left,
rect.top - rect.bottom);
HPDF_Page_SetFontAndSize (page, font, 10);
HPDF_Page_TextOut (page, rect.left, rect.top + 3, "Rotated coordinate system");
HPDF_Page_SetFontAndSize (page, font, 13);
HPDF_Page_TextRect (page, rect.left, rect.top, rect.right, rect.bottom,
SAMP_TXT, HPDF_TALIGN_LEFT, NULL);
/* text along a circle */
HPDF_Page_Circle (page, 210, 190, 145);
HPDF_Page_Circle (page, 210, 190, 113);
angle1 = 360 / (strlen (SAMP_TXT));
angle2 = 180;
font = HPDF_GetFont (pdf, "Courier-Bold", NULL);
HPDF_Page_SetFontAndSize (page, font, 30);
for (i = 0; i < strlen (SAMP_TXT); i++) {
char buf[2];
float x;
float y;
rad1 = (angle2 - 90) / 180 * HPDF_PI;
rad2 = angle2 / 180 * HPDF_PI;
x = 210 + cos(rad2) * 122;
y = 190 + sin(rad2) * 122;
HPDF_Page_SetTextMatrix(page, cos(rad1), sin(rad1), -sin(rad1), cos(rad1), x, y);
buf[0] = SAMP_TXT[i];
buf[1] = 0;
HPDF_Page_ShowText (page, buf);
angle2 -= angle1;
}
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
HPDF_STATUS HPDF_SaveToFile(HPDF_Doc pdf, const char *filename)
Saves the current document to file.
void HPDF_Free(HPDF_Doc pdf)
Revoke a document object and all resources.
HPDF_Doc HPDF_New(HPDF_Error_Handler user_error_fn, void *user_data)
Create an instance of a document object and initialize it.
HPDF_Font HPDF_GetFont(HPDF_Doc pdf, const char *font_name, const char *encoding_name)
Get requested font object handle.
HPDF_STATUS HPDF_Page_Stroke(HPDF_Page page)
Paint current path.
HPDF_STATUS HPDF_Page_SetTextLeading(HPDF_Page page, HPDF_REAL value)
Set text leading (line spacing).
HPDF_STATUS HPDF_Page_TextOut(HPDF_Page page, HPDF_REAL xpos, HPDF_REAL ypos, const char *text)
Put text to the specified position.
HPDF_STATUS HPDF_Page_GSave(HPDF_Page page)
Save the page's current graphics state to the stack.
HPDF_STATUS HPDF_Page_SetFontAndSize(HPDF_Page page, HPDF_Font font, HPDF_REAL size)
Set the type of font and size leading.
HPDF_STATUS HPDF_Page_Rectangle(HPDF_Page page, HPDF_REAL x, HPDF_REAL y, HPDF_REAL width, HPDF_REAL height)
Append rectangle to the current path.
HPDF_STATUS HPDF_Page_Concat(HPDF_Page page, HPDF_REAL a, HPDF_REAL b, HPDF_REAL c, HPDF_REAL d, HPDF_REAL x, HPDF_REAL y)
Concatenate the page's transformation matrix and specified matrix.
HPDF_STATUS HPDF_Page_Circle(HPDF_Page page, HPDF_REAL x, HPDF_REAL y, HPDF_REAL radius)
Append circle to current path.
HPDF_STATUS HPDF_Page_ShowText(HPDF_Page page, const char *text)
Put text at the current text position on the page.
HPDF_STATUS HPDF_Page_GRestore(HPDF_Page page)
Restore graphics state which is saved by HPDF_Page_GSave().
HPDF_STATUS HPDF_Page_SetGrayStroke(HPDF_Page page, HPDF_REAL value)
Set stroking color (Gray).
HPDF_STATUS HPDF_Page_BeginText(HPDF_Page page)
Begin text object and set text position to (0, 0).
HPDF_STATUS HPDF_Page_EndText(HPDF_Page page)
Finish text object.
HPDF_STATUS HPDF_Page_TextRect(HPDF_Page page, HPDF_REAL left, HPDF_REAL top, HPDF_REAL right, HPDF_REAL bottom, const char *text, HPDF_TextAlignment align, HPDF_UINT *len)
Put text inside the specified region.
HPDF_Point HPDF_Page_GetCurrentTextPos(HPDF_Page page)
Get current position for text showing.
HPDF_Page HPDF_AddPage(HPDF_Doc pdf)
Create new page and add it after the last page of document.
HPDF_STATUS HPDF_Page_SetSize(HPDF_Page page, HPDF_PageSizes size, HPDF_PageDirection direction)
Change page size and direction to a predefined ones.
HPDF_STATUS HPDF_Page_SetTextMatrix(HPDF_Page page, HPDF_REAL a, HPDF_REAL b, HPDF_REAL c, HPDF_REAL d, HPDF_REAL x, HPDF_REAL y)
#define HPDF_PI
Definition: hpdf_consts.h:32
@ HPDF_PAGE_SIZE_A5
ISO 216 "A5" page size (148.0mm x 210.0mm)
Definition: hpdf_page_sizes.h:139
@ HPDF_TALIGN_RIGHT
Definition: hpdf_types.h:605
@ HPDF_TALIGN_LEFT
Definition: hpdf_types.h:604
@ HPDF_TALIGN_CENTER
Definition: hpdf_types.h:606
@ HPDF_TALIGN_JUSTIFY
Definition: hpdf_types.h:607
@ HPDF_PAGE_PORTRAIT
Portrait orientation (longest size vertical)
Definition: hpdf_types.h:575
Definition: hpdf_objects.h:421
Definition: hpdf_doc.h:36
Definition: hpdf_types.h:105
HPDF_REAL y
Definition: hpdf_types.h:107
HPDF_REAL x
Definition: hpdf_types.h:106
Definition: hpdf_types.h:110
HPDF_REAL top
Definition: hpdf_types.h:114
HPDF_REAL right
Definition: hpdf_types.h:113
HPDF_REAL bottom
Definition: hpdf_types.h:112
HPDF_REAL left
Definition: hpdf_types.h:111