Dec 14 2008
PDF Form Filling Program
My mother just called me on Skype…
Me: Guess what I am doing now.
Mom: I don’t know.
Me: It is an interesting programming problem.
Mom: Hmm, it probably has to do with stocks.
Me: Close. It’s tax related.
Mom: Oh, um, you need to compute your gains and losses, add them up, and apply the loss deduction from previous years if you have any.
Me: No. No computation involved.
Mom: What’s so hard about adding everything up.
Me: No. No computation involved. Think. What do I have to do once I have all the data?
Mom: Once you have all the data, aren’t you done?
Me: No. Once I have all my data, my data is in an excel file. What do I have to do afterwards?
Mom: What’s so hard about filling out the form? It’s easy.
Me: Easy? Uh, mom, do you know how many trades I have? You want me to enter all that manually?
So yes, it took me some time to write a Java program that takes as input all of my matched trades as a CSV file and fills out Schedule D-1. I believe the program is now complete. If I did not do this, I would have to pay TurboTax, GainsKeeper, or TradeAccountant to chug it. Regarding wash sales, I am not going to adjust my basis for wash sales unless it spills over to the previous or following year. I am also not going to fill out the first five lines of Schedule D. I am just going to go straight to the continuation sheet.
How did I write the program? I got the PDF library files from iText so that I could fill out PDF forms with a Java program. Since my input file is a CSV file and all the items are separated by commas or newlines, I can just parse that with Java and then pass on that data to the PDF code to fill out the PDF file.
iText gives you all of the libraries for free and even provides complete documentation. However, it charges for the tutorial e-Book. (How clever!) But if I gave you a tiny piece of code shown below, you looked at the iText API, and you are at least a half decent programmer, then you should be able to figure out how to write your own Java Schedule D-1 form filler.
public static void main(String[] args) throws IOException, DocumentException {
PdfReader reader = new PdfReader(“C:\\f1040sd1.pdf”);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(“C:\\output.pdf”));
AcroFields form = stamper.getAcroFields();
form.setField(“f1_001(0)”, “Your Name”);
stamper.close();}
Some things you should know about the form field names on Schedule D-1…
f1_001(0) Name
f1_002(0) First 3 digits of SSN
f1_003(0) Middle 2 digits of SSN
f1_004(0) Last 3 digits of SSN
f1_005(0) Description
f1_006(0) Date Acquired
f1_007(0) Date Sold
f1_008(0) Sales Price Dollars
f1_009(0) Sales Price Cents
f1_010(0) Cost Basis Dollars
f1_011(0) Cost Basis Cents
f1_012(0) Gain Dollars
f1_013(0) Gain Cents
There are 24 lines on the form and the last line is…
f1_212(0) Description
f1_213(0) Date Acquired
f1_214(0) Date Sold
f1_215(0) Sales Price Dollars
f1_216(0) Sales Price Cents
f1_217(0) Cost Basis Dollars
f1_218(0) Cost Basis Cents
f1_219(0) Gain Dollars
f1_220(0) Gain Cents
And the last four fields on the page are…
f1_221(0) Total Sales Price Dollars (For this page only)
f1_222(0) Total Sales Price Cents (For this page only)
f1_223(0) Total Gain Dollars (For this page only)
f1_224(0) Total Gain Cents (For this page only)
Field names that start with “f1″ are for short term capital gains. If you want to fill out the page with long term capital gains, then those field names start with “f2″ so you can just change your code to write to those fields.
Of course if you need multiple Schedule D-1 sheets, you’ll have to have a few loops that take 24 matched trades at a time, add everything up, fill out one Schedule D-1 form, and continue to do so until all matched trades are filled out on multiple Schedule D-1 forms.
If you want the code, shoot me an e-mail (info on contact page), and I’ll be more than happy to send it to you. But if you are at least a half-decent programmer, you should be able to figure this out on your own. Also, the way I have things set up may not be optimal for you, especially your input CSV file. If your broker is thinkorswim, then you can generate the same CSV file that I am using as my input.
[...] I should install some video codecs so that I can watch some movies. I might need Eclipse because sometimes I need to write some Java. Maybe I would want to install LaTeX, because I might need it to write my thesis. Sometimes I need [...]