Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
15

Add pdf button not working

Community Beginner ,
Sep 05, 2023 Sep 05, 2023
i want when i click on Add Pdf button it should open a file explorer and open we can select pdf file and then i can use that pdf file anywhere .. Here is my code pls check and help me 
function
showUploadDialog() {
  // Dialog Definition
  var oDlg = {
    usnm: {
      flnm: "",
      vers: "",
      lfnu: "",
      lccn: "",
    },
    initialize: function (dialog) {
      dialog.load(this.usnm);
    },
    commit: function (dialog) {
      var data = dialog.store();
      this.usnm.flnm = data.flnm;
      this.usnm.vers = data.vers;
      this.usnm.lfnu = data.lfnu;
      this.usnm.lccn = data.lccn;
    },
    description: {
      name: "Test Dialog",
      elements: [
        {
          type: "view",
          elements: [
            { name: "Filename:", type: "static_text" },
            { item_id: "flnm", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "Version:", type: "static_text" },
            { item_id: "vers", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "LegalEdge File Number:", type: "static_text" },
            { item_id: "lfnu", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "LegalEdge Court Case Number:", type: "static_text" },
            { item_id: "lccn", type: "edit_text", char_width: 30 },
            { type: "button", name: "Add PDF", item_id: "add_pdf_button" },
            { type: "ok_cancel" },
          ],
        },
      ],
    },
  };

  // Dialog Activation
  oDlg.usnm.flnm = "Larry";
  oDlg.usnm.vers = "1.0";
  oDlg.usnm.lfnu = "Sample";
  oDlg.usnm.lccn = "Sample2";

  var response = app.execDialog(oDlg);

  // Attach a click event listener to the "Add PDF" button
  if (response === "ok") {
    var addPdfButton = this.getField("add_pdf_button"); // Access the "Add PDF" button directly

    addPdfButton.setAction("MouseUp", function () {
      addPdfButtonClicked = true; // Set the flag
      openFileExplorerForPdf(); // Call the function to open the file explorer
    });
  }
}

// Define a global variable to indicate whether the "Add PDF" button was clicked
var addPdfButtonClicked = false;

// Function to open the file explorer dialog
function openFileExplorerForPdf() {
  if (addPdfButtonClicked) {
    var cAttachmentPath = app.browseForDoc();
    if (cAttachmentPath) {
      var attachmentCounter = 0;
      attachmentCounter++;
      var fileAttachment = this.addAnnot({
        page: 0,
        type: "FileAttachment",
        rect: [100, 100, 200, 200],
        contents: "Attached file " + attachmentCounter,
        cAttachmentPath: cAttachmentPath,
      });
    }
    addPdfButtonClicked = false; // Reset the flag
  }
}
TOPICS
Cancel subscription , Comment review and collaborate Experiment , Crash or freeze , Create PDFs , Edit and convert PDFs , General troubleshooting , How to , Install update and subscribe to Acrobat , JavaScript , Modern Acrobat , PDF , PDF forms , Print and prepress , Rich media and 3D , Scan documents and OCR , Security digital signatures and esignatures , Standards and accessibility
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Any error message?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2023 Sep 05, 2023

here is the updated code , no error nothing showing ,and after click on add pdf still not happenig anythig @Bernd Alheit 

function showUploadDialog() {
  // Dialog Definition
  var oDlg = {
    usnm: {
      flnm: "",
      vers: "",
      lfnu: "",
      lccn: "",
    },
    initialize: function (dialog) {
      dialog.load(this.usnm);
    },
    commit: function (dialog) {
      var data = dialog.store();
      this.usnm.flnm = data.flnm;
      this.usnm.vers = data.vers;
      this.usnm.lfnu = data.lfnu;
      this.usnm.lccn = data.lccn;
    },
    description: {
      name: "Test Dialog",
      elements: [
        {
          type: "view",
          elements: [
            { name: "Filename:", type: "static_text" },
            { item_id: "flnm", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "Version:", type: "static_text" },
            { item_id: "vers", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "LegalEdge File Number:", type: "static_text" },
            { item_id: "lfnu", type: "edit_text", char_width: 30 },
          ],
        },
        {
          type: "view",
          elements: [
            { name: "LegalEdge Court Case Number:", type: "static_text" },
            { item_id: "lccn", type: "edit_text", char_width: 30 },
            { type: "button", name: "Add PDF", item_id: "add_pdf_button" },
            { type: "ok_cancel" },
          ],
        },
      ],
    },
  };

  // Dialog Activation
  oDlg.usnm.flnm = "Larry";
  oDlg.usnm.vers = "1.0";
  oDlg.usnm.lfnu = "Sample";
  oDlg.usnm.lccn = "Sample2";

  var response = app.execDialog(oDlg);

  // Attach a click event listener to the "Add PDF" button
  if (response === "ok") {
    var addPdfButton = oDlg.description.elements[3].elements[2]; // Access the "Add PDF" button directly

    addPdfButton.onClick = function () {
      addPdfButtonClicked = true; // Set the flag
      openFileExplorerForPdf(); // Call the function to open the file explorer
    };
  }
}

// Define a global variable to indicate whether the "Add PDF" button was clicked
var addPdfButtonClicked = false;

// Function to open the file explorer dialog
function openFileExplorerForPdf() {
  if (addPdfButtonClicked) {
    var cAttachmentPath = app.browseForDoc();
    if (cAttachmentPath) {
      var attachmentCounter = 0;
      attachmentCounter++;
      var fileAttachment = this.addAnnot({
        page: 0,
        type: "FileAttachment",
        rect: [100, 100, 200, 200],
        contents: "Attached file " + attachmentCounter,
        cAttachmentPath: cAttachmentPath,
      });
    }
    addPdfButtonClicked = false; // Reset the flag
  }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Where does you use this functions?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 05, 2023 Sep 05, 2023

i am using this function for adobe plugin , made a js file and paste that js file in this path C:\Program Files\Adobe\Acrobat DC\Acrobat\Javascripts , its just a function if u need full code i can provide you , so you can also run in you system 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Looks like that the item_id of the button is wrong. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 05, 2023 Sep 05, 2023
LATEST

Bernd is correct, the "item_id" for an element in a custom dialog must be 4 characters, no more, no less. 

 

However, this is only one problem with the code, there are many others. You need to adopt a different development strategy. Simplify and solve one issue at a time. For example, get the custom dialog working before adding other code. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines