Click here to Skip to main content
Click here to Skip to main content

Tagged as

SolidWidgets PDF

, 19 Jul 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

Jake Franta

United States United States
No Biography provided

Comments and Discussions

 
QuestionLandscape PinmemberKalpana Volety11-Jan-13 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 19 Jul 2012
Article Copyright 2012 by Jake Franta
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid