Replace page

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

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.
Post Reply
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Replace page

Post by pHol »

Hi!

I am working on a part of a system handling PDFs and one bit in particular that requires me to extract a page, do some work on that page and then put it back into the document. (replaceing the page that was extracted) I can extract the page and do the work on it just fine, but I'm getting System.AccessViolationException when trying to replace the page. I am using the replacePage operation you have provided a sample code for in the documentation at https://sdkhelp.pdf-xchange.com/vie ... placePages, but it does not say whether it is possible to replace a page on an open document or not. My question is it possible to replace a page in an open document or do I have to close the document, replace the page and then reopen it? If it is possible, I'm guessing I have use the operation incorrect and am wondering how to do it correctly. Basically, is it me misinterpreting the methods or am I using them incorrectly?

The error I get:
System.AccessViolationException occurred
HResult=0x80004003
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
This is the code for my ExtractPage mathod:

Code: Select all

private void ExtractPages(string outputPath, int pageToExtract)
        {
            int nID = Inst.Str2ID("op.document.extractPages", false);
            IOperation Op = Inst.CreateOp(nID);
            ICabNode input = Op.Params.Root["Input"];
            input.v = Doc; // reference to the document that is opened in the Control
            ICabNode options = Op.Params.Root["Options"];
            options["PagesRange.Type"].Int = 8;
            options["PagesRange.Current"].Int = pageToExtract;
            options["CommentsAction"].Int = 0;
            options["FieldsAction"].Int = 0;
            options["BookmarksAction"].Int = 1;
            options["DeletePages"].Bool = false;
            options["ExtractPagesAction"].Int = 1;
            options["OverwriteAll"].Bool = true;
            options["LocalFolder"].String = outputPath;
            options["FileName"].v = "TESTPDF";
            options["OpenFolder"].Bool = false;
            Op.Do();
        }
This is the code for my ReplacePage method:

Code: Select all

private void ReplacePage(string pathToSrcDocument)
        {
            int nID = Inst.Str2ID("op.document.replacePages", false);
            IOperation Op = Inst.CreateOp(nID);
            ICabNode input = Op.Params.Root["Input"];
            input.v = Doc; // reference to the document that is opened in the Control
            ICabNode options = Op.Params.Root["Options"];
            options["PagesRange.Type"].Int = 1;
            options["PagesRange.Filter"].Int = 1; // the source document only have one page and that is why I'm using the "All pages" type and filter
            options["CommentsAction"].Int = 0;
            options["FieldsAction"].Int = 0;
            options["BookmarksAction"].Int = 1;
            options["ContentOnly"].Bool = false;
            IAFS_Inst fsInst = (IAFS_Inst)Inst.GetExtension("AFS");
            IAFS_Name destPath = fsInst.DefaultFileSys.StringToName(pathToSrcDocument);
            options["Src"].v = destPath;
            Op.Do();
        }
I would also like to point out that there is an error in your sample code. The comment you have for

Code: Select all

options["CommentsAction"].v = 0; //Don't copy comments
in both the operation for replace page and in the operation for extract page is incorrect. For both operations, in the options table at https://sdkhelp.pdf-xchange.com/vie ... es_Options it says that input 0 for CommentsAction means Copy and input 2 is don't copy.

PS.I am using the desktop on my computer as output folder for extract and input folder (+ filename) for replace.DS.

//Philip
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Replace page

Post by Sasha - Tracker Dev Team »

Hello Philip,

Please provide a working sample project with a test document and reproduction steps and then we can advice further.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
pHol
User
Posts: 30
Joined: Mon May 08, 2017 9:47 am

Re: Replace page

Post by pHol »

I have found a solution. The documentation was a bit confusing, but once I understood what the different options were I was able to get it to work.

For anyone having a similar issue, here is my working code with comments that is a bit more detailed in how the operation works.

Code: Select all

// Replace a page in a document that is opened in the Control. 
public void ReplacePage(string sourcePathToTakePagesFROM, int page)
{
    int nID = Inst.Str2ID("op.document.replacePages", false);
    IOperation Op = Inst.CreateOp(nID);
    ICabNode input = Op.Params.Root["Input"];
    input.v = Doc;
    ICabNode options = Op.Params.Root["Options"];

    // PagesRange options is referring to the document that one wishes to replace page FROM
    // This means that it is not possible to use "Current page" since there is no reference to "Current page" in the source document (FROM document).
    // If using Exact pages, remember that selected page(s) is 1-indexed 
    // options["PagesRange.Type"].v = 6; (Exact) 
    // options["PagesRange.Text"].v = "1"; (x-y, where x and y are numbers, will give a range taking every page from x to y)
    // Here it is set to take ALL of the pages in the source (FROM) document and replace into the document set as input (input.v = Doc)
    options["PagesRange.Type"].Int = 1;
    options["PagesRange.Current"].Int = 1;

    options["CommentsAction"].Int = 0; // Copy comments
    options["FieldsAction"].Int = 0; // Copy fields
    options["BookmarksAction"].Int = 1; // Copy all
    options["ContentOnly"].Bool = false;

    // Here I am only replacing the page I have as an input
    options["Position"].Int = page; // Start index of the pages that will replaced in the target/opened/working/destination document. 0-indexed but will show as page 1 if using the SetupUI (when inserting 0)
    options["PositionStop"].Int = page; // End index of the pages that will replaced in the target/opened/working/destination document

    IAFS_Inst fsInst = (IAFS_Inst)Inst.GetExtension("AFS"); // if not initiated previously
    IAFS_Name sourcePath = fsInst.DefaultFileSys.StringToName(sourcePathToTakePagesFROM); // sourcePathToTakePagesFROM have be full path
    options["Src"].v = sourcePath; // Source/FROM document

    Op.Do();
}
Hope this can help others with the same issue

// Philip
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Replace page

Post by Sasha - Tracker Dev Team »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply