How to Create PDF Files With C Source Code

Creating Adobe PDF (Portable Document Format) files with an application written in the C programming language could potentially involve many hours of deciphering Adobe's documentation. But, at least one open source (free) project has done that work already to produce a library with which your C programs can link to create PDF files.

Things You'll Need

  • Microsoft Visual C++, Express (or Full) edition
  • Haru PDF library from libharu.org
  • Zlib DLL file (a dependency of the Haru library) from zlib.net
Show More

Instructions

    • 1

      Download the Haru Free PDF Library from Libharu.org. Select the libharu-<version>-vc6.zip file for download, which is under the "Windows Binaries" heading. Download a dependency of the Haru library, the Zlib, from the Zlib link in this article's Resources section.

    • 2

      Create a new Visual C++ Windows32 Console Application.
      Enter "pdfmkr" for the project's name and "C:\Users\<YourUserName>\Desktop\pdfmkr" for its location. Do not check the "Create directory" checkbox.

    • 3

      Install the library files: unzip the libharu-2.1.0-vc6.zip and zlib123-dll.zip files to the default locations assigned by your unzipping application.

      Copy the libhpdf.lib and libhpdf.exp files from the lib subfolder of the unzipped libharu zip file, to C:\Users\<YourUserName>\Desktop\pdfmkr\pdfmkr.

    • 4

      Copy the zdll.lib, zdll.exp and zdll.def files from the unzipped Zlib's lib subfolder to the same destination folder referenced in Step 3. Also, copy the zlib1.dll file from the _root_ folder (not the lib subfolder) of the Zlib unzipped folder, to Step 3's destination folder.

    • 5

      Copy the header files to where the Visual C++ compiler can find them: copy all header (file extension *.h) files from the libharu "include" subfolder, to the same folder from Step 3. Copy all Zlib header files from the Zlib "include" subfolder, to Step 3's destination folder.

    • 6

      Create references to the lib files you just copied so the Visual C++ linker can actually see them: in the Visual C++ IDE, select "View>Property Manager." In the Property Manager window, right-click "pdfmkr" and select "Properties." Expose the "Linker>Input" node by clicking "Configuration Properties," then "Linker." In the "Additional Dependencies" row, enter the names of the library files you just copied: "zdll.lib" and "libhpdf.lib" (but not the DLL file).

    • 7

      Paste the following source code into the Visual C++ code window:

      //////////////////////////////////////////////
      #include "stdafx.h"
      #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>
      #include <setjmp.h>
      #include "hpdf.h"

      jmp_buf env;

      #ifdef HPDF_DLL
      void __stdcall
      #else
      void
      #endif
      cbHandleErr (HPDF_STATUS errNum,
      HPDF_STATUS detail_num,
      void *userinfo)
      {
      printf ("ERROR: errNum=%04X, detail_num=%u\n", (HPDF_UINT)errNum,
      (HPDF_UINT)detail_num);
      longjmp(env, 1);
      }

      const char fname [] = "myPDF.PDF";

      int main (int argc, char **argv)
      {
      HPDF_Doc objPDF;
      HPDF_Page pdfPage;
      HPDF_Font font;
      HPDF_REAL pageHeight;

      objPDF = HPDF_New (cbHandleErr, NULL);
      if (!objPDF) {
      printf ("Can't create PdfDoc object\n");
      return 1;
      }

      if (setjmp(env)) {
      HPDF_Free (objPDF);
      return 1;
      }

      /* Create pdfPage */
      pdfPage = HPDF_AddPage (objPDF);
      pageHeight = HPDF_Page_GetHeight (pdfPage);

      /* Put some text on pdfPage */
      font = HPDF_GetFont (objPDF, "Times-Roman", NULL);
      HPDF_Page_SetFontAndSize (pdfPage, font, 24);
      HPDF_Page_BeginText (pdfPage);
      HPDF_Page_TextOut (pdfPage, 20, pageHeight-75, "My first PDF"); //yes
      HPDF_Page_EndText (pdfPage);
      HPDF_SaveToFile (objPDF, fname);
      HPDF_Free (objPDF);

      return 0;
      }

      //////////////////////////////////////////////

    • 8

      Press F5 to compile and run the program. Look for the output file, myPDF.PDF, in the folder referenced in Step 3.

Tips & Warnings

  • For Step 3, install all lib and *.h files from the Zlib and libhpdf zip files to the folder containing your project's stdafx.h header file. That's where Visual C++ looks for include and library files when building an app in debug/development mode. If you don't see stdafx.h in the folder mentioned in Step 3, use Windows Explorer to search for it.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured