Delete selected pages

  • I'm trying to delete the selected pages in a pdf, but can't seem to figure it out...
    The idea here is that the user has selected one or more pages in a pdf (loaded in pPDFViewer). I then save the selected pages to a tempfile for future use. This works fine. The rest of the pages should then be available for the user to make another selection, but without the pages previously selected. I therefore try to delete the pages from the original document, but the COMPDF_PageSelectionGet-command returns -1 no matter what I do, and consequently the page aren't deleted. Did I miss anything?

    Code
    int Res = 0;
    Res = pPDFViewer->Plus->SaveSelectionToFile(TempFileName);                       // Res = 65536
    int SelectedCount = pPDFViewer->CommandEx(COMPDF_PageSelectionGet, -1);          // SelectedCount = -1
    for (int i = 0, j = 0; i < pPDFViewer->PageCount && j < SelectedCount; i++) {    // pPDFViewer->PageCount = 55 for my testdocument
      if ((Res = pPDFViewer->CommandEx(COMPDF_PageSelectionGet, i)) == 1) {          // Res = -1, both for selected and unselected pages
        Res = pPDFViewer->DeletePage(i);
        j++;
      }
    }
    Res = pPDFViewer->Command(COMPDF_PageSelectionClear);

    I guess I could invert the selection, save the selection as a new tempfile and load this as my new original... but I don't want to :)

    • Offizieller Beitrag

    Hi,

    The problem is here, that DeletePage will remove the page from the visible page list and so the PageCount will change.

    You need to do the loop backwards.

    COMPDF_PageSelectionGet also needs to be called with i+1 (Page = [1..N])
    EDIT - this was wrong. PageSelectionGet should work with 0..N


    Julian

  • Good tip, I'll do that :)
    But the real problem is that CommandEx(COMPDF_PageSelectionGet, -1) returns -1, while I expected it to return the number of selected pages, and that CommandEx(COMPDF_PageSelectionGet, i) returns -1, while I expected it to return 0 for unselected and 1 for selected pages. I guess -1 indicates that something has gone horribly wrong, but what? Is there something like a GetLastError() or something?

  • I also get some problems when trying to rotate pages with CommandStrEx(COMPDF_RotatePage, "all", Angle). Stringparameter "all" crashes with the message ''all' ist kein gültigner Integerwert', which I guess means something like 'all is not a valid number', even though the documentation says I can use "all" and "selected" as parameters here. When trying a number here, e.g. "3" the command returns -1, but does rotate the page. A parameter like "3-4" which I would expect to rotate pages 3 and 4 does nothing.

    With all these commands returning -1 I'm starting to suspect that I need to enable commands in some way, or that something else is fundamentally wrong. I have tried setting ViewOptions with
    pPDFViewer->ViewOptions = pPDFViewer->ViewOptions << wpSelectClickedPage << wpShowPageMultiSelection << wpDisableBookmarkView;

    A short bit of example code would be much appreciated :)

    • Offizieller Beitrag

    Hi,

    You are right, there is a problem. I will post a new release tonight - please then see manual for changes and example code.

    One thing though:

    DeletePage works with a phsical page number, not a logical number like the selection. (Physical pages are those who are in memory but not necessarily displayed. Logical are what is displayed.)

    The command

    WPViewPDF1->CommandEx(COMPDF_ConvertLogicalPageNr, i));

    is used to convert a logical 0..N number into a physical page 0..N number.

    This code I am using right now for testing:

    Code
    WPViewPDF1.CommandEx(COMPDF_BEGINUPDATE,0);
    c := WPViewPDF1.CommandEx(COMPDF_GetPageCount, 0);
    for i:=c-1 downto 0 do
      if  WPViewPDF1.CommandEx( COMPDF_PageSelectionGet, i )=1 then
           WPViewPDF1.CommandEx(COMPDF_DeletePage,
           WPViewPDF1.CommandEx(COMPDF_ConvertLogicalPageNr, i));
    WPViewPDF1.CommandEx(COMPDF_ENDUPDATE,0);

    BEGINUPDATE/ENDUPDATE will be in next release.


    Julian

  • Downloaded and installed new version, problem remains.

    CommandStrEx(COMPDF_RotatePage, "all", 90) crashes with "Project ### raised exception class EConvertError with message ''all' ist kein gültiger Integerwert'."
    CommandStrEx(COMPDF_RotatePage, "selected", 90) crashes with "Project ### raised exception class EConvertError with message ''selected' ist kein gültiger Integerwert'."
    CommandStrEx(COMPDF_RotatePage, "3", 90) works.
    CommandStrEx(COMPDF_RotatePage, "3-5", 90) works.

    I can make a work-around for this, if I can get info on the selection, but:

    CommandEx(COMPDF_PageSelectionGet, -1) returns -1.
    CommandEx(COMPDF_PageSelectionGet, 0) returns -1.
    CommandEx(COMPDF_PageSelectionGet, 1) returns -1.
    It doesn't seem to matter if I select none, one or several pages.

    I'm doing this in C++ on CodeGear C++ Builder 2007, if that matters at all.

    The code I'm trying is:

  • My bad... just had a brainwave and checked to see if the version I though I was running was the version I was really running... which it wasn't :(

    Everything seems to be working now... thanks :)