Print Page | Close Window

PDF: Why does text field start with a blank line?

Printed From: Foxit's Planet PDF Forum
Category: Lets Talk PDF
Forum Name: What's wrong with my PDF?
Forum Description: Having problems with a PDF document? Here's the place to post it.
URL: http://forums.planetpdf.com/forum_posts.asp?TID=4010
Printed Date: 15 Nov 2019 at 1:32pm
Software Version: Web Wiz Forums 11.10 - http://www.webwizforums.com


Topic: PDF: Why does text field start with a blank line?
Posted By: mmitchell_houston
Subject: PDF: Why does text field start with a blank line?
Date Posted: 30 Aug 2018 at 9:08pm
I have created a PDF form designed to help people enter URLs into a long list.
When the user enters a new URL, it will be appended to the bottom of the list.

I've got it working (stripped out the URL functionality for testing), but it keeps adding a blank line to the beginning of the list. I've tried all sorts of things, including an if statement searching for null value, and I think there's a null in the field, but that darned line break at the start of the list is very annoying.

  uploads/3597/MoveURL_Working_08-30c-2018.pdf - uploads/3597/MoveURL_Working_08-30c-2018.pdf

================================
Here's the code only if you're interested (I added comments to help you guys sort out what I'm up to).

//  Summary: User will enter a URL into the textbox
//  User will click a button and the URL will be moved down
//  to the big text area. If there is already text there,
//  the new URL will be appended to the end of the list.
//
//  NOTE: I will worry about formatting the link after I
//  get this first part working.

//  ----- Elements we are working with -----
//  btn01_Mkt_addURL Button that triggers this function
//  txt01_Mkt_url Textbox (small, on top)
//  btn01_Mkt_desc Button -- IGNORE FOR NOW
//  txt01_Mkt_desc Textbox -- IGNORE FOR NOW
//  btn01_Mkt_showDesc Button shows btn01_Mkt_desc & txt01_Mkt_desc -- IGNORE FOR NOW
//  btn01_Mkt_hideDesc Button hides btn01_Mkt_desc & txt01_Mkt_desc -- IGNORE FOR NOW
//  btn01_Mkt_popup IGNORE FOR NOW
//  txt01_Mkt_combinedText   Big text area at the bottom (supports RTF content)
// =====================================================================================
//  Declare variables
var oldText1;
var newURL1;

// Get value from the first (smaller) field
newURL1 = getField("txt01_Mkt_url").value;
// Get value from the second (bigger) field at the bottom
oldText1 = getField("txt01_Mkt_combinedText").value;


// Append the two together
var combinedText1 = oldText1 + "\n" + newURL1;

// Put the combined text into big text area
getField("txt01_Mkt_combinedText").value = combinedText1;

// Debugger
app.alert("Debugger\nnewURL1 = " + newURL1 + "\n\n" + "oldText1 = " + oldText1 + "\n\n" + "combinedText1 = " + combinedText1, 3);




Replies:
Posted By: BAlheit
Date Posted: 31 Aug 2018 at 6:52am
oldText1 is an empty string.

The command "oldText1 + "\n" + newURL1" adds a newline to the empty string. The result is a blank line.


Posted By: mmitchell_houston
Date Posted: 31 Aug 2018 at 6:08pm
Thanks. I originally had some error trapping to handle this, but cut it out because it didn't seem to help. Maybe there was something wrong with that code. I'll go back and check.


Posted By: mmitchell_houston
Date Posted: 31 Aug 2018 at 6:29pm
THANKS!
I think my first attempt at checking for an empty string didn't work, so I gave up on that route. Then it came down to me doing things in the wrong order.

I have updated my code (new additions in red):

//  Summary: User will enter a URL into the textbox
//  User will click a button and the URL will be moved down
//  to the big text area. If there is already text there,
//  the new URL will be appended to the end of the list.
//
//  NOTE: I will worry about formatting the link after I
//  get this first part working.

//  ----- Elements we are working with -----
//  btn01_Mkt_addURL Button that triggers this function
//  txt01_Mkt_url Textbox (small, on top)
//  btn01_Mkt_desc Button -- IGNORE FOR NOW
//  txt01_Mkt_desc Textbox -- IGNORE FOR NOW
//  btn01_Mkt_showDesc Button shows btn01_Mkt_desc & txt01_Mkt_desc -- IGNORE FOR NOW
//  btn01_Mkt_hideDesc Button hides btn01_Mkt_desc & txt01_Mkt_desc -- IGNORE FOR NOW
//  btn01_Mkt_popup IGNORE FOR NOW
//  txt01_Mkt_combinedText   Big text area at the bottom (supports RTF content)
// =====================================================================================
//  Declare variables
var oldText1;
var newURL1;

// Get value from the first (smaller) field
newURL1 = getField("txt01_Mkt_url").value;
// Get value from the second (bigger) field at the bottom
oldText1 = getField("txt01_Mkt_combinedText").value;

//  If this is not the first entry (in other words, oldText1
//  is not empty), this adds a new line before inserting the new URL
if (oldText1 != "") {
oldText1 = oldText1 + "\n";
}
if (oldText1 == "") {
oldText1 = "";
}


// Append the two together
var combinedText1 = oldText1 + newURL1;

// Put the combined text into big text area
getField("txt01_Mkt_combinedText").value = combinedText1;


// Debugger
// app.alert("Debugger\nnewURL1 = " + newURL1 + "\n\n" + "oldText1 = " + oldText1 + "\n\n" + "combinedText1 = " + combinedText1, 3);



Posted By: BAlheit
Date Posted: 01 Sep 2018 at 7:27am
You can use this:

var combinedText1;
if (oldText1 == "") combinedText1  = newURL1;
else combinedText1 = oldText1 + "\n" + newURL1;


Posted By: mmitchell_houston
Date Posted: 01 Sep 2018 at 9:03pm
Originally posted by BAlheit BAlheit wrote:

You can use this:
var combinedText1;
if (oldText1 == "") combinedText1  = newURL1;
else combinedText1 = oldText1 + "\n" + newURL1;

Thanks very much! I was pretty sure someone was going to suggest something like that (I know I would if I were reading this clunky code from the outside). Smile

Right now I'm a little rusty on my javascript (especially for PDFs – the bulk of my experience is in javascript for the web), so I like to write everything out in full the first time I create the code. This makes it easier for me to see/fix errors. After I know it's working, then I go back and streamline everything.

UNNECESSARY BACKGROUND:
I do this because, years ago, I spent a full day trying to track down an error in a long, complex if/else function and it wasn't until I broke each and every piece into a separate "if-test" that I discovered I had been looking for the error in the wrong place. Since then, my first pass at the code is purposefully clunky so I can test it at each-and-every step of the way. 

Thanks again!


Posted By: mmitchell_houston
Date Posted: 01 Sep 2018 at 9:13pm
BTW, Thanks again. I used your code and it works great!



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.10 - http://www.webwizforums.com
Copyright ©2001-2017 Web Wiz Ltd. - https://www.webwiz.net