Working with PDF layers programmatically

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
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Working with PDF layers programmatically

Post by SMan »

Hi everyone,

now that I've successfully updated the version of PDF-XChange Editor SDK used in our software, we would like to use some of the Editor's functionality to work with PDF layers as e.g. described in this or this thread - only programmatically instead of manually through the UI. (I understand some of these features are very new and there may be more PDF layer functionality in development right at this time.)

Specifically, we would like to be able to do the following programmatically:
  1. add new PDF layers to a document
  2. move annotations that have been selected manually to a specific layer
  3. change the visibility of a specific layer
  4. change the properties of a specific layer (in particular, initial state for visibility, print and export)
  5. flatten all layers (or, once that functionality is available, export specific layers to a new document)
(Somehow this must also involve the ability to programmatically identify and select specific layers that at an earlier time have been added to the document. When experimenting in the PDF editor manually, I accidentally found out that you can give identical names to more than one layer, which means that the "name" is not a reliably unique identifier. The layers that need to be manipulated as per point 2 through 4 would always be layers that in an earlier step were added by our own software as per point 1.)

So far, I haven't been able to find anything about these tasks in the SDK documentation or the Full Demo sample.

Thanks in advance for any assistance!

Best regards,
Sven (SMan)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Working with PDF layers programmatically

Post by Sasha - Tracker Dev Team »

Hello SMan,

The layers and optional content are not a simple matter. For starters, you should understand what a layer is and what are the OCGs and OCMDs, and how they a linked by reading a PDF Specification. Then you will understand how all of this works.
As for your requirements, adding a new layer is not available via the SDK directly.
To move an annotation to the specific Optional Content Group, you should modify it's properties with the needed OCG.
To change the OCG properties, the op.layers.modify operation should be used
Flattening all OCGs is available via the command execution only.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: Working with PDF layers programmatically

Post by SMan »

Hello Alex,
Sasha - Tracker Dev Team wrote: Mon Aug 10, 2020 9:17 am The layers and optional content are not a simple matter. For starters, you should understand what a layer is and what are the OCGs and OCMDs, and how they a linked by reading a PDF Specification. Then you will understand how all of this works.
I've done so now with the PDF 1.7 specification. I hope that nothing fundamental has changed with respect to OCGs/layers in PDF 2.0. However, I haven't found anything explicit about whether the name of an OCG (= layer) has to be unique within a given document (so presumably they don't have to be), and it looks like OCGs don't have any property at all which can serve as a unique identifier for OCGs within a given document. This may be an issue for our ideas.
Sasha - Tracker Dev Team wrote: Mon Aug 10, 2020 9:17 am As for your requirements, adding a new layer is not available via the SDK directly.
Too bad - if that is the case then that is going to be a show-stopper for now. But this is at least a clear statement which I can work with, so thanks for that. Is there any hope that this will become possible with a later version of the SDK in the not too distant future?
Sasha - Tracker Dev Team wrote: Mon Aug 10, 2020 9:17 am Flattening all OCGs is available via the command execution only.
That would be the command "cmd.layersView.flattenLayers", correct?

Regards,
Sven (SMan)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Working with PDF layers programmatically

Post by Sasha - Tracker Dev Team »

Hello SMan,

An unique identifier for the OCG is it's object number in the PDF structure.
We'll see whether we can add an addNewLayer operation to the SDK in one of the Future releases.
The Flatter Layers command is one that you need.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: Working with PDF layers programmatically

Post by SMan »

Hello Alex,
Sasha - Tracker Dev Team wrote: Fri Aug 14, 2020 9:45 am An unique identifier for the OCG is it's object number in the PDF structure.
Good to know. In this case, we'd also need ways to get the object number of a layer that has been selected (e.g. manually), to programmatically select and/or otherwise modify a layer based on its object number.
Sasha - Tracker Dev Team wrote: Fri Aug 14, 2020 9:45 am We'll see whether we can add an addNewLayer operation to the SDK in one of the Future releases.
Also good to know. Any timeframe on that, maybe?

And while I'm at it, we could also use a functionality to export selected layers to new documents while preserving those layers, and to merge a layer from one document into another document (where one would be the original or both would be derived from the same original document), all programmatically.

Best regards,
Sven (SMan)
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: Working with PDF layers programmatically

Post by SMan »

Hi again,

In the meantime I have found a way to add layers (OCGs) to a PDF document using iTextSharp. Now everything that we would like to do with layers is on the table again and it would be great if we could use PDF-XChange Editor SDK for some of these tasks. So I have some more questions:
  • Is it possible to programmatically get a reference to a collection containing the OCGs of a document?
  • Is it possible to programmatically access the title of a particular OCG?
  • As a shortcut, is it possible to get a reference to a particular OCG or a collection of OCGs if their title(s) match a given string?
Also, we use PDF-XChange Editor SDK, among other things, to place images as stamps on documents. Here is a stripped down version of the VB.net code that does this:

Code: Select all

    Private Sub SetStampOnDocument(strImageFilePath As String)

        Dim inst As PXV_Inst = PdfEditor.Inst
        Dim doc As IPXV_Document = PdfEditor.Doc

        Dim nID As Integer = inst.Str2ID("op.document.addImage", False)

        Dim Op As IOperation = inst.CreateOp(nID)

        Dim input As ICabNode = Op.Params.Root("Input")
        input.v = doc

        Dim options As ICabNode = Op.Params.Root("Options")
        options("Page").v = 0 'First page
        options("Pos.top").v = 700
        options("Pos.left").v = 50
        options("Pos.right").v = 550
        options("Pos.bottom").v = 50

        options("AlignMode").v = 0 'Top left
        options("AsStamp").v = True 'Do not insert as a part of the content

        Dim fsInst As IAFS_Inst = DirectCast(inst.GetExtension("AFS"), IAFS_Inst)
        Dim destPath As IAFS_Name = fsInst.DefaultFileSys.StringToName(strImageFilePath) ' Converting string to name

        options("Src").v = destPath

        Try
            Op.Do()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
I know that such a stamp can be moved to an OCG interactively with PDF-XChange Editor (assuming the document has any). It would be great, however, if the stamp could programmatically be put in the OCG directly at or immediately after its creation. Are there any specific options, commands or methods I could add to the above code to achieve this?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Working with PDF layers programmatically

Post by Sasha - Tracker Dev Team »

Hello SMan,

The thing is that right now the SDK is not ready for this yet as we will need a Document's OCManager interface of some sort as a starting point and we don't have it yet.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
SMan
User
Posts: 23
Joined: Tue May 15, 2018 10:18 am

Re: Working with PDF layers programmatically

Post by SMan »

Hello Alex,

ok, thanks for the feedback. While we've been able to make a bit of progress with iTextSharp, we'll definitely need some functionality in the XChange Editor for what we want to achieve, so we've shelved that particular project for the time being and will await future developments in the SDK.

Best regards,
Sven (SMan)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Working with PDF layers programmatically

Post by Sasha - Tracker Dev Team »

Hello SMan,

The layers functionality is still under active development. When it's ready, we can think about including some of it in the SDK.

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