Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample app based on helowrld, but creating a PDF/A-2b compliant document as output.
/*
//
// Copyright (c) 2015-2016, Datalogics, Inc. All rights reserved.
// Portions Copyright (C) 2000-2003 Adobe Systems Incorporated
// All rights reserved.
//
// http://dev.datalogics.com/adobe-pdf-library/license-for-downloaded-pdf-samples/
//
// NOTICE: Adobe permits you to use, modify, and distribute this file
// in accordance with the terms of the Adobe license agreement
// accompanying it. If you have received this file from a source other
// than Adobe, then your use, modification, or distribution of it
// requires the prior written permission of Adobe.
//
*/
/*
//
// Project: - "Hello World" Example of Simple Text Display, made PDF/A-2b compliant
//
*/
#ifdef MAC_ENV
#include <Carbon/Carbon.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "PDFInit.h"
#include "CosCalls.h"
#include "CorCalls.h"
#include "ASCalls.h"
#include "ASExtraCalls.h"
#include "PDCalls.h"
#include "PSFCalls.h"
#include "PERCalls.h"
#include "PEWCalls.h"
#include "PIExcept.h"
#include "PDMetadataCalls.h"
#include "PagePDECntCalls.h"
#include "AcroColorCalls.h"
#include "MyPDFLibUtils.h"
#ifdef MAC_ENV
#include "macUtils.h"
#endif
boolean embedOutputIntent(PDDoc doc, char *pathToICCProfile)
{
ASInt32 err = 0;
ASFile Profile;
ASTFilePos ProfileSize;
ASUns8 *ProfileBuffer;
ASStm ProfileStm;
ASPathName Path;
/* First, read the profile into memory */
Path = ASFileSysCreatePathFromCString(NULL, pathToICCProfile);
err = ASFileSysOpenFile(NULL, Path, ASFILE_READ, &Profile);
if (err != 0)
{
fprintf(stderr, "Unable to open the ICC profile, \"%s\".\n", pathToICCProfile);
return false;
}
else
{
ASFileSysReleasePath(NULL, Path);
ProfileSize = ASFileGetEOF(Profile);
ProfileBuffer = (ASUns8 *)ASmalloc(ProfileSize);
ASFileRead(Profile, (char *)ProfileBuffer, ProfileSize);
ASFileClose(Profile);
AC_Profile ac_profile;
ACMakeBufferProfile(&ac_profile, ProfileBuffer, ProfileSize);
PDDocColorConvertEmbedOutputIntent(doc, ac_profile);
ACUnReferenceProfile(ac_profile);
ASfree(ProfileBuffer);
return true;
}
}
void switchOutputIntentsToPDFA(PDDoc pdSrcDoc)
{
CosDoc cosSrcDoc = PDDocGetCosDoc(pdSrcDoc);
CosObj SrcRoot = CosDocGetRoot(cosSrcDoc);
CosObj srcObj = CosDictGetKeyString(SrcRoot, "OutputIntents");
ASAtom asSubtype = ASAtomFromString("S");
ASAtom asPDFX = ASAtomFromString("GTS_PDFX");
ASAtom asPDFA1 = ASAtomFromString("GTS_PDFA1");
if (CosObjGetType(srcObj) == CosArray)
{
int numIntents = CosArrayLength(srcObj);
for (int i = 0; i < numIntents; i++)
{
CosObj intentObj = CosArrayGet(srcObj, i);
if (CosObjGetType(intentObj) == CosDict)
{
CosObj SObj = CosDictGet(intentObj, asSubtype);
if (CosObjGetType(SObj) == CosName && CosNameValue(SObj) == asPDFX)
{
CosDictPut(intentObj, asSubtype, CosNewName(cosSrcDoc, false, asPDFA1));
}
}
}
}
}
void copyPDFXOutputIntentsToPDFA(PDDoc pdSrcDoc)
{
CosDoc cosSrcDoc = PDDocGetCosDoc(pdSrcDoc);
CosObj SrcRoot = CosDocGetRoot(cosSrcDoc);
CosObj srcObj = CosDictGetKeyString(SrcRoot, "OutputIntents");
ASAtom asSubtype = ASAtomFromString("S");
ASAtom asPDFX = ASAtomFromString("GTS_PDFX");
ASAtom asPDFA1 = ASAtomFromString("GTS_PDFA1");
if (CosObjGetType(srcObj) == CosArray)
{
int numIntents = CosArrayLength(srcObj);
for (int i = 0; i < numIntents; i++)
{
CosObj intentObj = CosArrayGet(srcObj, i);
if (CosObjGetType(intentObj) == CosDict)
{
CosObj SObj = CosDictGet(intentObj, asSubtype);
if (CosObjGetType(SObj) == CosName && CosNameValue(SObj) == asPDFX)
{
CosObj intentObjCopy = CosObjCopy(intentObj, cosSrcDoc, false);
CosDictPut(intentObjCopy, asSubtype, CosNewName(cosSrcDoc, false, asPDFA1));
CosArrayPut(srcObj, i + numIntents, intentObjCopy);
}
}
}
}
}
/* DLADD: wfles 16Jul2007 - Added function arguments to MainProc. */
void MainProc(int argc, char **argv )
{
PDDoc pdDoc = NULL; /* reference to a PDF document */
PDPage pdPage = NULL; /* reference to a page in doc */
PDEContent pdeContent; /* container for page content */
ASFixedRect mediaBox; /* dimensions of page */
PDSysFont sysFont; /* used by PDEFont creation */
PDEFont pdeFont = NULL; /* reference to a font used on a page */
PDEFontAttrs pdeFontAttrs; /* font attributes */
PDEText pdeText = NULL; /* container for text */
ASDoubleMatrix textMatrix; /* transformation matrix for text */
PDEColorSpace pdeColorSpace = NULL; /* ColorSpace */
PDEGraphicState gState; /* graphic state to apply to operation */
char *HelloWorldStr = "Hello World!";
ASPathName outPath = NULL;
ASErrorCode errCode = 0;
/*==================================================================*\
Create Doc, Page, Content Container
\*==================================================================*/
DURING
pdDoc = PDDocCreate(); /* create new document */
mediaBox.left = fixedZero; /* dimensions of page */
mediaBox.top = Int16ToFixed(4*72); /* in this case 5" x 4" */
mediaBox.right = Int16ToFixed(5*72);
mediaBox.bottom = fixedZero;
/* create page with those dimensions and insert as first page */
pdPage = PDDocCreatePage(pdDoc, PDBeforeFirstPage, mediaBox);
/* Acquire PDEContent container for page */
pdeContent = PDPageAcquirePDEContent(pdPage, NULL);
/*==================================================================*\
Acquire Font, Add Text, Insert Into Page Content Container
\*==================================================================*/
memset(&pdeFontAttrs, 0, sizeof(pdeFontAttrs));
pdeFontAttrs.name = ASAtomFromString("CourierStd");
pdeFontAttrs.type = ASAtomFromString("Type1");
sysFont = PDFindSysFont(&pdeFontAttrs, sizeof(PDEFontAttrs), 0);
pdeFont = PDEFontCreateFromSysFont(sysFont, kPDEFontCreateEmbedded);
/* The following code sets up the default Graphics state. We do this so that
* we can free the PDEColorSpace objects
*/
pdeColorSpace = PDEColorSpaceCreateFromName(ASAtomFromString("DeviceCMYK"));
memset(&gState, 0, sizeof(PDEGraphicState));
gState.strokeColorSpec.space = gState.fillColorSpec.space = pdeColorSpace;
gState.fillColorSpec.value.color[0] = fixedOne;
gState.fillColorSpec.value.color[1] = fixedOne;
gState.fillColorSpec.value.color[2] = fixedZero;
gState.fillColorSpec.value.color[3] = fixedZero;
gState.miterLimit = fixedTen;
gState.flatness = fixedOne;
gState.lineWidth = fixedOne;
memset(&textMatrix, 0, sizeof(textMatrix)); /* clear structure */
textMatrix.a = 24; /* set font width and height */
textMatrix.d = 24; /* to 24 point size */
textMatrix.h = 1*72; /* x,y coordinate on page */
textMatrix.v = 2*72; /* in this case, 1" x 2" */
pdeText = PDETextCreate(); /* create new text run */
PDETextAddEx(pdeText, /* text container to add to */
kPDETextRun, /* kPDETextRun, kPDETextChar */
0, /* index */
(Uns8 *)HelloWorldStr, /* text to add */
strlen(HelloWorldStr), /* length of text */
pdeFont, /* font to apply to text */
&gState, sizeof(gState), /* graphic state to apply to text */
NULL, 0, /* text state and size of structure*/
&textMatrix, /* transformation matrix for text */
NULL); /* stroke matrix */
/* insert text into page content */
PDEContentAddElem(pdeContent, kPDEAfterLast, (PDEElement) pdeText);
/*==================================================================*\
Convert To Objects, Add To Page, Release Resources
\*==================================================================*/
/* Set the PDEContent for the page */
PDPageSetPDEContentCanRaise(pdPage, NULL);
embedOutputIntent(pdDoc, "..\\..\\Resource\\Color\\Profiles\\USWebCoatedSWOP.icc");
copyPDFXOutputIntentsToPDFA(pdDoc);
ASText namespaceName = ASTextFromPDText("http://www.aiim.org/pdfa/ns/id/");
ASText namespacePrefix = ASTextFromPDText("pdfaid");
ASText partPath = ASTextFromPDText("part");
ASText partValue = ASTextFromPDText("2");
ASText conformancePath = ASTextFromPDText("conformance");
ASText conformanceValue = ASTextFromPDText("B");
PDDocSetXAPMetadataProperty(pdDoc, namespaceName, namespacePrefix, partPath, partValue);
PDDocSetXAPMetadataProperty(pdDoc, namespaceName, namespacePrefix, conformancePath, conformanceValue);
ASTextDestroy(conformanceValue);
ASTextDestroy(conformancePath);
ASTextDestroy(partValue);
ASTextDestroy(partPath);
ASTextDestroy(namespacePrefix);
ASTextDestroy(namespaceName);
ASText xap = PDDocGetXAPMetadata(pdDoc);
printf("***xap***:\n%s\n***End xap***\n",ASTextGetEncoded(xap,0));
ASTextDestroy(xap);
/* save document to a file */
#if !MAC_ENV
outPath = ASFileSysCreatePathFromDIPath( NULL, "out.pdf", NULL );
#else
outPath = GetMacPath("out.pdf");
#endif
PDDocSave(pdDoc, PDSaveFull | PDSaveLinearized, outPath, ASGetDefaultFileSys(), NULL, NULL);
HANDLER
errCode = ERRORCODE;
END_HANDLER
if(errCode)
DisplayError(errCode);
/* remember to release all objects that were created */
if(outPath) ASFileSysReleasePath( NULL, outPath);
if(pdeFont) PDERelease((PDEObject) pdeFont);
if(pdeText) PDERelease((PDEObject) pdeText);
if(pdeColorSpace) PDERelease((PDEObject)pdeColorSpace);
if(pdPage)
{
PDPageReleasePDEContent(pdPage, NULL);
PDPageRelease(pdPage);
}
if(pdDoc) PDDocRelease(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