Click here to Skip to main content
15,868,030 members
Articles / Programming Languages / C++

SolidWidgets PDF

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

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:

  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:

C++
// Start a new PDF document
// swFrame::setLicenseFile(L"licensefile.swl",L"");
swPDF pdfDoc;
// Set the page dimensions to 8.5x11
pdfDoc.setPageWidth(850);
pdfDoc.setPageHeight(1100);
//
// 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);
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);
//
// Render a bitmap handle to the current page, the bitmap will stretch to occupy the entire page
pdfDoc.renderBitmapFullPage(hBitmap);
//
// Start a new page
pdfDoc.newPage();
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;
pdfDoc.renderBitmap(hBitmap,imageX,0);
//
// Start a new page
pdfDoc.newPage();
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
pdfDoc.setFont(swFONT_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;
}
//
DeleteDC(hMemDC);
DeleteObject(hBitmap);
ReleaseDC(GetDesktopWindow(),hDC);
// Finally save the PDF document to disk
pdfDoc.save("c:\\temp\\MyPDFDoc.pdf");

Final outcome:

Login

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!

License

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 --