Xojo Conferences
MBSOct2019CologneDE

DynaPDF Manual - Page 257

Previous Page 256   Index   Next Page 258

Function Reference
Page 257 of 770
Examples (C):
Example 1: Create a PDF file in memory:
// First we declare an error callback function that is called if an
// error occurred.
SI32 PDF_CALL PDFError(const void* Data, SI32 ErrCode, const char*
ErrMessage, SI32 ErrType)
{
printf("%s\n", ErrMessage);
return 0;
}
int main(int argc, char* argv[])
{
FILE* f; char* buffer; UI32 bufSize;
PPDF* pdf = pdfNewPDF();
if (!pdf) return 2; // Out of memory?
// No need to check return values.
pdfSetOnErrorProc(pdf, NULL, (void*)PDFError);
pdfSetDocInfo(pdf, diAuthor, "Jens Boschulte");
pdfSetDocInfo(pdf, diCreator, "C sample project");
pdfSetDocInfo(pdf, diSubject, "How to create a PDF file in memory?");
pdfCreateNewPDF(pdf, NULL);
pdfAppend(pdf);
pdfSetFont(pdf, "Arial", fsItalic, 40.0, true, cp1252);
pdfWriteFText(pdf, taCenter, "My first memory based PDF...");
pdfEndPage(pdf);
pdfCloseFile(pdf);
// The file must be opened in binary mode!
if ((f = fopen("c:/cout.pdf", "wb")) == NULL)
{
pdfDeletePDF(pdf);
printf("Cannot open output file!");
return 3;
}
buffer = pdfGetBuffer(pdf, &bufSize); // Get the file buffer
if (buffer)
{
// We write the buffer as it is to the file.
fwrite(buffer, 1, bufSize, f);
fclose(f);
}
// We created a new PDF instance inside the function. When the
// instance is deleted all used resources are freed. However, in most
// cases it is a better way to use one PDF instance for multiple PDF
// files to improve processing speed. In the latter case we must call
// pdfFreePDF(pdf); to free the internal used resources.
 

Previous topic: CreateNewPDF

Next topic: CreateOCG