Introduction
Free for PERSONAL and COMMERCIAL use
The 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:
swPDF pdfDoc;
pdfDoc.setPageWidth(850);
pdfDoc.setPageHeight(1100);
long bitmapWidth = 425;
long bitmapHeight = 550;
HDC hDC = GetDC(GetDesktopWindow());
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, bitmapWidth, bitmapHeight);
SelectObject(hMemDC,hBitmap);
RECT pageRect;
pageRect.left = 0;
pageRect.top = 0;
pageRect.right = bitmapWidth;
pageRect.bottom = bitmapHeight;
FillRect(hMemDC,&pageRect,(HBRUSH)GetStockObject(WHITE_BRUSH));
SetTextColor(hMemDC,RGB(0,0,0));
DrawText(hMemDC,L"Hello There",wcslen(L"Hello There"),&pageRect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
pdfDoc.renderBitmapFullPage(hBitmap);
pdfDoc.newPage();
long pageWidth = pdfDoc.getPageWidth();
long pageHeight = pdfDoc.getPageHeight();
long imageX = (pageWidth-bitmapWidth)/2;
pdfDoc.renderBitmap(hBitmap,imageX,0);
pdfDoc.newPage();
pageWidth = pdfDoc.getPageWidth();
pageHeight = pdfDoc.getPageHeight();
pdfDoc.setFont(swFONT_COURIER,10);
long textHeight = pdfDoc.getTextHeight();
long numRows = pageHeight/textHeight;
long midX = pageWidth/2;
long curY = 0;
for(long row=0;row<numRows;row++)
{
pdfDoc.drawText(0,curY,midX,textHeight,"Column-1 Data",0); pdfDoc.drawText(midX,curY,midX,textHeight,"Column-2 Data",0);
curY+= textHeight;
}
DeleteDC(hMemDC);
DeleteObject(hBitmap);
ReleaseDC(GetDesktopWindow(),hDC);
pdfDoc.save("c:\\temp\\MyPDFDoc.pdf");
Final outcome:

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.
Conclusion
I hope this tutorial helps someone who needs to implement such functionality. Best of luck!
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.