Skip to content

Instantly share code, notes, and snippets.

@datalogics-pgallot
Created June 17, 2016 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalogics-pgallot/d229329fa9f9efd19430d8a8dc8ad234 to your computer and use it in GitHub Desktop.
Save datalogics-pgallot/d229329fa9f9efd19430d8a8dc8ad234 to your computer and use it in GitHub Desktop.
DocAdvanced - Sample app which reproduces Acrobat's Document Properties Advanced tab.
/*
// Project: DocAdvanced - Sample app which reproduces Acrobat's Document Properties Advanced tab.
*/
#ifndef MAC_ENV
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "ASExtraCalls.h"
#include "PDCalls.h"
#include "PagePDECntCalls.h"
#include "PDMetadataCalls.h"
#include "MyPDFLibUtils.h"
#ifdef MAC_ENV
#include "MacUtils.h"
#endif
void MainProc(int argc, char **argv)
{
PDDoc pdDoc;
int i = 0;
if (argc > 1)
pdDoc = MyPDDocOpen(argv[1]);
else
return;
DURING
CosDoc cosPDFDoc = PDDocGetCosDoc(pdDoc);
CosObj DocRoot = CosDocGetRoot(cosPDFDoc);
fprintf(stdout, "Base URL: ");
ASBool bFoundBaseURI = false;
CosObj cosURI = CosDictGet(DocRoot, ASAtomFromString("URI"));
if (CosObjGetType(cosURI) == CosDict)
{
CosObj cosBaseURI = CosDictGet(cosURI, ASAtomFromString("Base"));
if (CosObjGetType(cosBaseURI) == CosString)
{
ASTCount byteLen = -1;
ASText ASTxtBaseURI = NULL;
DURING
ASTxtBaseURI = ASTextFromPDText(CosStringValue(cosBaseURI, &byteLen));
char *displayBaseURI = (char *)ASTextGetUnicodeCopy(ASTxtBaseURI, kUTF8);
fprintf(stdout, "%s\n", displayBaseURI);
bFoundBaseURI = true;
ASfree(displayBaseURI);
ASTextDestroy(ASTxtBaseURI);
HANDLER
END_HANDLER
}
}
if (!bFoundBaseURI)
fprintf(stdout, "\n");
fprintf(stdout, "Search Index: ");
ASBool bFoundIdx = false;
CosObj cosSearch = CosDictGet(DocRoot, ASAtomFromString("Search"));
if (CosObjGetType(cosSearch) == CosDict)
{
CosObj cosIndexes = CosDictGet(cosSearch, ASAtomFromString("Indexes"));
if (CosObjGetType(cosIndexes) == CosArray)
{
CosObj cosIdx0 = CosArrayGet(cosIndexes, 0);
if (CosObjGetType(cosIdx0) == CosDict)
{
CosObj cosFSpec = CosDictGet(cosIdx0, ASAtomFromString("Index"));
PDFileSpec idxFileSpec = PDFileSpecFromCosObj(cosFSpec);
if (PDFileSpecIsValid(idxFileSpec))
{
bFoundIdx = true;
ASFile docFile = PDDocGetFile(pdDoc);
ASPathName docPath = ASFileAcquirePathName(docFile);
ASPathName docParentPath = ASFileSysAcquireParent(ASGetDefaultUnicodeFileSys(), docPath);
ASFileSys outFileSys;
ASPathName idxPath = PDFileSpecAcquireASPathEx(idxFileSpec, ASGetDefaultUnicodeFileSys(), docParentPath, &outFileSys, false);
ASText asDisplayPath = ASTextNew();
ASFileSysDisplayASTextFromPath(outFileSys, idxPath, asDisplayPath);
char *displaySearchPath = (char *)ASTextGetUnicodeCopy(asDisplayPath, kUTF8);
fprintf(stdout, "%s\n", displaySearchPath);
ASfree(displaySearchPath);
ASTextDestroy(asDisplayPath);
ASFileSysReleasePath(NULL, idxPath);
ASFileSysReleasePath(NULL, docParentPath);
ASFileSysReleasePath(NULL, docPath);
}
}
}
}
if (!bFoundIdx)
fprintf(stdout, "\n");
fprintf(stdout, "Trapped: ");
ASAtom trapped = PDDocGetTrapped(pdDoc);
if (trapped == ASAtomFromString("False"))
fprintf(stdout, "No\n");
else if (trapped == ASAtomFromString("True"))
fprintf(stdout, "Yes\n");
else
fprintf(stdout, "Unknown\n");
fprintf(stdout, "Page Scaling: ");
ASBool bAppDefault = true;
CosObj cosViewPrefs = CosDictGet(DocRoot, ASAtomFromString("ViewerPreferences"));
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("PrintScaling"));
if (CosObjGetType(cosPref) == CosName && CosNameValue(cosPref) == ASAtomFromString("None"))
{
fprintf(stdout, "None\n");
bAppDefault = false;
}
}
if (bAppDefault)
fprintf(stdout, "Default\n");
fprintf(stdout, "DuplexMode: ");
ASBool bHasDuplex = false;
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("Duplex"));
if (CosObjGetType(cosPref) == CosName)
{
if (CosNameValue(cosPref) == ASAtomFromString("DuplexFlipShortEdge"))
{
fprintf(stdout, "Duplex Flip Short Edge\n");
bHasDuplex = true;
}
else if (CosNameValue(cosPref) == ASAtomFromString("DuplexFlipLongEdge"))
{
fprintf(stdout, "Duplex Flip Long Edge\n");
bHasDuplex = true;
}
}
}
if (!bHasDuplex)
fprintf(stdout, "Simplex\n");
fprintf(stdout, "Paper Source by Page Size: ");
ASBool bPickByTray = false;
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("PickTrayByPDFSize"));
bPickByTray = (CosObjGetType(cosPref) == CosBoolean && CosBooleanValue(cosPref));
}
fprintf(stdout, "%s\n", bPickByTray ? "Yes" : "No");
fprintf(stdout, "Print Page Range: ");
ASBool bHasInvalidRange = true;
char szRangeStr[256] = "";
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("PrintPageRange"));
if (CosObjGetType(cosPref) == CosArray)
{
int rangelen = CosArrayLength(cosPref);
if (rangelen > 0 && rangelen % 2 == 0)
{
bHasInvalidRange = false;
int i = 0;
do {
int start = CosIntegerValue(CosArrayGet(cosPref, i++));
int stop = CosIntegerValue(CosArrayGet(cosPref, i++));
if (start < 0 || stop < 0)
{
bHasInvalidRange = true;
break;
}
if (start == stop)
sprintf(szRangeStr, "%s%d", szRangeStr, start + 1);
else
sprintf(szRangeStr, "%s%d-%d", szRangeStr, start + 1, stop + 1);
if (i < rangelen)
strcat(szRangeStr, ", ");
} while (i < rangelen);
}
}
}
fprintf(stdout, "%s\n",bHasInvalidRange ? "" : szRangeStr);
fprintf(stdout, "Number of Copies: ");
int numCopies = 1;
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("NumCopies"));
if (CosObjGetType(cosPref) == CosInteger || CosObjGetType(cosPref) == CosFixed)
{
int numCopiesVal = CosIntegerValue(cosPref);
if (numCopiesVal >= 2 && numCopiesVal <= 5)
numCopies = numCopiesVal;
}
}
if (numCopies == 1)
fprintf(stdout, "Default\n");
else
fprintf(stdout, "%d\n", numCopies);
fprintf(stdout, "Binding: ");
ASBool bLeftEdge = true;
if (CosObjGetType(cosViewPrefs) == CosDict)
{
CosObj cosPref = CosDictGet(cosViewPrefs, ASAtomFromString("Direction"));
if (CosObjGetType(cosPref) == CosName)
bLeftEdge = !(CosNameValue(cosPref) == ASAtomFromString("R2L"));
}
fprintf(stdout, "%s Edge\n", bLeftEdge ? "Left" : "Right");
fprintf(stdout, "Language: ");
ASBool bFoundLang = false;
CosObj cosLang = CosDictGet(DocRoot, ASAtomFromString("Lang"));
if (CosObjGetType(cosLang) == CosString)
{
ASTCount byteLen = -1;
DURING
char *displayLang = CosCopyStringValue(cosLang, &byteLen);
fprintf(stdout, "%s\n", displayLang);
bFoundLang = true;
ASfree(displayLang);
HANDLER
END_HANDLER
}
if (!bFoundLang)
fprintf(stdout, "\n");
HANDLER
char buf[512];
ASGetErrorString(ERRORCODE, buf, sizeof(buf));
fprintf(stderr, "Error code: 0x%lx, Error Message: %s\n", ERRORCODE, buf);
END_HANDLER
PDDocClose(pdDoc);
}
#define INCLUDE_MYPDFLIBAPP_CPP 1
#include "MyPDFLibApp.cpp"
#undef INCLUDE_MYPDFLIBAPP_CPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment