Highlighted

Exporting single pages as pdf with prefix

Participant ,
Jan 16, 2018

Copy link to clipboard

Copied

I'm very new to scripting, only able to recognize the basics at this point.

I've been browsing trough the forums looking for a script which will export single pages from my document, taking a prefix from a text box on the page with a specific paragraph style. This script is doing it pretty much as I'd like it: Script Export Single Pages with Custom Filename and Paragraph Style?  Except I need it to export pdf's instead of jpegs.

This is my (failing) attempt, changing the jpg output to pdf:

if (app.documents.length != 0) { 

     var myDoc = app.activeDocument; 

    MakePdffile(); 

else{   

     alert("Please open a document and try again.");   

}  

 

 

function MakePdffile() {  

 

 

 

 

 

 

     for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) { 

 

          if (myDoc.pages.item(myCounter).appliedSection.name != "") { 

               myDoc.pages.item(myCounter).appliedSection.name = ""; 

          } 

             

            var myPageName = myDoc.pages.item(myCounter).name; 

          var myPdfPrefix = ""; 

          var isStyleExist = true; 

 

          //Checking the existing of the paragraph style (filename) 

          try { 

              myDoc.paragraphStyles.item("title"); 

          } 

          catch (myError) { 

              isStyleExist = false; 

          } 

 

          if (isStyleExist) 

 

 

          myPdfPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter)) + myPageName; 

           

 

          var myFilePath = "C:/Temp/" + myPdfPrefix + ".pdf"; 

          var myFile = new File(myFilePath); 

          var myPdfExportPreset = app.PdfExportPresets.item("test setting v1");//Set pdf preset

          myDoc.exportFile(ExportFormat.PdfType, myFile, myPdfExportPreset);

     } 

 

 

// The new function, but needs to add checking the file name rules. 

 

 

function getParagraphContent (paragraphStyle, currentPage) 

    var paragraphContent = null; 

    for (var c = 0; c < currentPage.textFrames.length; c++) 

    { 

        for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++) 

        { 

            if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle) 

            { 

                paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents; 

   // Remove spaces and returns at the end: 

                paragraphContent = paragraphContent.replace(/\s+$/, ''); 

   // Replace remaining spaces with hyphen: 

                paragraphContent = paragraphContent.replace(/\s+/g, '-'); 

   // Make lowercase: 

                paragraphContent = paragraphContent.toLowerCase(); 

                return paragraphContent; 

            } 

        } 

    } 

    return paragraphContent; 

}

Can somebody point me out in the right direction? Many thanks in advance!

Try this  code...

// Check if document is open     

if(app.documents.length >0){

               // Ask for target folder

               var myFolder = Folder.selectDialog("Please select a target folder...");

               

                // Check if a target folder was selected

                if (myFolder != null){

                           

                            var myDoc = app.activeDocument;

                            var fileName = myDoc.name;

                           

                            // Do for each page in active document

                            for(x = 0; x < myDoc.pages.length; x++){

                                        // Get current page name

                                        var myPageName = myDoc.pages.name;

                               

                                        // Set the PDF export page range to the page name

                                        app.pdfExportPreferences.pageRange = myPageName;

                                      

                                        // Get the text from the page

                                        var myText = getTextFromPageUsingParagraphStyle(myDoc.pages);

                                       

                                        // Create new filename

                                        var myFileName = fileName.replace(".indd", "_" + myText + ".pdf");

                                       

                                        // Create new filepath

                                        var myFilePath = myFolder.fullName + "/" + myFileName;

                                       

                                        // Create new file object

                                        var myFile = File(myFilePath);

                                       

                                        // Check if PDF-file allready exists

                                        if (myFile.exists == false){

                                                // Export the file

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");

                                        }else{

                                                alert("Error: PDF-file allready exists !");

                                        }

                            }

                       

                                alert("Done !");

                  }else{

                                alert("Error: Script aborted !");

                  }

}else{

                        alert("Error: No documents are opened !");

}

function getTextFromPageUsingParagraphStyle(myPage){

          // Set the find options

          app.findTextPreferences = app.changeTextPreferences = null;

          app.findTextPreferences.appliedParagraphStyle = "title";

          // Get all textframes from the page

          var myTextframes = myPage.textFrames;

        

          // Set the default text

          var myText = "";

        

          // Search every textframe using the find options

          for (y=0; y < myTextframes.length; y++){

                var mySearchResult = myTextframes.findText();

                if (mySearchResult.length > 0){

                        myText = mySearchResult[0].contents;

                        break;

                }

          }

         return myText;

}

Topics

Scripting

Views

2.2K

Likes

Translate

Translate

Report

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

Exporting single pages as pdf with prefix

Participant ,
Jan 16, 2018

Copy link to clipboard

Copied

I'm very new to scripting, only able to recognize the basics at this point.

I've been browsing trough the forums looking for a script which will export single pages from my document, taking a prefix from a text box on the page with a specific paragraph style. This script is doing it pretty much as I'd like it: Script Export Single Pages with Custom Filename and Paragraph Style?  Except I need it to export pdf's instead of jpegs.

This is my (failing) attempt, changing the jpg output to pdf:

if (app.documents.length != 0) { 

     var myDoc = app.activeDocument; 

    MakePdffile(); 

else{   

     alert("Please open a document and try again.");   

}  

 

 

function MakePdffile() {  

 

 

 

 

 

 

     for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) { 

 

          if (myDoc.pages.item(myCounter).appliedSection.name != "") { 

               myDoc.pages.item(myCounter).appliedSection.name = ""; 

          } 

             

            var myPageName = myDoc.pages.item(myCounter).name; 

          var myPdfPrefix = ""; 

          var isStyleExist = true; 

 

          //Checking the existing of the paragraph style (filename) 

          try { 

              myDoc.paragraphStyles.item("title"); 

          } 

          catch (myError) { 

              isStyleExist = false; 

          } 

 

          if (isStyleExist) 

 

 

          myPdfPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter)) + myPageName; 

           

 

          var myFilePath = "C:/Temp/" + myPdfPrefix + ".pdf"; 

          var myFile = new File(myFilePath); 

          var myPdfExportPreset = app.PdfExportPresets.item("test setting v1");//Set pdf preset

          myDoc.exportFile(ExportFormat.PdfType, myFile, myPdfExportPreset);

     } 

 

 

// The new function, but needs to add checking the file name rules. 

 

 

function getParagraphContent (paragraphStyle, currentPage) 

    var paragraphContent = null; 

    for (var c = 0; c < currentPage.textFrames.length; c++) 

    { 

        for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++) 

        { 

            if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle) 

            { 

                paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents; 

   // Remove spaces and returns at the end: 

                paragraphContent = paragraphContent.replace(/\s+$/, ''); 

   // Replace remaining spaces with hyphen: 

                paragraphContent = paragraphContent.replace(/\s+/g, '-'); 

   // Make lowercase: 

                paragraphContent = paragraphContent.toLowerCase(); 

                return paragraphContent; 

            } 

        } 

    } 

    return paragraphContent; 

}

Can somebody point me out in the right direction? Many thanks in advance!

Try this  code...

// Check if document is open     

if(app.documents.length >0){

               // Ask for target folder

               var myFolder = Folder.selectDialog("Please select a target folder...");

               

                // Check if a target folder was selected

                if (myFolder != null){

                           

                            var myDoc = app.activeDocument;

                            var fileName = myDoc.name;

                           

                            // Do for each page in active document

                            for(x = 0; x < myDoc.pages.length; x++){

                                        // Get current page name

                                        var myPageName = myDoc.pages.name;

                               

                                        // Set the PDF export page range to the page name

                                        app.pdfExportPreferences.pageRange = myPageName;

                                      

                                        // Get the text from the page

                                        var myText = getTextFromPageUsingParagraphStyle(myDoc.pages);

                                       

                                        // Create new filename

                                        var myFileName = fileName.replace(".indd", "_" + myText + ".pdf");

                                       

                                        // Create new filepath

                                        var myFilePath = myFolder.fullName + "/" + myFileName;

                                       

                                        // Create new file object

                                        var myFile = File(myFilePath);

                                       

                                        // Check if PDF-file allready exists

                                        if (myFile.exists == false){

                                                // Export the file

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");

                                        }else{

                                                alert("Error: PDF-file allready exists !");

                                        }

                            }

                       

                                alert("Done !");

                  }else{

                                alert("Error: Script aborted !");

                  }

}else{

                        alert("Error: No documents are opened !");

}

function getTextFromPageUsingParagraphStyle(myPage){

          // Set the find options

          app.findTextPreferences = app.changeTextPreferences = null;

          app.findTextPreferences.appliedParagraphStyle = "title";

          // Get all textframes from the page

          var myTextframes = myPage.textFrames;

        

          // Set the default text

          var myText = "";

        

          // Search every textframe using the find options

          for (y=0; y < myTextframes.length; y++){

                var mySearchResult = myTextframes.findText();

                if (mySearchResult.length > 0){

                        myText = mySearchResult[0].contents;

                        break;

                }

          }

         return myText;

}

Topics

Scripting

Views

2.2K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jan 16, 2018 0
Participant ,
Jan 18, 2018

Copy link to clipboard

Copied

Ho Jojakeem,

Try this one...

    

// Check if document is open    

if(app.documents.length >0){

               // Ask for target folder

               var myFolder = Folder.selectDialog("Please select a target folder...");

              

                // Check if a target folder was selected

                if (myFolder != null){

                          

                            var myDoc = app.activeDocument;

                            var fileName = myDoc.name;

                          

                            // Do for each page in active document

                            for(x = 0; x < myDoc.pages.length; x++){

                                        // Get current page name

                                        var myPageName = myDoc.pages.name;

                              

                                        // Set the PDF export page range to the page name

                                        app.pdfExportPreferences.pageRange = myPageName;

                                      

                                        // Create new filename

                                        var myFileName = fileName.replace(".indd", "_" + myPageName + ".pdf");

                                      

                                        // Create new filepath

                                        var myFilePath = myFolder.fullName + "/" + myFileName;

                                      

                                        // Create new file object

                                        var myFile = File(myFilePath);

                                      

                                        // Check if PDF-file allready exists

                                        if (myFile.exists == false){

                                                // Export the file

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");

                                        }else{

                                                alert("Error: PDF-file allready exists !");

                                        }

                            }

                      

                                alert("Done !");

                  }else{

                                alert("Error: Script aborted !");

                  }

}else{

                        alert("Error: No documents are opened !");

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 18, 2018 0
Participant ,
Jan 18, 2018

Copy link to clipboard

Copied

Hi Tmmls,

Thanks for the reply! I guess it is a somewhat similar script. It generates seperate pdf's and also the promt for target folde is useful.

Now if that could be combined with getting input from the 'title' paragraph styled text and use that as a file prefix, it would be it!

The eventual idea is to put a title from a data merge source in the slug of a page and let it be the prefix of a single page pdf file name. For instance 'Business-card_John-Doe.pdf'

It should be something like this bit of code to add I believe:

  1.             var myPageName = myDoc.pages.item(myCounter).name;   
  2.           var myPdfPrefix = "";   
  3.           var isStyleExist = true;   
  4.    
  5.           //Checking the existing of the paragraph style (filename)   
  6.           try {   
  7.               myDoc.paragraphStyles.item("title");   
  8.           }   
  9.           catch (myError) {   
  10.               isStyleExist = false;   
  11.           }   
  12.    
  13.           if (isStyleExist)   
  14.    
  15.    
  16.           myPdfPrefix = getParagraphContent(myDoc.paragraphStyles.item("title"), myDoc.pages.item(myCounter)) + myPageName;   
  17.              
  18.    
  19.           var myFilePath = "C:/Temp/" + myPdfPrefix + ".pdf";  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 18, 2018 0
Participant ,
Jan 19, 2018

Copy link to clipboard

Copied

Try this  code...

// Check if document is open     

if(app.documents.length >0){

               // Ask for target folder

               var myFolder = Folder.selectDialog("Please select a target folder...");

               

                // Check if a target folder was selected

                if (myFolder != null){

                           

                            var myDoc = app.activeDocument;

                            var fileName = myDoc.name;

                           

                            // Do for each page in active document

                            for(x = 0; x < myDoc.pages.length; x++){

                                        // Get current page name

                                        var myPageName = myDoc.pages.name;

                               

                                        // Set the PDF export page range to the page name

                                        app.pdfExportPreferences.pageRange = myPageName;

                                      

                                        // Get the text from the page

                                        var myText = getTextFromPageUsingParagraphStyle(myDoc.pages);

                                       

                                        // Create new filename

                                        var myFileName = fileName.replace(".indd", "_" + myText + ".pdf");

                                       

                                        // Create new filepath

                                        var myFilePath = myFolder.fullName + "/" + myFileName;

                                       

                                        // Create new file object

                                        var myFile = File(myFilePath);

                                       

                                        // Check if PDF-file allready exists

                                        if (myFile.exists == false){

                                                // Export the file

                                                myDoc.exportFile(ExportFormat.pdfType, myFile, false, "Press Quality");

                                        }else{

                                                alert("Error: PDF-file allready exists !");

                                        }

                            }

                       

                                alert("Done !");

                  }else{

                                alert("Error: Script aborted !");

                  }

}else{

                        alert("Error: No documents are opened !");

}

function getTextFromPageUsingParagraphStyle(myPage){

          // Set the find options

          app.findTextPreferences = app.changeTextPreferences = null;

          app.findTextPreferences.appliedParagraphStyle = "title";

          // Get all textframes from the page

          var myTextframes = myPage.textFrames;

        

          // Set the default text

          var myText = "";

        

          // Search every textframe using the find options

          for (y=0; y < myTextframes.length; y++){

                var mySearchResult = myTextframes.findText();

                if (mySearchResult.length > 0){

                        myText = mySearchResult[0].contents;

                        break;

                }

          }

         return myText;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2018 0
Participant ,
Jan 19, 2018

Copy link to clipboard

Copied

Perfect!

I'm going to study on what you've done here. This is going to be a great tool for me to build upon.

Thank you a lot!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 19, 2018 0
Explorer ,
Jan 23, 2018

Copy link to clipboard

Copied

Hi guys,

I have written something like that. Maybe will be useful.

https://drive.google.com/drive/folders/1HZpmtdt5YknRD6ITCH-k4k-nVKMZx1C7

It works in 3 independent ways for user to choose:

1) can map paragraph text with assigned paragraph style to insert this paragraph text as a pdf file name

2) you can type few letters of paragraph you'd like to use as a file name and script matching whole paragraph as a pdf file name

3) without choosing any of options above it using document file name and adding number at the end

I was using it with text on slug so I had only one paragraph but discovered few limitations:

- if you'll not use "return" sign at the end of paragraph script will cut last letter of text from this paragraph as it automatically cutting "return" sign from the end (couldn't make it work with grep)

- if you're using matching with paragraph style it's recommended to use this style only for paragraph you want to mach as a name (script taking 1st paragraph with selected paragraph style to mach name and ignoring other on the same page - first doesn't mean one on the very top. it depends of textframes creation order, will not explaining that"

- text to match can't be on locked layer.

- there's no progress bar, and if you'll think it's frozen...be patience...it's working

there's a dropdown with pdf settings - this settings are definitions from indesign so create your own pdf export preset if you'd like different than listed

additional feature is you can type range of pages to export. ex: if you have 100pages document and you'll type 3 pages per pdf it will make you a pdfs with 3 pages each and from each 1st it'll match wanted text as a file name.

pdfs will be saved in your indd file location in folder called "single_PDFs"

help yourselves...hope it will help

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jan 23, 2018 0
Explorer ,
Jul 15, 2020

Copy link to clipboard

Copied

"1) can map paragraph text with assigned paragraph style to insert this paragraph text as a pdf file name"

OMG: I have been looking for this, all the while trying to modify a script that does this to JPGs - without success.

 

Your Google Drive link does, to my despair, not work. Would it be possible for you to re-post the this script in text form here or a link elsewere? 

 

Many thanks in advance! : ) 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 15, 2020 0