Copy and object from a pdf file into another pdf file

Copy and object from a pdf file into another pdf file

Post by Gandal » Fri, 16 Nov 2001 00:20:07



I'm writing a plugin copying into an open pdf files (doc1) all the objects
(images, etc.) from another pdf files (doc2).
So far, this is the main procedure (no error checkings to have a more
readable code):

/////////////////////////////////////////////////////////////////////////
//
 char* IMG_PATH = "c:\\Logo1.pdf"; // Logo1.pdf is the doc2 containing the
objects (an image for example)

 ASInt32 numElem; // Number of PDEElents objects into doc2

 //The file into which objects must be copied
 AVDoc avDoc1;
 PDDoc pdDoc1;
 PDPage pdPage1;
 AVPageView avPageView1;
 PDEContent pdeContent1;
 PDEElement pdeElem1[200]; // Just to simplify for the moment I use a fixed
length array of objects

 //The file from which objects must be copied
 ASPathName path2;
 AVDoc avDoc2;
 PDDoc pdDoc2;
 PDPage pdPage2;
 PDEContent pdeContent2;

 // Get doc1 data
 avDoc1 = AVAppGetActiveDoc(); // Get AVDoc of current open file
 pdDoc1 = AVDocGetPDDoc(avDoc1); // Get PDDoc from AVDoc
 pdPage1 = PDDocAcquirePage (pdDoc1, 1); // Get first page
 pdeContent1 = PDPageAcquirePDEContent (pdPage1, gExtensionID); // Get page
content, gExtensionID defined elsewhere

 // Open doc2 and get its data
 path2 = ASFileSysCreatePathName (NULL, ASAtomFromString("Cstring"),
IMG_PATH, 0); // Create a path to load doc2
 avDoc2 = AVDocOpenFromFile(path2, ASGetDefaultFileSys(), NULL); // Open
doc2
 pdDoc2 = AVDocGetPDDoc(avDoc2); // Get PDDOc
 pdPage2 = PDDocAcquirePage (pdDoc2, 0); // Get first page
 pdeContent2 = PDPageAcquirePDEContent (pdPage2, gExtensionID); // Get page
content

 numElem = PDEContentGetNumElems(pdeContent2); // Get num of elems from
doc2's PDEContent

 // Store doc2's elements into an array
 for (int i=0; i<=numElem; i++)
 {
  pdeElem1[i] = PDEContentGetElem(pdeContent2, i);
 }

 // IF I COMPILE AND RUN, TILL HERE EVERYTHING IS OK AND NO ERROR MESSAGES
OR EXCEPTIONS ARISE

 // Add to doc1's PDEContent all the stored elements/objects from the array
 for (int j=0; j<=numElem; j++)
 {
  PDEContentAddElem (pdeContent1, kPDEAfterLast, pdeElem1[i]);
 }

 // AT THIS POINT, IF I COMPILE AND RUN THE EXCEPTION
"peErrWrongPDEObjectType" ARISE and the error message "Incorrect PDEObject
type" is displayed

 // Save changes to the doc1's PDEContent
 PDPageSetPDEContent(pdPage1, gExtensionID);
 PDPageNotifyContentsDidChange (pdPage1);

 // Release all objects
 ...
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////

I hope the above makes sense. Could someone help me?
Many thanks.

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----

 
 
 

Copy and object from a pdf file into another pdf file

Post by David van Driessch » Fri, 16 Nov 2001 04:54:09


Gandall,

Before making more serious checks, can you tell when you get the exception?

Quote:>  for (int i=0; i<=numElem; i++)
>  {
>   pdeElem1[i] = PDEContentGetElem(pdeContent2, i);
>  }

In the loop you have here you read one element too many. (Loop starts at
zero, so does numElem+1 elements). If you get the exception at the end of
the add loop it's probably because of this (I think you'll get a nil pointer
when you ask for the last element above and I wouldn't be surprised that
AddElem gives an exception when you ask it to add a nil pointer element...

Also, have a look at this:

Quote:>  pdPage1 = PDDocAcquirePage (pdDoc1, 1); // Get first page

I think pages start at 0 so either the code or the comment is wrong...

Hope this helps,
David.

--
----------------------------------------------------------------
David van Driessche
Enfocus Software                  Web:   http://www.enfocus.com
Director of Software Development
------------------------------------------
Chaos reigns within.
Reflect. Repent. Reboot.
Order shall return.
----------------------------------------------------------------

 
 
 

Copy and object from a pdf file into another pdf file

Post by Gandal » Fri, 16 Nov 2001 20:24:55


Ok, I've gove farther, so now I have a different problem.
First the code:

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//////////////////////

char* IMG_PATH = "c:\\Logo1.pdf"; //doc2 with all the objs to copy
ASInt32 numElem; //number of objs/elements in doc2

//DOC1
AVDoc  avDoc1;
PDDoc  pdDoc1;
PDPage  pdPage1;
PDEContent pdeContent1;
PDEElement pdeElem1[200];

//DOC2
ASPathName path2;
AVDoc  avDoc2;
PDDoc  pdDoc2;
PDPage  pdPage2;
PDEContent pdeContent2;

///////////////////////////////////////////////////////////////

//open doc1
path2 = ASFileSysCreatePathName (NULL, ASAtomFromString("Cstring"),
IMG_PATH, 0);
avDoc2 = AVDocOpenFromFile(path2, ASGetDefaultFileSys(), NULL);
pdDoc2 = AVDocGetPDDoc(avDoc2);

//get 1st page content
pdPage2 = PDDocAcquirePage (pdDoc2, 0);
pdeContent2 = PDPageAcquirePDEContent (pdPage2, gExtensionID);

//get number of elemts
numElem = PDEContentGetNumElems(pdeContent2);

//get each element form doc2 and copy it, so that I can close doc2 before
//opening doc1 and use the same gExtensionID to acquire doc1's page content
for (int i=0; i<numElem; i++)
{
 pdeElem1[i] = PDEElementCopy(PDEContentGetElem(pdeContent2, i),
kPDEElementCopyForClip);

Quote:}

//release everything from doc2
PDPageReleasePDEContent (pdPage2, gExtensionID);
PDPageRelease (pdPage2);
AVDocClose (avDoc2, true);
ASFileSysReleasePath (NULL, path2);

///////////////////////////////////////////////////////////////

//open doc1
avDoc1 = AVAppGetActiveDoc();
pdDoc1 = AVDocGetPDDoc(avDoc1);

//get 1st page content
pdPage1 = PDDocAcquirePage (pdDoc1, 0);
pdeContent1 = PDPageAcquirePDEContent (pdPage1, gExtensionID);

//error code and message for content writing
ASInt32 errorCode = 0;
char errorMsg[256];

//for each elemnt in the array, add it to the doc1's 1st page content, set
the content
//and notify the change.
for (int j=0; j<numElem; j++)
{
 PDEContentAddElem (pdeContent1, kPDEAfterLast, (PDEElement)pdeElem1[j]);

 DURING
  PDPageSetPDEContent(pdPage1, gExtensionID);
  PDPageNotifyContentsDidChange(pdPage1);
 HANDLER
  errorCode = ASGetExceptionErrorCode();
 END_HANDLER

Quote:}

if (errorCode)
{
 ASGetErrorString (ASGetExceptionErrorCode(), errorMsg, 256);
 AVAlertNote (errorMsg);

Quote:}

//release everything
for (int k=0; k<numElem;k++)
{
 PDERelease((PDEObject)pdeElem1[k]);

Quote:}

PDPageReleasePDEContent (pdPage1, gExtensionID);
PDPageRelease ( pdPage1 );

//////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

NOW THE PROBLEM. During the following loop

   //for each elemnt in the array, add it to the doc1's 1st page content,
set the content
   //and notify the change.
   for (int j=0; j<numElem; j++)
   {
    PDEContentAddElem (pdeContent1, kPDEAfterLast, (PDEElement)pdeElem1[j]);

    DURING
     PDPageSetPDEContent(pdPage1, gExtensionID);
     PDPageNotifyContentsDidChange(pdPage1);
    HANDLER
     errorCode = ASGetExceptionErrorCode();
    END_HANDLER
   }

the first elemnt (which is an image) is added and the page content is
correctly updated (I can see the image appear on doc1);
then the second elemnt (which actually I'm not sure what kind of object it
is) is added
(so the line "PDEContentAddElem (pdeContent1, kPDEAfterLast,
(PDEElement)pdeElem1[j]);" is executed) but when trying to set the page
content
(so when the line "PDPageSetPDEContent(pdPage1, gExtensionID);" is executed)
I get an Acrobat error, that is a message box with the following title:
      "AVToolTipView: Acrobat.exe - Application Error"
and the following error message:
      "The instruction at  0x00647885 referenced memory at 0x00000004. The
memory could not be read"
As you can see this is not an exception arised by the handler.

Of course the problem is due to the second element which actually I don't
really know what type of object it is: doc2 is just a word document
converted to pdf. The original doc document contains an image (the one that
works) and some MS Wprd Art text which is the one being converted into
elemnts causing the access violation.

Thanks.
--
Giovanni Stoto

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----