Creating a new doc and inserting pages programmatically

PDF-XChange Editor SDK for Developers

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Creating a new doc and inserting pages programmatically

Post by docu-track99 »

Hello folks,

Looking for some guidance on how to implement a feature where the user can specify a range of pages from the active document and insert those pages into a new document.

I'm running into a problem that maybe you guys can identify through my pretty basic code here:

Code: Select all

  Dim oCurrentDoc = frmMain.pdfCtrl.Inst.ActiveDoc.CoreDoc
            Dim oNewDoc = frmMain.pdfCtrl.Inst.NewCoreDoc
            Dim nStart As Integer = Val(txtStart.Text) 'this is the start page of current doc
            Dim nEnd As Integer = Val(txtEnd.Text) 'this is the end page of current doc

            oNewDoc.Pages.InsertPagesFromDoc(oCurrentDoc, 0, nStart, nEnd - 1, PXC_InsertPagesFlags.IPF_Annots_Copy + PXC_InsertPagesFlags.IPF_Bookmarks_CopyAll)
I am getting an error on the last line for some reason, and I'm not entirely sure why. I have ensured the oCurrentDoc has pages, I have tried hardcoding the page range of nStart and nEnd to 1 and 2 so as to ensure I am staying within the bounds of the number of pages, and I have tried using different InsertPagesFlags. Furthermore, if I use plain old InsertPage it works fine. Any help is appreciated.

Thanks,
Doc.It Development
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

Hello docu-track99,

Can you specify the error code that you're getting so that we can reproduce the same behavior on our side?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Re: Creating a new doc and inserting pages programmatically

Post by docu-track99 »

Hi Alex,

Error at PDFXEdit.IPXC_Pages.InsertPagesFromDoc(IPXC_Document pSrcDoc, UInt32 [blablabla...])

Error HRESULT E_FAIL has been returned from a call to a COM component.

Thanks,
Doc.It Development
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

Hello docu-track99,

I've tried doing the same thing and everything is worked fine:

Code: Select all

			PDFXEdit.IPXC_Document curDoc = pdfCtl.Doc.CoreDoc;
			PDFXEdit.IPXC_Document newDoc = pdfCtl.Inst.NewCoreDoc();
			newDoc.Pages.InsertPagesFromDoc(curDoc, 0, 0, curDoc.Pages.Count, (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Annots_Copy | (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Bookmarks_CopyAll);
Note that the third and fourth parameter in the InsertPagesFromDoc method are Starting Page and Pages Count NOT the End Page. Maybe the problem lies there.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Re: Creating a new doc and inserting pages programmatically

Post by docu-track99 »

Very strange indeed. Thanks for pointing that out, but I've ruled out any issues with the parameters. I recently updated the control to 316, maybe the version you tested it on worked better? I also tried it on a machine with 315. Still no luck.

I even took your CSharp demo solution and added the code to a button. Opened a document, clicked the button, same exception.

Also, I'm on Windows 10. I tried disabling UAC completely as I thought it might be messing around with the temp areas. Still no luck.

Do you have any suggestions I could try?

Thanks,
Doc.It Development
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

From what I can tell, the problem does not occur right now in the code that would go into the next release. So I would suggest waiting for a week for the new release and then check. Because there were several bugs with the Insert Pages inner logic - probably they were fixed.

HTH,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

One more thing - maybe the problem lies in the PDF files that you are using? Can you post one that has a problem with these methods as an attachment so we can try recreating this behavior here?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Re: Creating a new doc and inserting pages programmatically

Post by docu-track99 »

I've attached a sample PDF. I have tried a variety of of PDFs and all of them have the same problem. Also, just for my own sanity I hope it's OK if I include a modified version of your CSharp demo solution where I added the insert pages code to a button. It's kind of sloppy but works as a quick test. Maybe you can try running the solution and testing it with the sample PDF.

If it doesn't work for you I would say maybe there is something wrong with the code (although I copied directly from you). Could be something wrong with the PDF as well of course, but you can rule that out if you run it on your own solution. If it does work for you, the code is fine and should therefore be working in my tests too, so there must be something either with the control or maybe some other factor we aren't thinking about. I'm sure we'll figure it out sooner or later...

Thanks,
Doc.It Development
You do not have the required permissions to view the files attached to this post.
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

Hello docu-track99,

I've recreated the problem by using the dll from the 316.1 build. From what I can say - the problem does not occur with the current code that will go to the 317 build, so after a week or so you can try it for yourself and see whether it was fixed.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Re: Creating a new doc and inserting pages programmatically

Post by docu-track99 »

Hello Alex,

Since the update, I've tried the InsertPagesFromDoc method again. I am no longer getting the previous error, however, I am getting a new exception now, "Value does not fall within the expected range" on the nStartPage parameter. It appears that anything greater than a start page of 0 will cause this error. Can you please confirm that you get the same error? If so, can you suggest an alternative to this type of functionality so that I can implement the alternative for the time being?

Thanks,
Doc.It Development
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

Hello docu-track99,

I confirmed the same error when I tried doing this:

Code: Select all

newDoc.Pages.InsertPagesFromDoc(curDoc, 0, 1, curDoc.Pages.Count, (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Annots_Copy | (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Bookmarks_CopyAll);
The error occurs because you specify pages count that exceeds the possible number of pages available in the document. The correct code should look like:

Code: Select all

newDoc.Pages.InsertPagesFromDoc(curDoc, 0, 1, curDoc.Pages.Count - 1, (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Annots_Copy | (uint)PDFXEdit.PXC_InsertPagesFlags.IPF_Bookmarks_CopyAll);
Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
docu-track99
User
Posts: 518
Joined: Thu Dec 06, 2007 8:13 pm

Re: Creating a new doc and inserting pages programmatically

Post by docu-track99 »

Hi Alex,

Thanks, I must've been tired when I posted. Instead of specifying an actual page count I was specifying the page that I wanted the count to stop at. So I would get this error. No problems here and it seems the insert pages works really well now.

Thanks,
Doc.It Development
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: Creating a new doc and inserting pages programmatically

Post by Sasha - PDF-XChange »

Glad that helped docu-track99, we'll mark this topic as closed.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ