Free for PERSONAL and COMMERCIAL use
SolidWidgets UI framework includes a basic PDF processing class, which supports the creation of PDF documents programmatically. One of the
unique features of this implementation is that it treats y-coordinates the same way they work in Windows. Most of the PDF libraries out there have the zero y-coordinate
starting from the bottom of the page, which makes it very difficult for developers to figure out where to position objects on the page because they would have to think about
the placement opposite to what they are used to when they layout a window under most operating systems. Using the PDF functionality in SolidWidgets, I no longer have to think
different from what I am used to when laying out a page, whether the page represents a window or a PDF document. That said, let us dig into the actual API available in this class.
Using the SolidWidgets PDF Class
There are two ways to render a PDF page using this class:
- Create the page from a bitmap or a PNG file.
- Manually draw the objects on the page by managing the coordinates, and calling various graphics functions to draw text, lines, boxes, etc.
The first step in using this API is to instantiate an instance of
swPDF. By doing so, a new blank PDF document gets created, and a new page is automatically started.
Next, you can either render an image on the page by calling one of the
renderXXXXX functions and passing a file path or a windows bitmap handle, or you can custom draw your objects
on the page manually. Following are examples of rendering an image, and custom rendering a page:
long bitmapWidth = 425;
long bitmapHeight = 550;
HDC hDC = GetDC(GetDesktopWindow());
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, bitmapWidth, bitmapHeight);
pageRect.left = 0;
pageRect.top = 0;
pageRect.right = bitmapWidth;
pageRect.bottom = bitmapHeight;
DrawText(hMemDC,L"Hello There",wcslen(L"Hello There"),&pageRect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
long pageWidth = pdfDoc.getPageWidth();
long pageHeight = pdfDoc.getPageHeight();
long imageX = (pageWidth-bitmapWidth)/2;
pageWidth = pdfDoc.getPageWidth();
pageHeight = pdfDoc.getPageHeight();
long textHeight = pdfDoc.getTextHeight();
long numRows = pageHeight/textHeight;
long midX = pageWidth/2;
long curY = 0;
pdfDoc.drawText(0,curY,midX,textHeight,"Column-1 Data",0); pdfDoc.drawText(midX,curY,midX,textHeight,"Column-2 Data",0);
Points of Interest
As easy and simplistic as this code looks, you will start realizing the power of this framework as you dig deeper into the various components.
You can find more help on http://www.solidwidgets.com. The site includes a bunch of examples on how to use each component.
I hope this tutorial helps someone who needs to implement such functionality. Best of luck!