Skip to main content
Participant
September 5, 2023
Question

Cant add pdf as attachment

  • September 5, 2023
  • 1 reply
  • 277 views

I want after click on add pdfbutton it shoul dopen a file explorer so i can attach a file  ,pls help me , i ll given to u my file if u can help me so u can easly resolve my problem 

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
    console.println("ssuuuuiiiiiiiiiiiiii");
  }
}
This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 5, 2023

The way to develop and debug code is to start simple.  Isolate the bits you are not sure about and figure out those bits before incorporating them into a larger script. 

For example. In the code above, the custom dialog is irrelavant. Why is it there? 

Why is any of the code there? Is the "app.browseForDoc()" working as expected? Or is it adding the attachment that is the problem?  Are you getting an error, or is it just not working. 

Please test only the code that isn't working. Post that, and tell us exactly what isn't working. 

 

 

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