Click here to Skip to main content
15,614,665 members
Articles / Programming Languages / C++
Posted 19 Jul 2012

Tagged as


4 bookmarked

SolidWidgets PDF

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
19 Jul 2012CPOL2 min read
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.



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:

  1. Create the page from a bitmap or a PNG file.
  2. 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:

// Start a new PDF document
// swFrame::setLicenseFile(L"licensefile.swl",L"");
swPDF pdfDoc;
// Set the page dimensions to 8.5x11
// Create a sample HBITMAP object
long bitmapWidth = 425;
long bitmapHeight = 550;
HDC hDC = GetDC(GetDesktopWindow());
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, bitmapWidth, bitmapHeight);
RECT pageRect;
pageRect.left = 0; = 0;
pageRect.right = bitmapWidth;
pageRect.bottom = bitmapHeight;
DrawText(hMemDC,L"Hello There",wcslen(L"Hello There"),&pageRect,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
// Render a bitmap handle to the current page, the bitmap will stretch to occupy the entire page
// Start a new page
long pageWidth = pdfDoc.getPageWidth();
long pageHeight = pdfDoc.getPageHeight();
// Render a bitmap centered horizontally at the top of the page,
// maintaining the original bitmap dimensions.
long imageX = (pageWidth-bitmapWidth)/2;
// Start a new page
pageWidth = pdfDoc.getPageWidth();
pageHeight = pdfDoc.getPageHeight();
// Custom-Render the page with a grid of 2 columns, and as many rows as can be fit on the page.
// Set the font to Courier/10
// render the table
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); // 0: center, -1: left, 1: right
    pdfDoc.drawText(midX,curY,midX,textHeight,"Column-2 Data",0);
    curY+= textHeight;
// Finally save the PDF document to disk"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 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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

-- There are no messages in this forum --