Rukk
06-15-2009, 09:44 PM
Hello,
I'm writing a JavaScript function that will let me add text in a PDF file. So far, so good. I am able to open my PDF file, write in it, save it, print... the only problem is that I can't put my text where I want. Here's my function:
1.function modifPdf(src)
2.{
3. // Create ActiveX Object to manipulate PDF
4. var pdf = new ActiveXObject('AcroExch.PDDoc');
5.
6. // Variables
7. var pdfJS;
8. var annot;
9. var props;
10. var printParams;
11. var page;
12. var rectSize = new Array();
13.
14. // Open PDF
15. pdf.Open(src);
16.
17. page = pdf.AcquirePage(0);
18. page = page.GetSize()
19. rectSize[0] = 25;
20. rectSize[1] = page.y - 50;
21. rectSize[2] = page.x - 25;
22. rectSize[3] = page.y - 25;
23.
24. // Get the Javascript object of the pdf
25. pdfJS = pdf.GetJSObject();
26.
27. // Add the annot
28. // Annot is created but not positionned (rect not working?)
29. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: rectSize,author: 'SA',contents: 'Test'});
30. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: [25,742,1199,767],author: 'SA',contents: 'Test'});
31. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: new Array(25,742,1199,767), author: 'SA',contents: 'Test'});
32.
33. // Printing
34. printParams = pdfJS.getPrintParams();
35. printParams.interactive = -1;
36. printParams.firstPage = 0;
37. printParams.pageHandling = printParams.constants.handling.fit;
38. //pdfJS.print(printParams);
39. // Save on comp instead of printing for tests
40. pdf.Save(1, "C:/Tempo/test.pdf" );
41.
42. pdf.Close();
43.}
It creates the annot, but it positions it at 0,0 with a width & height of 0. BUT (which is strange) when I directly add one of the following lines directly in my PDF via Advanced -> Document Processing -> JavaScript Debugger... it works with the good position!!
this.addAnnot({page: 0,type: 'FreeText',rect: rectSize,author: 'SA',contents: 'Test'});
this.addAnnot({page: 0,type: 'FreeText',rect: [25,742,1199,767],author: 'SA',contents: 'Test'});
this.addAnnot({page: 0,type: 'FreeText',rect: new Array(25,742,1199,767), author: 'SA',contents: 'Test'});
Looks like JavaScript has trouble passing an array to my pdfJS object :/
Anyone knows how to solve this? Thanks!
I'm writing a JavaScript function that will let me add text in a PDF file. So far, so good. I am able to open my PDF file, write in it, save it, print... the only problem is that I can't put my text where I want. Here's my function:
1.function modifPdf(src)
2.{
3. // Create ActiveX Object to manipulate PDF
4. var pdf = new ActiveXObject('AcroExch.PDDoc');
5.
6. // Variables
7. var pdfJS;
8. var annot;
9. var props;
10. var printParams;
11. var page;
12. var rectSize = new Array();
13.
14. // Open PDF
15. pdf.Open(src);
16.
17. page = pdf.AcquirePage(0);
18. page = page.GetSize()
19. rectSize[0] = 25;
20. rectSize[1] = page.y - 50;
21. rectSize[2] = page.x - 25;
22. rectSize[3] = page.y - 25;
23.
24. // Get the Javascript object of the pdf
25. pdfJS = pdf.GetJSObject();
26.
27. // Add the annot
28. // Annot is created but not positionned (rect not working?)
29. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: rectSize,author: 'SA',contents: 'Test'});
30. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: [25,742,1199,767],author: 'SA',contents: 'Test'});
31. pdfJS.addAnnot({page: 0,type: 'FreeText',rect: new Array(25,742,1199,767), author: 'SA',contents: 'Test'});
32.
33. // Printing
34. printParams = pdfJS.getPrintParams();
35. printParams.interactive = -1;
36. printParams.firstPage = 0;
37. printParams.pageHandling = printParams.constants.handling.fit;
38. //pdfJS.print(printParams);
39. // Save on comp instead of printing for tests
40. pdf.Save(1, "C:/Tempo/test.pdf" );
41.
42. pdf.Close();
43.}
It creates the annot, but it positions it at 0,0 with a width & height of 0. BUT (which is strange) when I directly add one of the following lines directly in my PDF via Advanced -> Document Processing -> JavaScript Debugger... it works with the good position!!
this.addAnnot({page: 0,type: 'FreeText',rect: rectSize,author: 'SA',contents: 'Test'});
this.addAnnot({page: 0,type: 'FreeText',rect: [25,742,1199,767],author: 'SA',contents: 'Test'});
this.addAnnot({page: 0,type: 'FreeText',rect: new Array(25,742,1199,767), author: 'SA',contents: 'Test'});
Looks like JavaScript has trouble passing an array to my pdfJS object :/
Anyone knows how to solve this? Thanks!