Print PDF from javascript - javascript

I am using the below mentioned code in my VB.net application to print two copies of pdf document.
js.Append("var pp = this.getPrintParams();")
js.Append("var iCopies = 2;")
js.Append("var iPages = this.numPages;")
js.Append("pp.NumCopies = iCopies;")
js.Append("pp.interactive = pp.constants.interactionLevel.silent;")
js.Append("for ( var i = 0; i < iPages; i++ ) { pp.firstPage = i; pp.lastPage = i;")
js.Append("this.print(pp);")
js.Append("}")
It is working great. But how can I make the last page print only 1 copy instead of two copies.
Your help greatly appreciated.

js.Append("for ( var i = 0; i < iPages; i++ ) { pp.firstPage = i; pp.lastPage = i;")
js.Append("if(i == (iPages - 1)) pp.NumCopies = 1; ") ' This line does it
js.Append("this.print(pp);")
js.Append("}")

Related

Illustrator scripting for text baseline (javascript)

I'm trying to make a script for illustrator but my code seems to not be working and doing nothing on me.
It's supposed to make the "(" and ")" characters baselines to be 3px.
Looking for someone who can make this work.
function test(){
var doc = app.activeDocument;
var t = doc.textFrames[0], thisChar, thisSize, thisBaseline;
for(var i=0; i<t.characters.length; i++){
thisChar = t.characters;
if(thisChar.contents == "(" || thisChar.contents == ")"){
thisSize = thisChar.characterAttributes.size;
thisBaseline = thisChar.characterAttributes.baselineShift;
thisChar.characterAttributes.baselineShift = 3;
}
};
};
test();
var frames = app.activeDocument.selection;
for (var j=0; j<frames.length; j++) {
if (frames[j].typename == "TextFrame") {
for (var i=0; i<frames[j].characters.length; i++) {
var ch = frames[j].characters[i];
if( ch.contents == "(" || ch.contents == ")" ) ch.baselineShift = 3;
}
}
}
It shifts baseline for all brackets within all selected objects.
Note: this simple implementation doesn't handle grouped objects.
var frames = app.activeDocument.textFrames;
for (var j=0; j<frames.length; j++) {
for (var i=0; i<frames[j].characters.length; i++) {
var ch = frames[j].characters[i];
if( ch.contents == "(" || ch.contents == ")" ) ch.baselineShift = 3;
}
}
This variant of the script does the work within all text frames (grouped or not). You don't have to select anything.

Different ways of counting characters in Indesign using javascript

I'm writing a javascript program to handle each character intо Indesign document.
To begin with, I wrote two different ways of counting characters, which for some reason give different results for large documents. Why?
var
myDocument, docStories, docCharacters,
docFootnotesCharacters, docTablesCharacters;
myDocument = app.activeDocument;
var TotalChars = 0;
// Fisrt way
docStories = myDocument.stories.everyItem();
docCharacters = docStories.characters.length;
docFootnotesCharacters = docStories.footnotes.everyItem().characters.length;
docTablesCharacters = docStories.tables.everyItem().cells.everyItem().characters.length;
statReport = [];
// Second way
for ( j = 0; j < myDocument.stories.length; j++ ) {
myStory = myDocument.stories.item(j);
var Frames = myStory.textContainers;
for ( i = 0; i < Frames.length; i++ ) {
var Frame = Frames[i];
for (var TextCnt = 0; TextCnt < Frame.texts.length; TextCnt++) {
CurrentText = Frame.texts.item(TextCnt);
TotalChars += CurrentText.characters.length;
}
for (var TableCnt = 0; TableCnt < Frame.tables.length; TableCnt++) {
var CurrentTable = Frame.tables.item(0);
for ( var CellCnt = 0; CellCnt < CurrentTable.cells.length; CellCnt++ ) {
var CurrentCell = CurrentTable.cells.item(CellCnt);
TotalChars += CurrentCell.characters.length;
}
}
for (var FootNoteCnt = 0; FootNoteCnt < Frame.footnotes.length; FootNoteCnt++) {
var CurrentFootNote = Frame.footnotes.item(0);
TotalChars += CurrentFootNote.characters.length;
}
}
}
statReport.push ( "Characters: " + ( docCharacters + docFootnotesCharacters + docTablesCharacters ) );
statReport.push ( "TotalChars: " + TotalChars );
alert ( statReport.join ( "\r" ), "Document Text Statistic" );
In the second method, you're counting all the characters inside text frames in the story. But stories can be overset (the text overflows). The first method will count overset text (because you're counting the characters in a story, but the second method will ignore those, because it's only counting characters in story frames.

Slow execution of a loop in Apps Script

Basically what I am looking for is any recommendations you can provide me to make this piece of code to execute faster.
The intention of this piece of code its basically by testing some conditions obtain a score and the questions related for each profile selected, and print those scores in the "Results Sheet" and print the questions in Sheets assigned with the name of the profile.
Through some quick and basic testing I discovered whenever the loop starts to print either the results or the questions its where the slow execute starts.
Sorry for the mess on the code I just started programming in Apps
Script several weeks ago.
EDIT
To be quite more specific Im trying to find out if there is any work around to print the questions in the different sheets and values of the profiles. I took a look at the similar question and he addresses a loop using for..in, Im still trying to find out if there is any other logical process I can do to get those values.
var iGrade1 = 0;
var iGrade2 = 0.5;
var iGrade3 = 1;
var resultsSheet = SpreadsheetApp.getActive().getSheetByName('Results');
var iStartingColumn = 5;
var iCellD = 'D';
var iCellE = 'E';
var iTotCounter = 2;
var amountProfiles = profileSheet.getRange
(2, perfilSheet.getLastColumn()).getValue();
var iTotPerf = 0;
var iPT = 1;
var aResizeP = 1;
for( i = 2; i< questionsSheet.getLastRow() ; i++ ){
//Range
sBI = 'B' + i;
sDI = 'D' + i;
//Copies values like 1|2|3|4|, 3|4|, 2|3|, etc.
profileCodes = questionsSheet.getRange(sDI).getValue();
for(j = 0; j < amountProfiles; j++){
//There is a checkbox where user selects if he wants that profile or not
if(profileSheet.getRange(1,iStartingColumn).getValue()
=== 'Yes'){
var sheetProfile = SpreadsheetApp.getActive().getSheetByName
(profileSheet.getRange(3, iStartingColumn).getValue());
var totProfile = profileSheet.getRange(perfilSheet.getLastRow(), iStartingColumn).getValue();
//User can select in a checkbox between 'Bad' 'Regular' and 'Good'
if( ( questionsSheet.getRange( sBI ).getValue() === '× Bad' ) ){
if(profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
iTotPerf = iTotPerf + iGrade1; }
}
else if( ( questionsSheet.getRange( sBI ).getValue() === ' ± Regular' ) ){
if( profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
iTotPerf = iTotPerf + iGrade2; }
}
else if( ( questionsSheet.getRange( sBI ).getValue() === '✓ Good' ) ){
if(profileCodes.split("|").indexOf( String( perfilSheet.getRange(2, iStartingColumn).getValue() ) ) > -1 ){
iTotPerf = iTotPerf + iGrade3; }
}
if ( profileCodes.split('|').indexOf(String
(perfilSheet.getRange(2, iStartingColumn).getValue()))
> -1){
questionsSheet.getRange('A' + i + ':' + 'C' + i)
.copyTo(sheetProfile.getRange('A' + iPT),SpreadsheetApp
.CopyPasteType.PASTE_NORMAL,false);
sheetProfile.autoResizeColumn(aResizeP);
}
if(iTotPerf != 0){
resultadosSheet.getRange(iCellD + iTotCounter).setValue(resultadosSheet.getRange(iCellD + iTotCounter).getValue() + iTotPerf);
resultsSheet.getRange(iCellE + iTotCounter).setValue( ( (resultsSheet.getRange(iCellD + iTotCounter).getValue() * 100)/ totPerfil )/ 100);
}
}
iTotPerf = 0;
iTotCounter++;
iStartingColumn++;
}
iPT += 1;
iTotCounter = 2;
iStartingColumn = 5;
}

Javascript table's pagination

I'm trying to put pagination function into my table and taking this post "Simple pagination in javascript" for reference.
https://codepen.io/duongvu/pen/eewdPG?editors=1111
It's supposed to input the data from my array into the table's class. However it doesn't work in my case.
It freezes and can not proceed when clicking on <next>
Try this out #Duong Vu
for (var i = 0; i < records_per_page; i++)
{
col[i].innerText = content[i + (page-1) * records_per_page].number;
}
function changePage(page) {
var btn_next = document.getElementById("btn_next");
var btn_prev = document.getElementById("btn_prev");
var col = document.getElementsByClassName("no");
var colName = document.getElementsByClassName("name");
var colDetails = document.getElementsByClassName("info");
var page_span = document.getElementById("page");
if (page < 1) page = 1;
if (page > numPages()) page = numPages();
for (var i = (page - 1) * records_per_page; i < (page * records_per_page); i++) {
col[i % records_per_page].innerText = content[i] ? content[i].number : '';
colName[i % records_per_page].innerText = content[i] ? content[i].name : '';
colDetails[i % records_per_page].innerText = content[i] ? content[i].detail : '';
}
}
You are printing only numbers thats why it was not printing any other result. And as you have only 4 rows, so you need to use col[i % records_per_page] to print inside the table data on page changes.
Here is updated codepen.

Generating Radio Button Result Depending On TextArea Value

A few days ago I did not manage to generate a result from my radio button, I managed to solve that problem now. My other problem is how do I generate ONLY few set of the radio button depending on TextArea value changes;
For Example in this code
<script language="javascript" type="text/javascript">
function generatetest() {
codeOne = document.docContainer.text1.value;
codeTwo = document.docContainer.number2.value;
function firstType(){
var codeFour = 0;
for( i = 0; i < document.docContainer.radio4.length; i++ )
{
if( document.docContainer.radio4[i].checked == true )
codeFour = document.docContainer.radio4[i].value;
}
var codeTen = 0;
for( i = 0; i < document.docContainer.radio10.length; i++ )
{
if( document.docContainer.radio10[i].checked == true )
codeTen = document.docContainer.radio10[i].value;
}
document.docContainer.textarea12.value = codeOne + codeTwo + codeFour + codeTen;
}
function secondType(){
var codeSix = 0;
for( i = 0; i < document.docContainer.radio6.length; i++ )
{
if( document.docContainer.radio6[i].checked == true )
codeFour = document.docContainer.radio6[i].value;
}
var codeEleven = 0;
for( i = 0; i < document.docContainer.radio11.length; i++ )
{
if( document.docContainer.radio11[i].checked == true )
codeTen = document.docContainer.radio11[i].value;
}
document.docContainer.textarea12.value = codeOne + codeTwo + codeSix + codeEleven
}
if (document.docContainer.number2.value="1")
{
firstType();
}
else if (document.docContainer.number2.value="2")
{
secondType();
}
}
</script>
The code is working, problem is, even though I insert the value on textarea "number2" as "2", the code still generate firstType() result
Am Im using wrong if-statement or what and how do I go around about this?
Your code could be more fun to read, but Putting quotes around your numbers is causing javascript to treat them like text. Is this what you want to be doing?

Categories