Using PXC_GetContentDC  SOLVED

This Forum is for the use of Software Developers requiring help and assistance for Tracker Software's PDF-Tools SDK of Library DLL functions(only) - Please use the PDF-XChange Drivers API SDK Forum for assistance with all PDF Print Driver related topics or PDF-XChange Viewer SDK if appropriate.

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Post Reply
sjrogers
User
Posts: 10
Joined: Tue Mar 03, 2015 8:38 pm

Using PXC_GetContentDC

Post by sjrogers »

I am trying to use the PDF SDK in my application to create .pdf files by rendering directly to the device context created by PXC_GetContentDC. I am having problems getting this to work. I think my problem is that I don't understand how to correctly use the Draw Rect, Page Rect, and Page size. I have written a simple test app to try to understand how it works, but I still haven't figured it out. My output that I render with GDI does appear in the created .PDF FIle, but it is zoomed very close in, and the coordinate system I expect it to be using doesn't seem to be the case. My sample code is below. Can anyone help me understand how these rect parameters should be specified in relation to the page or what else I could be doing wrong? I would expect the graphics I drew to be completely in the page and very small, but instead is very very large and only a small part is on the page. I attached the file I produced. I did find a thread on here that talked about using the PXC_GetContentDC and had examples, but none of the file links were valid.


_PXCDocument* pdf = NULL;
if (IS_DS_FAILED(PXC_NewDocument(&pdf, "<YOUR REG KEY>", "<YOUR DEV CODE>"))) return;
PXC_SetDocumentInfoA(pdf, InfoField_Author, "GTPlot");
PXC_SetDocumentInfoA(pdf, InfoField_Title, "Test Plot");
PXC_SetDocumentInfoA(pdf, InfoField_Creator, "GTPlot");
PXC_SetDocumentInfoA(pdf, InfoField_Keywords, "GTPlot");
PXC_EnableLinkAnalyzer(pdf, TRUE);
PXC_SetCompression(pdf, FALSE, FALSE, ComprType_C_Auto, 75, ComprType_I_Auto, ComprType_M_Auto);
PXC_SetEmbeddingOptions(pdf, TRUE, TRUE, TRUE);
HRESULT res = S_OK;

_PXCPage* page = NULL;
double pageWidth = I2L(8.5);
double pageHeight = I2L(11);
res = PXC_AddPage(pdf, pageWidth, pageHeight, &page);
if (IS_DS_FAILED(res))
{
// error adding page
return;
}

_PXCContent* pContent = (_PXCContent*)page;
// Drawing rectangle
RECT drawRect;
drawRect.left = 0;
drawRect.top = 0;
drawRect.right = pageWidth;
drawRect.bottom = pageHeight;

// Document rectangle
PXC_RectF pageRect;
pageRect.left =0;
pageRect.right = pageWidth;
pageRect.top = 0;
pageRect.bottom = pageHeight;

// hdc for drawing
HDC hdc;
HRESULT hr = PXC_GetContentDC(pContent, NULL, &drawRect, &pageRect, &hdc);
if (IS_DS_FAILED(hr))
{
// Handle error
return;
}

// Now hdc could be used for GDI operations
CDC *cdc = CDC::FromHandle(hdc);
CPen red(0,1,RGB(255,0,0));
cdc->SelectObject(&red);
cdc->MoveTo(10,10);
cdc->LineTo(90,90);
cdc->MoveTo(10,90);
cdc->LineTo(90,10);
cdc->MoveTo(10,10);
cdc->LineTo(10,90);
cdc->LineTo(90,90);
cdc->LineTo(90,10);
cdc->LineTo(10,10);

// After all drawings are done hdc must be released
PXC_ReleaseContentDC(pContent, FALSE);
char fname[MAX_PATH]= "m:\\temp\\test.pdf";
DWORD fl = 0;//WEF_ShowSaveDialog;
res = PXC_WriteDocumentExA(pdf, fname, sizeof(fname), fl, NULL);
PXC_ReleaseDocument(pdf);

if (IS_DS_ERROR(res))
{
// error - to do
}
Attachments
test.pdf
(7.9 KiB) Downloaded 173 times
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17901
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using PXC_GetContentDC

Post by Tracker Supp-Stefan »

Hello sjrogers,

I've asked a colleague from the dev team to take a look but it seems like we will need to pass this to the project leader for a further investigation. We will try to post back here as soon as possible, but it seems like your objects are scaled 31.38 times.

Regards,
Stefan
sjrogers
User
Posts: 10
Joined: Tue Mar 03, 2015 8:38 pm

Re: Using PXC_GetContentDC

Post by sjrogers »

Thanks for investigating. I look forward to the response.

I have done some more experimenting to see if I could get it to work, but no luck so for.

I have tried specifying a printer for the source DC instead of using NULL (for the screen). This seems to make a small difference in the scale of what I was drawing, but I am still not getting what I would expect.

I have also tried using the following type of code to verify that the DC created by PXC_GetContentDC is like what I expect it to be in regard to size and DPI, and it is.

POINT point;

long stat=cdc->Escape(GETPRINTINGOFFSET, 0, NULL, &point);


PrinterInfo m_printerInfo;

m_printerInfo.left=point.x;
m_printerInfo.top=point.x;

cdc->Escape(GETPHYSPAGESIZE,0,NULL,&point);

m_printerInfo.pageX=point.x;
m_printerInfo.pageY=point.y;

m_printerInfo.right=cdc->GetDeviceCaps(HORZRES)+m_printerInfo.left;
m_printerInfo.bottom=cdc->GetDeviceCaps(VERTRES)+m_printerInfo.top;

m_printerInfo.dpiX=cdc->GetDeviceCaps(LOGPIXELSX);
m_printerInfo.dpiY=cdc->GetDeviceCaps(LOGPIXELSY);

I have also tried changing the mapping mode of the DC, with no successful results.

I have also tried getting the printer DC, getting it page size in inches, multiplying it by 72 for PXC_GetPageSize:

double pw1=m_printerInfo1.pageX / m_printerInfo1.dpiX * 72.0;
double ph1=m_printerInfo1.pageY / m_printerInfo1.dpiY * 72.0;
res=PXC_GetPageSize(page, &pw1, &ph1);

Then using the page size for the

double pw=m_printerInfo1.pageX;
double ph=m_printerInfo1.pageY;

// Drawing rectangle
RECT drawRect;
drawRect.left = 0;
drawRect.top = 0;
drawRect.right = pw;
drawRect.bottom = ph;

// Document rectangle
PXC_RectF pageRect;
pageRect.left =0;
pageRect.right = pw;
pageRect.top = 0;
pageRect.bottom = ph;

HDC hdc;
HRESULT hr = PXC_GetContentDC(pContent, dlg.m_pd.hDC, &drawRect, &pageRect, &hdc);



This also doesn't seem to make any difference.

I am sure there is something trivial I am doing wrong, but I don't know what it is.


Any help is appreciated.
User avatar
Ivan - Tracker Software
Site Admin
Posts: 3549
Joined: Thu Jul 08, 2004 10:36 pm
Location: Vancouver Island - Canada
Contact:

Re: Using PXC_GetContentDC

Post by Ivan - Tracker Software »

Here is your code, slightly modified:

Code: Select all

_PXCDocument* pdf = NULL;
if (IS_DS_FAILED(PXC_NewDocument(&pdf, "<YOUR REG KEY>", "<YOUR DEV CODE>"))) return;
PXC_SetDocumentInfoA(pdf, InfoField_Author, "GTPlot");
PXC_SetDocumentInfoA(pdf, InfoField_Title, "Test Plot");
PXC_SetDocumentInfoA(pdf, InfoField_Creator, "GTPlot");
PXC_SetDocumentInfoA(pdf, InfoField_Keywords, "GTPlot");
PXC_EnableLinkAnalyzer(pdf, TRUE);
PXC_SetCompression(pdf, FALSE, FALSE, ComprType_C_Auto, 75, ComprType_I_Auto, ComprType_M_Auto);
PXC_SetEmbeddingOptions(pdf, TRUE, TRUE, TRUE);
HRESULT res = S_OK;

_PXCPage* page = NULL;
double _PWi = 8.5;
double _PHi = 11.0;
double pageWidth = I2L(_PWi);
double pageHeight = I2L(_PHi);
res = PXC_AddPage(pdf, pageWidth, pageHeight, &page);
if (IS_DS_FAILED(res))
{
	// error adding page
	return;
}

_PXCContent* pContent = (_PXCContent*)page;
// Drawing rectangle; in tenth of mm
RECT drawRect;
drawRect.left = 0;
drawRect.top = 0;
drawRect.right = _PWi * 2540.0;
drawRect.bottom = _PHi * 2540.0;

// Document rectangle
PXC_RectF pageRect;
pageRect.left =0;
pageRect.right = pageWidth;
pageRect.top = pageHeight;
pageRect.bottom = 0;

// hdc for drawing
HDC hdc;
HRESULT hr = PXC_GetContentDC(pContent, NULL, &drawRect, &pageRect, &hdc);
if (IS_DS_FAILED(hr))
{
	// Handle error
	return;
}

// Lets calculate boundaries in pixels
LONG hres = cdc.GetDeviceCaps(HORZRES);
LONG vres = cdc.GetDeviceCaps(VERTRES);
LONG hsize = cdc.GetDeviceCaps(HORZSIZE);
LONG vsize = cdc.GetDeviceCaps(VERTSIZE);

LONG dpix = (LONG)((double)hres * 25.4 / (double)hsize);
LONG dpiy = (LONG)((double)vres * 25.4 / (double)vsize);

LONG maxx = (LONG)(_PWi * dpix);
LONG maxy = (LONG)(_PHi * dpiy);

// our drawing area is (0, 0) - (maxx, maxy)

// Now hdc could be used for GDI operations
CDC *cdc = CDC::FromHandle(hdc);
CPen red(0,1,RGB(255,0,0));
cdc->SelectObject(&red);
cdc->MoveTo(10,10);
cdc->LineTo(90,90);
cdc->MoveTo(10,90);
cdc->LineTo(90,10);
cdc->MoveTo(10,10);
cdc->LineTo(10,90);
cdc->LineTo(90,90);
cdc->LineTo(90,10);
cdc->LineTo(10,10);

// After all drawings are done hdc must be released
PXC_ReleaseContentDC(pContent, FALSE);
char fname[MAX_PATH]= "m:\\temp\\test.pdf";
DWORD fl = 0;//WEF_ShowSaveDialog;
res = PXC_WriteDocumentExA(pdf, fname, sizeof(fname), fl, NULL);
PXC_ReleaseDocument(pdf);

if (IS_DS_ERROR(res)) 
{
	// error - to do
}
Tracker Software (Project Director)

When attaching files to any message - please ensure they are archived and posted as a .ZIP, .RAR or .7z format - or they will not be posted - thanks.
sjrogers
User
Posts: 10
Joined: Tue Mar 03, 2015 8:38 pm

Re: Using PXC_GetContentDC

Post by sjrogers »

Thanks Ivan, your modified version of the code has me moving forward again.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17901
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Using PXC_GetContentDC

Post by Tracker Supp-Stefan »

:)
Post Reply