Skip to content

Instantly share code, notes, and snippets.

@datalogics-pgallot
Created May 20, 2016 19:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save datalogics-pgallot/c1fab26f5aeb2157342e5133f3da1412 to your computer and use it in GitHub Desktop.
/*
// Project: DocDescriptiont - Sample app which reproduces Acrobat's Document Properties Description 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)
{
char* propertiesStrs[] = { "Title", "Author", "Subject", "Keywords", "" };
ASText propName;
ASText propValue = ASTextNew();
char *displayPropValue = NULL;
PDDoc pdDoc;
int i = 0;
if (argc > 1)
pdDoc = MyPDDocOpen(argv[1]);
else
return;
DURING
CosDoc cosPDFDoc = PDDocGetCosDoc(pdDoc);
CosObj DocRoot = CosDocGetRoot(cosPDFDoc);
while (propertiesStrs[i][0] != 0x0)
{
propName = ASTextFromPDText(propertiesStrs[i]);
PDDocGetInfoASText(pdDoc, propName, propValue);
displayPropValue = (char *)ASTextGetUnicodeCopy(propValue, kUTF8);
fprintf(stdout, "%s: %s\n", propertiesStrs[i], displayPropValue);
i++;
ASTextDestroy(propName);
ASfree(displayPropValue);
}
#define DATE_SIZE 48
displayPropValue = (char *)ASmalloc(DATE_SIZE);
PDDocGetInfo(pdDoc, "CreationDate", displayPropValue, DATE_SIZE);
fprintf(stdout, "Created: %s\n", displayPropValue);
PDDocGetInfo(pdDoc, "ModDate", displayPropValue, DATE_SIZE);
fprintf(stdout, "Modified: %s\n", displayPropValue);
ASfree(displayPropValue);
if (CosDictKnownKeyString(DocRoot, "Metadata"))
{
ASText namespaceName = ASTextFromPDText("http://ns.adobe.com/xap/1.0/");
propName = ASTextFromPDText("xmp:CreatorTool");
propValue = PDDocGetXAPMetadataProperty(pdDoc, namespaceName, propName);
displayPropValue = (char *)ASTextGetUnicodeCopy(propValue, kUTF8);
fprintf(stdout, "Application: %s\n", displayPropValue);
ASTextDestroy(propName);
ASfree(displayPropValue);
ASTextDestroy(namespaceName);
namespaceName = ASTextFromPDText("http://ns.adobe.com/pdf/1.3/");
propName = ASTextFromPDText("pdf:Producer");
propValue = PDDocGetXAPMetadataProperty(pdDoc, namespaceName, propName);
displayPropValue = (char *)ASTextGetUnicodeCopy(propValue, kUTF8);
fprintf(stdout, "PDF Producer: %s\n", displayPropValue);
ASTextDestroy(propName);
ASfree(displayPropValue);
ASTextDestroy(namespaceName);
}
else{
propName = ASTextFromPDText("Creator");
PDDocGetInfoASText(pdDoc, propName, propValue);
displayPropValue = (char *)ASTextGetUnicodeCopy(propValue, kUTF8);
fprintf(stdout, "Application: %s\n", displayPropValue);
ASTextDestroy(propName);
ASfree(displayPropValue);
propName = ASTextFromPDText("Producer");
PDDocGetInfoASText(pdDoc, propName, propValue);
displayPropValue = (char *)ASTextGetUnicodeCopy(propValue, kUTF8);
fprintf(stdout, "PDF Producer: %s\n", displayPropValue);
ASTextDestroy(propName);
ASfree(displayPropValue);
ASTextDestroy(propValue);
}
ASUns32 verMajor = 0, verMinor = 0, verExtensLvl = 0;
CosObj cosExtBase;
PDDocGetVersionEx(pdDoc, &verMajor, &verMinor, &cosExtBase, &verExtensLvl);
fprintf(stdout, "PDF Version: ");
if (verMajor == 1)
{
switch (verMinor){
case 9:
fprintf(stdout, "1.7, Adobe Extension Level %d (Acrobat X)\n", verExtensLvl);
break;
case 8:
case 7:
if (CosObjGetType(cosExtBase) != CosNull && verExtensLvl != 0)
fprintf(stdout, "1.7, Adobe Extension Level %d (Acrobat %d.x)\n", verExtensLvl, verMinor + 1);
else
fprintf(stdout, "1.7 (Acrobat %d.x)\n", verMinor + 1);
break;
default:
fprintf(stdout, "1.%d (Acrobat %d.x)\n", verMinor, verMinor + 1);
}
}
PDPage page0 = PDDocAcquirePage(pdDoc, 0);
ASFixed fPgWidth, fPgHeight;
PDPageGetSize(page0, &fPgWidth, &fPgHeight); // swaps page dimensions based on page rotation.
float userUnit = PDPageGetUserUnitSize(page0);
float pgWidth = ASFixedToFloat(fPgWidth) * userUnit / 72.0;
float pgHeight = ASFixedToFloat(fPgHeight) * userUnit / 72.0;
fprintf(stdout, "Page Size: %0.2f x %0.2f in\n", pgWidth, pgHeight);
fprintf(stdout, "Number of Pages: %d\n", PDDocGetNumPages(pdDoc));
ASBool bMarked = false;
ASBool bStructTree = CosObjGetType(CosDictGet(DocRoot, ASAtomFromString("StructTreeRoot"))) == CosDict;
CosObj cosMarkInfo = CosDictGet(DocRoot, ASAtomFromString("MarkInfo"));
if (CosObjGetType(cosMarkInfo) == CosDict)
{
CosObj cosMarked = CosDictGet(cosMarkInfo, ASAtomFromString("Marked"));
if (CosObjGetType(cosMarked) == CosBoolean)
bMarked = CosBooleanValue(cosMarked);
}
fprintf(stdout, "Tagged PDF: %s\n", (bMarked && bStructTree) ? "Yes" : "No");
ASInt32 flags = PDDocGetFlags(pdDoc);
fprintf(stdout, "Fast Web View: %s\n", (flags & PDDocIsLinearized) ? "Yes": "No");
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