Knowledge Base Nr: 00226 pdfsupport.cpp - http://www.swe-kaiser.de

Downloads:

win32: linux: pdf support klasse zum erzeugen und manipulieren von pdf-dateien

  
//es wird die bekannte pdflib von http://www.pdflib.com benutzt

#################################################################

class CPDFSupport
{
public:
CPDFSupport();
virtual ~CPDFSupport();

int Init(const char* lpszTemplateFile, const char* lpszOutputFile, double fWidth, double fHeight);
int Save();

int Text(double fSize, double fPosX, double fPosY, const char* lpszText);
int Punkt(double fWidth, double fPosX, double fPosY);
int Linie(double fWidth, double fPosX1, double fPosY1, double fPosX2, double fPosY2);
int Rechteck(double fLineWidth, double fPosX, double fPosY, double fWidth, double fHeight);
int Grafik(double fPosX, double fPosY, const char* lpszGrafikFile, double fSizeX=0.0, double fSizeY=0.0);
//size=0 benutzt dateiinterne grössen - bei jpgs gibt man am besten fSizeX/fSizeY in mm an!
int SetColor(const char* lpszType, int nRed, int nGreen, int nBlue);
//lpszType => fill(flächen), stroke(linien) oder fillstroke(beides)
...
};

#################################################################

void CAvrtestDlg::OnPrint()
{
...
CPDFSupport pdf;
const char* lpszTemplateFile = "./printin.pdf";
const char* lpszOutputFile = "./printout.pdf";

//falls lpszOutputFile noch geöffnet erst schliessen
CWnd* pWnd = FindWindow(NULL, "Adobe Reader - [printout.pdf]");
if (pWnd)
pWnd->SendMessage(WM_CLOSE);

int nErr = pdf.Init(lpszTemplateFile, lpszOutputFile, a4_height, a4_width); //a4quer

//y-achse
for (int y=0; y<6; y++)
{
pdf.Linie(PDF_LINEWIDTH, PDF_XOFFS, PDF_YOFFS+y*10, PDF_XOFFS+PDF_WIDTH, PDF_YOFFS+y*10);
...
pdf.Text(PDF_TEXTWIDTH, PDF_XOFFS-10, PDF_YOFFS+y*10, strText);
pdf.Text(PDF_TEXTWIDTH, PDF_XOFFS+PDF_WIDTH+1.0, PDF_YOFFS+y*10, strText);
}

//messwerte
nErr = pdf.SetColor("fillstroke", 255, 0, 0);

bool bFill = IsDlgButtonChecked(IDC_FILL) ? true : false;

for (int nCount = 0; nCount < g_nMaxSamples; nCount++)
{
int nCode = g_nValue[nCount];
...
double fXPos = nCount * ((0.0+PDF_WIDTH) / g_nMaxSamples);
double fYPos = nCode * (50.0 / 255.0);

if (bFill)
pdf.Linie(PDF_VALUEWIDTH, PDF_XOFFS + fXPos, PDF_YOFFS + fYPos, PDF_XOFFS + fXPos, PDF_YOFFS - 0.1);
else
pdf.Punkt(PDF_VALUEWIDTH, PDF_XOFFS + fXPos, PDF_YOFFS + fYPos);
}

nErr = pdf.Grafik(PDF_XOFFS, PDF_YOFFS - 40, "./test.jpg", 35, 28);
ASSERT(!nErr);

nErr = pdf.Save();
ASSERT(!nErr);

str.Format("\"C:\\Programme\\Adobe\\Acrobat 7.0\\Reader\\AcroRd32.exe\" \"%s\"", lpszOutputFile);
UINT nRes = WinExec(str, SW_HIDE);
...
}

#################################################################

#include <afxwin.h>
#include "pdfsupport.h"

#pragma comment(lib, "../cpp_classes/pdflib/pdflib.lib")
//http://www.pdflib.com

CPDFSupport::CPDFSupport()
{
//TRACE("CPDFSupport::CPDFSupport()\n");
m_pdf = 0;
}


CPDFSupport::~CPDFSupport()
{
//TRACE("CPDFSupport::~CPDFSupport()\n");
if (m_pdf)
{
PDF_delete(m_pdf);
m_pdf = 0;
}
}


int CPDFSupport::Init(const char* lpszTemplateFile, const char* lpszOutputFile, double fWidth, double fHeight)
{
//TRACE("CPDFSupport::Init(%s)\n", lpszTemplateFile);

if (m_pdf)
{
TRACE("WARNING: PDFlib object war nicht leer!\n");
ASSERT(FALSE);
PDF_delete(m_pdf);
}

if ((m_pdf = PDF_new()) == (PDF*) 0)
{
TRACE("ERROR: Couldn't create PDFlib object (out of memory)!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
if (PDF_begin_document(m_pdf, lpszOutputFile, 0, "") == -1)
{
TRACE("ERROR: %s\n", PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
return -3;
}
PDF_set_info(m_pdf, "Creator", "libpdf");
PDF_set_info(m_pdf, "Author", "a.kaiser (c)2004");
PDF_set_info(m_pdf, "Title", "PDF Dokument!");

int hPDI = PDF_open_pdi(m_pdf, lpszTemplateFile, 0, 0);
if (hPDI < 0)
{
TRACE("ERROR: %s\n", PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
return -4;
}

int hPage = PDF_open_pdi_page(m_pdf, hPDI, 1, "");
if (hPage < 0)
{
TRACE("ERROR: %s\n", PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
return -4;
}

PDF_begin_page_ext(m_pdf, fWidth, fHeight, "");
PDF_place_pdi_page(m_pdf, hPage, 0.0, 0.0, 1.0, 1.0);
PDF_close_pdi_page(m_pdf, hPage);

//metrisches koordinatensystem in mm
PDF_scale(m_pdf, 2.83465, 2.83465);
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Init() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}


int CPDFSupport::SetColor(const char* lpszType, int nRed, int nGreen, int nBlue)
{
//TRACE("CPDFSupport::SetColor(%s, %d, %d, %d)\n", lpszType, nRed, nGreen, nBlue);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
PDF_setcolor(m_pdf, lpszType, "rgb", (0.0+nRed)/255, (0.0+nGreen)/255, (0.0+nBlue)/255, 0);
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: SetColor() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}


int CPDFSupport::Save()
{
//TRACE("CPDFSupport::Save()\n");

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
PDF_end_page_ext(m_pdf, "");
PDF_end_document(m_pdf, "");
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Save() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

PDF_delete(m_pdf);
m_pdf = 0;
return 0;
}


int CPDFSupport::Punkt(double fWidth, double fPosX, double fPosY)
{
//TRACE("CPDFSupport::Punkt(%f,%f,%f)\n", fWidth, fPosX, fPosY);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
PDF_setlinewidth(m_pdf, fWidth/2.0);
PDF_circle(m_pdf, fPosX, fPosY, fWidth/2.0);
PDF_stroke(m_pdf);

/*
PDF_setlinewidth(m_pdf, fWidth);
PDF_moveto(m_pdf, fPosX, fPosY);
PDF_lineto(m_pdf, fPosX+fWidth, fPosY);
PDF_stroke(m_pdf);
*/
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Punkt() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}

int CPDFSupport::Rechteck(double fLineWidth, double fPosX, double fPosY, double fWidth, double fHeight)
{
TRACE("CPDFSupport::Rechteck(%f, %f, %f, %f, %f)\n", fLineWidth, fPosX, fPosY, fWidth, fHeight);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
PDF_setlinewidth(m_pdf, fLineWidth);
PDF_rect(m_pdf, fPosX, fPosY, fWidth, fHeight);
PDF_stroke(m_pdf);
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Rechteck() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}

int CPDFSupport::Linie(double fWidth, double fPosX1, double fPosY1, double fPosX2, double fPosY2)
{
//TRACE("CPDFSupport::Linie(%f,%f,%f,%f,%f)\n", fWidth, fPosX1, fPosY1, fPosX2, fPosY2);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
PDF_setlinewidth(m_pdf, fWidth);
PDF_moveto(m_pdf, fPosX1, fPosY1);
PDF_lineto(m_pdf, fPosX2, fPosY2);
PDF_stroke(m_pdf);
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Linie() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}

int CPDFSupport::Text(double fSize, double fPosX, double fPosY, const char* lpszText)
{
//TRACE("CPDFSupport::Text(%f,%f,%f,<%s>)\n", fSize, fPosX, fPosY, lpszText);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
int font = PDF_load_font(m_pdf, "Helvetica", 0, "host", "");
PDF_setfont(m_pdf, font, fSize);
PDF_set_text_pos(m_pdf, fPosX, fPosY);
PDF_show(m_pdf, lpszText);
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Text() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}

int CPDFSupport::Grafik(double fPosX, double fPosY, const char* lpszGrafikFile, double fSizeX, double fSizeY)
{
//TRACE("CPDFSupport::Grafik(%f,%f,%f,%f,<%s>)\n", fPosX, fPosY, fSizeX, fSizeY, lpszGrafikFile);

if (!m_pdf)
{
TRACE("ERROR: PDFlib object nicht initialisiert!\n");
ASSERT(FALSE);
return -1;
}

PDF_TRY(m_pdf)
{
int hImage = PDF_load_image(m_pdf, "auto", lpszGrafikFile, 0, "");
char szParam[100] = {0};

if ((fSizeX >= 0) && (fSizeY >= 0))
sprintf(szParam, "boxsize={%f %f} fitmethod=meet", fSizeX, fSizeY);
else
sprintf(szParam, "fitmethod=nofit", 0, 0);

PDF_fit_image(m_pdf, hImage, fPosX, fPosY, szParam); //boxsize={50 10} fitmethod entire
}
PDF_CATCH(m_pdf)
{
TRACE("ERROR: Grafik() PDFlib exception: [%d] %s: %s\n", PDF_get_errnum(m_pdf), PDF_get_apiname(m_pdf), PDF_get_errmsg(m_pdf));
ASSERT(FALSE);
PDF_delete(m_pdf);
m_pdf = 0;
return -2;
}

return 0;
}