
Adobe Acrobat SDK
Working with Cos Objects
Developing Plug-ins and Applications
Creating Cos streams 194
PDDoc pdDoc = AVDocGetPDDoc(avDoc);
PDPage pdPage = PDDocAcquirePage(pdDoc, pageNum); // acquire current page
//Use the PDPage object to create a CosDoc object
cd = PDDocGetCosDoc(PDPageGetDoc(pdPage));
//Define a stream that creates a thin line segment
sprintf(buf,"150 250 m 150 350 l S");
//Get the stream length
ASUns32 streamLength = (ASUns32)strlen(buf);
//Create a CosObj object that represents the stream length
LengthEntry = CosNewInteger(cd, false,streamLength);
//Create a CosObj that represents a stream dictionary
//and set it key-value pairs
AttrDict = CosNewDict(cd, false,5);
char *Length_KStr = "Length";
CosDictPutKeyString(AttrDict, Length_KStr, LengthEntry);
//Determine if the stream dictionary is valid
if (CosObjEqual (AttrDict, CosNewNull ()) == true)
{
AVAlertNote ("The stream dictionary could not be created");
return;
}
//Read the stream into memory by invoking the ASMemStmRdOpen method
OpenedStream = ASMemStmRdOpen(buf,streamLength);
DURING
//Create a new Cos stream using data from the ASStm object
PageStrm = CosNewStream(cd, true, OpenedStream, 0,
false, // StmDataIsNotDecoded
AttrDict, //The stream dictionary
EncodeParms, -1);
//Close the stream
ASStmClose(OpenedStream);
HANDLER
AVAlertNote ("Trying to create new CosStream");
CosObjDestroy (AttrDict);
ASStmClose (OpenedStream);
return;
END_HANDLER
//Completely replace the contents of the specified page with newContents
PDPageAddCosContents (pdPage, PageStrm);
Caution:
If you execute this code example without having a PDF document open, you will cause an
Adobe Reader or Acrobat run-time error. The run-time error occurs because this code example
creates a
PDPage
object that is based on the current PDF document page.