Using C++ and the Core API. So far, I am successfully able to open a document from file, insert the pages from the loaded document into a new document (in memory), and write out a new file.
What I seem unable to accomplish is changing the color of anything that is output. My strategy is this:
- From the new document, Iterate through all the pages.
- for each page, get the content, and from that get the items
- for each item get the color state
What I want is to be able to set the stroke color and fill color of the color state, for each item that has a color state.
Is this possible? Am I on the right track? Is there a simpler way to do what I'm trying to do? Can the Core API accomplish this? Here's what I have so far...
Code: Select all
// The following are members of the class...
// CComPtr<PXC::IPXC_Document> m_pDocument;
// CComPtr<PXC::IPXC_Pages> m_pPages;
int PDFLoader_pdfxCoreSDK::SaveDocumentAsGrayscale(const std::string strPathToOutputFile)
{
CComPtr<PXC::IPXC_Document> pNewDoc = 0;
if ((g_Inst->NewDocument(&pNewDoc) != S_OK) || (pNewDoc == 0))
{
return (-4); // we'll assume "out of memory".
}
CComPtr<PXC::IPXC_Pages> pPages = 0;
if ((pNewDoc->get_Pages(&pPages) != S_OK) || (pPages == 0))
{
return (-4);
}
PXC::ULONG_T numPages = 0;
m_pPages->get_Count(&numPages);
PXC::ULONG_T flags = 0; // PXC::PXC_InsertPagesFlags::IPF_Annots_Copy | PXC::PXC_InsertPagesFlags::IPF_Bookmarks_CopyAll;
HRESULT hRes = pPages->InsertPagesFromDoc(m_pDocument, 0, 0, numPages, flags, 0);
if (hRes != S_OK)
{
return (-4);
}
unsigned int uItemCounter = 0;
for (unsigned int uPage = 0; uPage < (unsigned int)numPages; ++uPage)
{
CComPtr<PXC::IPXC_Page> pPage = 0;
pPages->get_Item((PXC::ULONG_T)uPage, &pPage);
CComPtr<PXC::IPXC_Content> pContent = 0;
if (pPage->GetContent(PXC::PXC_ContentAccessMode::CAccessMode_FullClone, &pContent) == S_OK)
{
CComPtr<PXC::IPXC_ContentItems> pItems = 0;
if (pContent->get_Items(&pItems) == S_OK)
{
PXC::ULONG_T numItems = 0;
pItems->get_Count(&numItems);
for (unsigned int uItem = 0; uItem < numItems; ++uItem)
{
CComPtr<PXC::IPXC_ContentItem> pItem = 0;
if (pItems->get_Item(uItem, &pItem) == S_OK)
{
++uItemCounter;
PXC::PXC_CIType type;
pItem->get_Type(&type);
DebugPrintf("Page %u, Object %u of %u, Type: %s Item:%u\n", uPage, uItem, (unsigned int)numItems, GetTypeString(type), uItemCounter);
CComPtr<PXC::IPXC_CState > pCState = 0;
if (pItem->GetCState(&pCState) == S_OK)
{
CComPtr<PXC::IPXC_Color> pFillColor = 0;
if (pCState->get_FillColor(&pFillColor) == S_OK)
{
// So now I have the fill color. What can I do to change it? Is it a reference to the color being used, or is it a copy.
// Do I have to call pCState->put_FillColor(pFillColor) after I change it?
// Am I on the right track here? What do I need to do?
// MakeItGray(pFillColor);
pCState->put_FillColor(pFillColor);
}
CComPtr<PXC::IPXC_Color> pStrokeColor = 0;
if (pCState->get_StrokeColor(&pStrokeColor) == S_OK)
{
// So now I have the stroke color. What can I do to change it? Is it a reference to the color being used, or is it a copy.
// Do I have to call pCState->put_FillColor(pFillColor) after I change it?
// Am I on the right track here? What do I need to do?
// MakeItGray(pStrokeColor);
pCState->put_StrokeColor(pStrokeColor);
}
// Do I need to do this? Or will modifying the the object referenced by pCState change the IPXC_CState associated with the item?
pItem->SetCState(pCState);
}
}
}
}
}
}
// This works fine, no problem writing the document. All the pages appear, but the colors are identical
// to the colors in the original document.
std::wstring strOutputFilename = ToWString(strPathToOutputFile);
if (pNewDoc->WriteToFile((LPWSTR)strOutputFilename.c_str(), 0, 0) != S_OK)
{
return 0;
}
return 1;
}