开发环境:VC++6.0
PDF开发包:PDFlib-Lite-7.0.5
开发demo:
/*
* PDFlib client: hello example in C
* pdflib-BarCode
*/
#include
#include
#include
#include "pdflib.h"
#include "Code39.h"
FILE *fp;
void drawTable(PDF *p, double x0, double y0, double height, double width);
void addText(PDF *p, double x0, double y0, char *barCode);
int main(void)
{
PDF *p;
int font;
double startX,startY;
double pageEdgeX,pageEdgeY;
double tableEdgeX,tableEdgeY;
double tableHeight, tableWidth;
/* 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, "hypertextencoding", "host"); //--(增加位置)
if (PDF_begin_document(p, "hello.pdf", 0, "") == -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", "hello.c");
PDF_set_info(p, "Author", "Thomas Merz");
PDF_set_info(p, "Title", "Hello, world (C)!");
PDF_begin_page_ext(p, a4_width, a4_height, "");
/* Change "host" encoding to "winansi" or whatever you need! */
font = PDF_load_font(p, "Helvetica-Bold", 0, "host", "");
if (font == -1) {
printf("Error: %s\n", PDF_get_errmsg(p));
PDF_delete(p);
return(2);
}
PDF_save(p);
//#define a4_width 595.0
//#define a4_height 842.0
//左下角为(0,0)
//x横向,y纵向
pageEdgeX = 20;
pageEdgeY = 20;
tableEdgeX = 10;
tableEdgeY = 20;
tableHeight = 30;
tableWidth = 180;
fp = fopen("out.txt", "w");
if(!fp)
{
printf("file errpr!\n");
return -1;
}
startX = pageEdgeX;
startY = a4_height-pageEdgeY;
drawTable(p, startX, startY, tableHeight, tableWidth);
addText(p, startX, startY, "SN301");
drawTable(p, startX, startY-(tableEdgeX+tableHeight), tableHeight, tableWidth);
addText(p, startX, startY-(tableEdgeX+tableHeight), "SN302");
drawTable(p, startX, startY-2*(tableEdgeX+tableHeight), tableHeight, tableWidth);
addText(p, startX, startY-2*(tableEdgeX+tableHeight), "SN303");
PDF_restore(p);
PDF_end_page_ext(p, "");
PDF_end_document(p, "");
}
PDF_CATCH(p) {
printf("PDFlib exception occurred in hello 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;
}
void drawTable(PDF *p, double x0, double y0, double height, double width)
{
PDF_setcolor(p, "fillstroke", "rgb", 0, 0, 0, 0);
PDF_setlinewidth(p, 0.1);
//横向
PDF_moveto(p, x0, y0);
PDF_lineto(p, x0+width, y0);
PDF_stroke(p);
PDF_moveto(p, x0, y0-height);
PDF_lineto(p, x0+width, y0-height);
PDF_stroke(p);
//竖向
PDF_moveto(p, x0, y0);
PDF_lineto(p, x0, y0-height);
PDF_stroke(p);
PDF_moveto(p, x0+50, y0);
PDF_lineto(p, x0+50, y0-height);
PDF_stroke(p);
PDF_moveto(p, x0+width, y0);
PDF_lineto(p, x0+width, y0-height);
PDF_stroke(p);
}
void addText(PDF *p, double x0, double y0, char *barCode)
{
int font, font_ch;
double posX,posY;
font = PDF_load_font(p, "Helvetica", 0, "host", "");
if (font == -1) {
printf("Error: %s\n", PDF_get_errmsg(p));
PDF_delete(p);
return;
}
font_ch = PDF_load_font(p, "STSong-Light", 0, "GB-EUC-H", "");
if (font == -1) {
printf("Error: %s\n", PDF_get_errmsg(p));
PDF_delete(p);
return;
}
///*
posX = x0;
posY = y0;
PDF_save(p);
PDF_setfont(p, font_ch, 12);
PDF_set_text_pos(p, posX+5, posY-20);
PDF_show(p, "条形码");
PDF_restore(p);
//条码
char *text = barCode;
if(Code39Verify(text)==-1)
{
printf("involid char\n");
return;
}
char partial[1024]="";
if(Code39Encode(text, partial)==-1)
{
printf("code error\n");
return;
}
fwrite(partial, strlen(partial), 1, fp);
fwrite("\r\n", strlen("\r\n"), 1, fp);
double codeWid = 0.8;
double codeHig = 10;
char *str = partial;
posX = x0+60;
posY = y0-5;
PDF_setcolor(p, "fillstroke", "rgb", 0, 0, 0, 0);
PDF_setlinewidth(p, codeWid);
PDF_save(p);
while(*str)
{
for(int i=0; i<(*str-'0'); i++)
{
PDF_moveto(p, posX, posY);
PDF_lineto(p, posX, posY-codeHig);
posX += codeWid;
PDF_stroke(p);
}
str++;
posX += codeWid*(*str-'0');
str++;
}
PDF_restore(p);
//条码字符
posX = x0;
posY = y0-10;
PDF_save(p);
PDF_setfont(p, font, 8);
PDF_set_text_pos(p, posX+60, posY-15);
PDF_show(p, barCode);
PDF_restore(p);
}
// Code39.h: interface for the CCode39 class.
//
//////////////////////////////////////////////////////////////////////
//Code39编码
static char *code39set[]={
"111221211",//0
"211211112",//1
"112211112",//2
"212211111",//3
"111221112",//4
"211221111",//5
"112221111",//6
"111211212",//7
"211211211",//8
"112211211",//9
"211112112",//A
"112112112",//B
"212112111",//C
"111122112",//D
"211122111",//E
"112122111",//F
"111112212",//G
"211112211",//H
"112112211",//I
"111122211",//J
"211111122",//K
"112111122",//L
"212111121",//M
"111121122",//N
"211121121",//O
"112121121",//P
"111111222",//Q
"211111221",//R
"112111221",//S
"111121221",//T
"221111112",//U
"122111112",//V
"222111111",//W
"121121112",//X
"221121111",//Y
"122121111",//Z
"121111212",//-
"221111211",//.
"122111211",//Space
"121212111",//$
"121211121",///
"121112121",//+
"111212121",//%
"121121211",//*
};
int Code39Encode(char* text, char* partial);
int Code39Verify(char* text);
// Code39.cpp: implementation of the CCode39 class.
//
//////////////////////////////////////////////////////////////////////
#include
#include "Code39.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Code39Verify(char* text)
{
if(text[0]=='\0')
return -1;
while(*text&&((*text>='A'&&*text<='Z')||
(*text>='0'&&*text<='9')||
(*text=='-')||
(*text=='.')||
(*text=='$')||
(*text==' ')||
(*text=='/')||
(*text=='+')||
(*text=='%')||
(*text=='*')))
text++;
if(*text)
return -1;
return 0;//OK
}
int Code39Encode(char* text, char* partial)
{
unsigned i;
char *str = text;
if(!partial)
{
return -1;
}
//首字符为*
strcat(partial,code39set[43]);
strcat(partial,"2");
//如果字符串为空,则返回
if(!(*text))
{
return -1;
}
for(i=0;i='A') && (*str<='Z'))
strcat(partial,code39set[*str-'A'+10]);
else if((*str>='0') && (*str<='9'))
strcat(partial,code39set[*str-'0']);
else if(*str=='-')
strcat(partial,code39set[36]);
else if(*str=='.')
strcat(partial,code39set[37]);
else if(*str==' ')
strcat(partial,code39set[38]);
else if(*str=='$')
strcat(partial,code39set[39]);
else if(*str=='/')
strcat(partial,code39set[40]);
else if(*str=='+')
strcat(partial,code39set[41]);
else if(*str=='%')
strcat(partial,code39set[42]);
else if(*str=='*')
strcat(partial,code39set[43]);
else
{
return -1;
}
str++;
//每两个字符之间加一个白线
strcat(partial,"2");
}
//结束符为*
strcat(partial,code39set[43]);
return 0;
}
VC工程下载:http://download.csdn.net/detail/zhuyonghao123/7539803