Print Page | Close Window

How to Flatten a PDF's form fields

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=838
Printed Date: 22 Nov 24 at 6:02PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to Flatten a PDF's form fields
Posted By: jabaltie
Subject: How to Flatten a PDF's form fields
Date Posted: 19 Dec 07 at 7:39AM
I have a PDF with form fields that have been filled already.

It looks like this :

http://www.DES.online.unimep.br/au/pub/flatten.pdf

I only need a tool to flatten it's form fields and save back the resulting PDF, with form field content OK, of course.

Because when I flatten from my original application (using QuickPDF to do it), I have sometimes damaged PDFs generated (see my other postings ....).

So, my plan is to use QuickPDF to generate the PDF and then, flatten it by using something else OUTSIDE my original application. It could be even QuickPDF but it would have to be OUTSIDE the original application.

I have used PDFTK but it takes way, way too long to do it. About 30 seconds per PDF (on a PDF with 120 form fields).

I also tried to use QuickPDF itself, but it wont work. The script below generates an empty (blank) PDF....

Any other ideas ?


Here's my script :

DIM OBJARGS
DIM OBJQPDF
DIM ARQPDF
DIM LNI
DIM LNT
DIM FORMFIELDNAME

SET OBJARGS=WScript.Arguments

ARQPDF=OBJARGS(0)

SET OBJQPDF=WSCRIPT.CREATEOBJECT("ISED.QUICKPDF")

IF  NOT OBJQPDF.UNLOCKKEY("yourkey") = 1 THEN
    WSCRIPT.ECHO "COULD NOT UNLOCKKEY"
    WSCRIPT.QUIT(1)
END IF

IF  OBJQPDF.LOADFROMFILE(ARQPDF) = 0 THEN
    WSCRIPT.ECHO "COULD NOT LOAD TEMPLATE"
    WSCRIPT.QUIT(1)
END IF

LNT=OBJQPDF.FORMFIELDCOUNT()

FOR LNI=1 TO LNT
  IF  NOT OBJQPDF.FLATTENFORMFIELD(1) = 1 THEN
      FORMFIELDNAME=OBJQPDF.GETFORMFIELDTITLE(1)
      WSCRIPT.ECHO "ERROR WHILE FLATTENING THIS FORM FIELD '" + FORMFIELDNAME + "' ON " + ARQPDF,LNI
      WSCRIPT.QUIT(1)
  END IF
NEXT

IF  OBJQPDF.SaveToFile("C:\TEMP\TEST.PDF") = 0 THEN
    WSCRIPT.ECHO "COULD NOT SAVE TEMPLATE"
    WSCRIPT.QUIT(1)
END IF



Replies:
Posted By: chicks
Date Posted: 19 Dec 07 at 3:27PM


Try this:

DIM OBJARGS
DIM OBJQPDF
DIM ARQPDF
DIM LNI
DIM LNT
DIM FORMFIELDNAME

SET OBJARGS=WScript.Arguments

ARQPDF=OBJARGS(0)

SET OBJQPDF=WSCRIPT.CREATEOBJECT("ISED.QUICKPDF")

IF NOT OBJQPDF.UNLOCKKEY("yourkey") = 1 THEN
    WSCRIPT.ECHO "COULD NOT UNLOCKKEY"
    WSCRIPT.QUIT(1)
END IF

IF OBJQPDF.LOADFROMFILE(ARQPDF) = 0 THEN
    WSCRIPT.ECHO "COULD NOT LOAD TEMPLATE"
    WSCRIPT.QUIT(1)
END IF

LNT=OBJQPDF.FORMFIELDCOUNT()

FOR LNI=1 TO LNT
'-- Set form field font, size and color
ObjQPDF.SetFormFieldStandardFont 1, 8
ObjQPDF.SetFormFieldTextSize 1, 10.0
ObjQPDF.SetFormFieldColor 1, 0.0, 0.0, 0.0

IF NOT OBJQPDF.FLATTENFORMFIELD(1) = 1 THEN
      FORMFIELDNAME=OBJQPDF.GETFORMFIELDTITLE(1)
      WSCRIPT.ECHO "ERROR WHILE FLATTENING THIS FORM FIELD '" + FORMFIELDNAME + "' ON " + ARQPDF,LNI
      WSCRIPT.QUIT(1)
END IF
NEXT

IF OBJQPDF.SaveToFile("C:\TEMP\TEST.PDF") = 0 THEN
    WSCRIPT.ECHO "COULD NOT SAVE TEMPLATE"
    WSCRIPT.QUIT(1)
END IF

BTW, I was getting random garbage with the current version of QuickPDF (ActiveX). It goes away when I revert to version 5.14. Suspect a memory leak somewhere.

Also, pdftk actually does this faster on my PC, probably because there's no vbscript engine startup involved. Typically less than 50 milliseconds.
   Hi!I've replaced only the unlock-key ;-)Best regards,Ingo

Thanks, Ingo, but it was the one you published that expires at the end of this month


Posted By: jabaltie
Date Posted: 20 Dec 07 at 6:28AM
Hi !

I was very enthusiastic with your hint about the version. Then I reverted to 5.14. Have re-downloaded 5.14 but libraryversion actually prints 5.11.

Anyway, the problem persists.

Please take a copy of a PDF with garbage from here :

http://www.DES.online.unimep.br/au/pub/wrong2.pdf

Take a look after the word "Piracicaba".

You can see what I'm talking about....

What happened is that a field content was replaced by another.

Notice that the problem is TOTALLY RANDOM. I ran it again, same data, same version, same EXEcutable, and it runs OK.

Get a OK one from here :

http://www.DES.online.unimep.br/au/pub/ok1.pdf

Again, the problem is TOTALLY random.

Sometimes I run the EXE and it happens only at the first PDF generated. And it doesnt happen anymore. Other times I run the EXE and ALL of the PDFs are wrong. Sometimes the THIRD is wrong . And so forth.

I'm desperate. Dont know what else I can do.

Yes, data is OK.

I have adapted my software to generate a FDF file and run OUTSIDE of it, PDFTK. When a wrong PDF is generated by QUICKPDF, a OK one is generated with the same FDF by PDFTK.

The problem with PDFTK is that it is too, too, slow. 30 seconds per PDF. Impossible.

Any help ?




Posted By: chicks
Date Posted: 20 Dec 07 at 12:52PM
> The problem with PDFTK is that it is too, too, slow. 30 seconds per PDF. Impossible.

Try another PC. I'm getting < 50 milliseconds on my laptop, which isn't the fastest PC around.

I have some much older versions of QuickPDF somewhere. I'll try them to see if this (random garbage) has been a problem in the past when I can find them...


Posted By: jabaltie
Date Posted: 20 Dec 07 at 1:13PM
My strongest hint is regarded to the flattening function.

Because when I generate the PDFs without it, it works. There's no garbage....








Posted By: jabaltie
Date Posted: 20 Dec 07 at 1:21PM
As for the speed of PDFTK, try it there at your side...

This kit reproduces it all :

http://www.DES.online.unimep.br/au/pub/pdftk.zip

Thanks !


Posted By: chicks
Date Posted: 20 Dec 07 at 2:55PM
I have house guests for a couple of days, so won't be able to look at this 'til the weekend. Just a thought, though. I've never seen this problem before, so I wonder if it might be your templates (or whatever you are using to generate them) that are the root of the problem. You might want to test using one of the standard IRS forms, such as the http://www.irs.gov/pub/irs-pdf/f1040ez.pdf - 1040ez .


Posted By: jabaltie
Date Posted: 21 Dec 07 at 4:59AM
I understand your issue. I suspected of that before.

As a matter of fact, I'm using Open Office to generate the templates. It could be that, I guess.

But, on the other hand, consider this :

When I generate the PDF from QuickPDF and DO NOT flatten the fields, I have no mess.

Then, by using an EXTERNAL software, such as PDFTK, I flatten THE SAME PDF and I'll have a perfect PDF.

So, my conclusion : flattenformfield function is messy. When I use it, I'll have garbage. When I do not use it, I'll have no problems, although I'll need to flatten the form fields by another way (PDFTK in the case).

But I'll set up some time to make a test with the IRS form.

I'll be back only on January 3rd of 2008.

Thanks !




Posted By: marian_pascalau
Date Posted: 21 Dec 07 at 6:18PM
I will try to check this problem as well. As much as I know QuickPDF works quite well for that what you are expecting.
 


Posted By: jabaltie
Date Posted: 22 Dec 07 at 6:46AM
Thank you ! On the other hand, I'll try to design the template using Adobe Professional.

Thank you so much for your cooperation !


Posted By: chicks
Date Posted: 27 Dec 07 at 10:33PM
OK, your template definitely has issues. When I try to fill and flatten it with iText, it results in a blank PDF. iText is pretty much the standard PDF library available, so it shouldn't have problems with a conforming PDF.

I ran your template through pdftk to fix it (probably adds the appearance streams), so it now works fine with iText. Renamed it to "fixed.pdf".

I put together a simple java demo to create 99 PDFs using the template from 99 FDFs. It's compiled to .exe, so no java runtime is required. Takes exactly 30 seconds to create the 99 filled/flattened PDFs on my very slow desktop, should be much faster on a modern PC. You can download the test from my ftp server:

ftp://ftp.chicksolutions.com

user: anonymous@chicksolutions.com
pwd: (none)
file: FdfMulti.tgz

It's a gzipped tar, which can be opened and extracted using WinZip. Unzip, then run from the command prompt:

FdfMulti fixed.pdf fdf\*.fdf

This will fill the "fixed.pdf" with the 99 .fdf files in the fdf directory, outputting 99 PDFs to the fdf directory, each named the same as the corresponding fdf file.

Here's the java source code, also included in the .tgz:. Pretty basic stuff. Note that java (nicely) expands wildcards, so all the fdf's in the fdf directory come in as separate args given the "fdf\*.fdf" wildcard:

import com.lowagie.text.pdf.*;
import java.io.FileOutputStream;

public class FdfMulti {
    
    public static void usage() {
        System.out.println("FDF Multi merging and flattening");
        System.out.println("usage: FdfMulti template FDF File(s)");
    }
    
    public static void main(String[] args) {
        if (args.length < 2) {
            usage();
            return;
        }
       
        try {
         String template = args[0];
          for(int i = 1; i < args.length; i++) {
              String in_fdf = args;
              int j = in_fdf.lastIndexOf(".fdf");
              if(j <= 0)
               continue;

              String out_pdf = in_fdf.substring(0, j) + ".pdf";
              System.out.println("Creating " + out_pdf + " using " + in_fdf);

                 PdfReader reader = new PdfReader(template);
                     PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(out_pdf));
                     if (in_fdf != null) {
                    FdfReader fdf = new FdfReader(in_fdf);
                    AcroFields form = stamp.getAcroFields();
                    form.setFields(fdf);
                     }

                    stamp.setFormFlattening(true);

                     stamp.close();
             }
          return;
            }
            catch (Exception e) {
          e.printStackTrace();
          return;
            }
    }
}




Posted By: jabaltie
Date Posted: 02 Jan 08 at 6:02AM
Hello Chicks!

Thank you so much 4 your help !

I tried to download from your ftp server but it seems it's out.

I'd like to try your fixed pdf template upon QuickPDF itself. Just to see what happens...

So, if you can turn on your ftp server or either send me your fixed template by email (jabaltie  'at' unimep 'dot' br ), I'd appreciate !

Thanks !


Posted By: jabaltie
Date Posted: 02 Jan 08 at 7:47AM
Ah, one more thing :

When you say you fixed my template by using PDFTK, what have you done ?

Have you ran it through this option : "Repair a PDF's Corrupted XREF Table and Stream Lengths (If Possible)" ?

Once more, thank you !



Posted By: chicks
Date Posted: 02 Jan 08 at 12:33PM
The ftp server is working fine, but can't be accessed from a browser :)

Here's an alternative link for the download:

http://www.esnips.com/doc/352e8128-80ac-4760-8caa-ee906e4fe579/FdfMulti - FdfMulti


Posted By: jabaltie
Date Posted: 02 Jan 08 at 1:06PM
Strange....

I tried to load your fixed pdf template upon my software but I get an empty one at the end.

Here's a test script :

OPTION EXPLICIT

DIM objQPDF,objTempPDF,docFinalPDF,docPDF2beMerged,lni,lnt,lcfieldname,lcfieldvalue

Set objQPDF = WScript.CreateObject("ISED.QUICKPDF")

WScript.Echo "UnlockKey ",objQPDF.unlockkey("yourkey")

WScript.Echo "newdocument",objQPDF.NewDocument()
WScript.Echo "doccount   ",objQPDF.DocumentCount()

'WScript.Echo "Load File ",objQPDF.LoadFromFile("R:\USR\CPD\DES\RPS\TEMPLATES\AG\AG284D.PDF")
WScript.Echo "Load File ",objQPDF.LoadFromFile("C:\TEMP\FIXED.PDF")

WScript.Echo "doccount   ",objQPDF.DocumentCount()

lnt=objQPDF.FORMFIELDCOUNT()
if  lnt <> 122 then
    WScript.Echo "invalid field count : ",lnt
    Wscript.Quit(1)
end if

for lni=1 to lnt
    lcfieldname=objQPDF.GetFormFieldTitle(lni)
    wscript.Echo "campo numero ",lni,lcfieldname
next

'wscript.quit

for lni=lnt to 1 step -1
    lcfieldname=objQPDF.GetFormFieldTitle(lni)
    if  lcfieldname = "TCOTOTALDECREDITOS" then
        lcfieldvalue= "22"
    end if
    if  lcfieldname = "TCOLENGENDAOBS" then
        lcfieldvalue= "LEGENDA:"
    end if
    if  lcfieldname = "TCOCAMPUS" then
        lcfieldvalue= "Taquaral - Piracicaba         "
    end if
    if  lcfieldname = "TCOCAMPUSDATA" then
        lcfieldvalue= "Piracicaba, 20 de  Dezembro de 2007"
    end if
    if  lcfieldname = "TCODTSUBS15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS15" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS14" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS13" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODISCIP12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS12" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS11" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS10" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS9" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS8" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCREDITO7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOTURMA7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOSEMESTRE7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCO54DISCIP7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOCOD7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOOBS7" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCODTSUBS6" then
        lcfieldvalue= "16/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO6" then
        lcfieldvalue= "6"
    end if
    if  lcfieldname = "TCOTURMA6" then
        lcfieldvalue= "04.2"
    end if
    if  lcfieldname = "TCOSEMESTRE6" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP6" then
        lcfieldvalue= "ESTAGIO III"
    end if
    if  lcfieldname = "TCOCOD6" then
        lcfieldvalue= "42879"
    end if
    if  lcfieldname = "TCOOBS6" then
        lcfieldvalue= "*"
    end if
    if  lcfieldname = "TCODTSUBS5" then
        lcfieldvalue= "12/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO5" then
        lcfieldvalue= "8"
    end if
    if  lcfieldname = "TCOTURMA5" then
        lcfieldvalue= "29.9"
    end if
    if  lcfieldname = "TCOSEMESTRE5" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP5" then
        lcfieldvalue= "MONOGRAFIA I"
    end if
    if  lcfieldname = "TCOCOD5" then
        lcfieldvalue= "31351"
    end if
    if  lcfieldname = "TCOOBS5" then
        lcfieldvalue= "E"
    end if
    if  lcfieldname = "TCODTSUBS4" then
        lcfieldvalue= "12/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO4" then
        lcfieldvalue= "2"
    end if
    if  lcfieldname = "TCOTURMA4" then
        lcfieldvalue= "02.6"
    end if
    if  lcfieldname = "TCOSEMESTRE4" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP4" then
        lcfieldvalue= "ETICA PROFISSIONAL"
    end if
    if  lcfieldname = "TCOCOD4" then
        lcfieldvalue= "31344"
    end if
    if  lcfieldname = "TCOOBS4" then
        lcfieldvalue= "*"
    end if
    if  lcfieldname = "TCODTSUBS3" then
        lcfieldvalue= "12/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO3" then
        lcfieldvalue= "2"
    end if
    if  lcfieldname = "TCOTURMA3" then
        lcfieldvalue= "02.6"
    end if
    if  lcfieldname = "TCOSEMESTRE3" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP3" then
        lcfieldvalue= "ESTUDO DE PROBLEMAS BRASILEIROS II"
    end if
    if  lcfieldname = "TCOCOD3" then
        lcfieldvalue= "31336"
    end if
    if  lcfieldname = "TCOOBS3" then
        lcfieldvalue= "*"
    end if
    if  lcfieldname = "TCODTSUBS2" then
        lcfieldvalue= "12/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO2" then
        lcfieldvalue= "2"
    end if
    if  lcfieldname = "TCOTURMA2" then
        lcfieldvalue= "02.6"
    end if
    if  lcfieldname = "TCOSEMESTRE2" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP2" then
        lcfieldvalue= "DIREITO FINANCEIRO E TRIBUTARIO III"
    end if
    if  lcfieldname = "TCOCOD2" then
        lcfieldvalue= "31328"
    end if
    if  lcfieldname = "TCOOBS2" then
        lcfieldvalue= "*"
    end if
    if  lcfieldname = "TCODTSUBS1" then
        lcfieldvalue= "12/07/2007"
    end if
    if  lcfieldname = "TCOCREDITO1" then
        lcfieldvalue= "2"
    end if
    if  lcfieldname = "TCOTURMA1" then
        lcfieldvalue= "02.6"
    end if
    if  lcfieldname = "TCOSEMESTRE1" then
        lcfieldvalue= "09"
    end if
    if  lcfieldname = "TCO54DISCIP1" then
        lcfieldvalue= "DIREITO ADMINISTRATIVO II"
    end if
    if  lcfieldname = "TCOCOD1" then
        lcfieldvalue= "31310"
    end if
    if  lcfieldname = "TCOOBS1" then
        lcfieldvalue= "*"
    end if
    if  lcfieldname = "TCOINFOCOMP5" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOINFOCOMP4" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOINFOCOMP3" then
        lcfieldvalue= ""
    end if
    if  lcfieldname = "TCOINFOCOMP2" then
        lcfieldvalue= "E - VAGA AINDA NAO DEFERIDA. AGUARDE O RESULTADO DO DEFERIMENTO DA VAGA."
    end if
    if  lcfieldname = "TCOINFOCOMP1" then
        lcfieldvalue= "* - VAGA GARANTIDA NA TURMA."
    end if
    if  lcfieldname = "TCOANO" then
        lcfieldvalue= "2007"
    end if
    if  lcfieldname = "TCOPERIODOLET" then
        lcfieldvalue= "segundo "
    end if
    if  lcfieldname = "TCOTURNO" then
        lcfieldvalue= "diurno  "
    end if
    if  lcfieldname = "TCOCURSO" then
        lcfieldvalue= "DIREITO *************************************************************************************************"
    end if
    if  lcfieldname = "TCOSEMESMAT" then
        lcfieldvalue= "nono      "
    end if
    if  lcfieldname = "TCOMATRICULSEXO" then
        lcfieldvalue= "matriculada"
    end if
    if  lcfieldname = "TCORA" then
        lcfieldvalue= "03.7675-6"
    end if
    if  lcfieldname = "TCONOMEALUN" then
        lcfieldvalue= "ANA BEATRIZ VANELLI PASSARELLI*************************"
    end if
'    WScript.Echo "Field #",lni," from  A ",lcfieldname
    CALL objQPDF.SetFormFieldValue(lni,lcfieldvalue)
    objQPDF.FlattenFormfield(lni)
next


WScript.Echo "delete ",objQPDF.PageCount()
WScript.Echo "page # ",objQPDF.DeletePages(1,1)


WScript.Echo "save",objQPDF.SaveToFile("C:\TEMP\TEST.PDF")


If only I had a GOOD TEMPLATE, then I could test it upon my environment to see if the problem is on the template (very likely).

Thanks again !





Posted By: chicks
Date Posted: 02 Jan 08 at 1:31PM
I didn't test it with QuickPDF, but with iText, where it works fine. However, add this line, and it works fine with QuickPDF too:
...
ObjQPDF.SetFormFieldStandardFont lni, 8
    CALL objQPDF.SetFormFieldValue(lni,lcfieldvalue)
    objQPDF.FlattenFormfield(lni)
...


Posted By: jabaltie
Date Posted: 02 Jan 08 at 1:54PM
Now your template works. It's ugly but it works.

However, the field exchange problem persists.

I generated 6 PDF (same data, same software, same circumstances) and they were OK.

On the seventh, I had a data exchange problem.

Take a look :

http://www.des.online.unimep.br/au/pub/wrong7.pdf

On the column "Semestre" (Semester), one of them has the value "30748", which is the value of a preceding field. It should have the value "06" instead.

For me this is a QuickPDF bug....


Posted By: chicks
Date Posted: 02 Jan 08 at 2:08PM
Well, your best alternative is to avoid the use of form fields. Save the x, y, w, h and any other pertinent attributes for each field to an attribute file, then recreate your template without the fields. Then use the attributes from the attribute file along with DrawTextBox() to write your dynamic data to the template.


Posted By: jabaltie
Date Posted: 03 Jan 08 at 5:19AM
I understood your approach.

But AFAIK, there's no way to retrieve x and y from a form field. There's no function for that... At least not on the Form Fields group of functions.

If I had that, it would not be difficult to do it cause I already have an array of the templates and, within that array, I already have also a form fields list subarray. I could add those properties to that subarray.

So, from where can I get x, y width and height ?


Posted By: Ingo
Date Posted: 03 Jan 08 at 7:15AM
Hi!
You've told that the forms are always the same - so you can design / detect the positions by yourself. A normal form has 595 pixels width and 842 pixels height.
Best regards,
Ingo



Posted By: jabaltie
Date Posted: 03 Jan 08 at 7:20AM
Sorry. I can't get your point.

Say I have this template :

http://www.DES.online.unimep.br/au/pub/temp.pdf

I already know how to detect it's two fields names. But how to detect their corresponding x and y positions. Also other field properties such as bold.

How can I do that by using QuickPDF ?



Posted By: jabaltie
Date Posted: 03 Jan 08 at 7:41AM
Ah, another thing :

I could use an external tool to get those attributes. Cause usually my templates are loaded only AT THE START of the execution.

So, I could use PDFTK for instance, to retrieve those attributes.

Then, once I had them at hand, I'd use them by QuickPDF to generate my PDFs.

But PDFTK does not seem to dump those attributes as well.

It only has the dump_data_file option that does not show x,y, w and h....

Nor CutePDFSDK does it too.

Any other ideas ?




Posted By: chicks
Date Posted: 03 Jan 08 at 10:59AM
Some of the field attributes:

GetFormFieldBound(idx, 0) - Left
GetFormFieldBound(idx, 1) - Top
GetFormFieldBound(idx, 2) - Width
GetFormFieldBound(idx, 3) - Height
GetFormFieldFontName(idx)
GetFormFieldTextSize(idx)
GetFormFieldPage(idx)
GetFormFieldVisible(idx)
GetFormFieldPrintable(idx)
GetFormFieldType(idx)
GetFormFieldSubCount(idx)
GetFormFieldAlignment(idx)
GetFormFieldRotation(idx)
GetFormFieldColor(idx,1) - Red
GetFormFieldColor(idx,2) - Green
GetFormFieldColor(idx,3) - Blue
GetFormFieldMaxLen(idx)
GetFormFieldTabOrder(idx)

This post from a few years back has a QuickPDf / vbscript demo of this process:

http://forum.planetpdf.com/webboard/wbpx.dll/read?94629,3 - http://forum.planetpdf.com/webboard/wbpx.dll/read?94629,3


Posted By: jabaltie
Date Posted: 03 Jan 08 at 11:23AM
Ops. Sorry !

Saw all the attributes but never though that X,Y,W,H would be "bound" to the GETFORMFIELDBOUND function.

Now it's my turn to dig into the code.

I'm motivated. Looks like it'll work fine Smile

Once more, thank you so much !


Posted By: jabaltie
Date Posted: 03 Jan 08 at 2:52PM
I'm almost there !

But I'm stucked with a font issue...

The thing is : the form field has a font property. I have got it already.

So, on my array of form fields, I have their font names. For instance, on my template all of the form fields have "Helvetica" as their font name.

Later on, I'll need to use this font name ("Helvetica") to "drawtextbox".

How do I set it ?




Posted By: Ingo
Date Posted: 03 Jan 08 at 3:44PM
Hi!

With AddTrueTypeFont(...) you can add Helvetica to your document.
With SelectFont(...) you can select the font first and after it you can use DrawTextBox(...). I've read this in the reference guide ;-)

Best regards and good luck,
Ingo



Posted By: chicks
Date Posted: 03 Jan 08 at 5:00PM
Helvetica is a standard fort, no need to add TrueType version.

fontID=ObjQPDF.AddStandardFont(4)
ObjQPDF.SelectFont(fontID)

Only need to do this once at the beginning, unless you'll be changing fonts while writing.


Posted By: jabaltie
Date Posted: 03 Jan 08 at 5:07PM
The point is : I dont know which font the template will come up with.

I need to be able to respect the font of the form field ....




Posted By: chicks
Date Posted: 03 Jan 08 at 6:03PM
Look at the example I pointed to on PlanetPDF. It will select one of the 14 built-in fonts based on the form field's font name, or default to "Courier" if not matched. See the SetFont() and SelectFont() functions in the example vbscript.


Posted By: jabaltie
Date Posted: 04 Jan 08 at 8:01AM
I finally managed the fonts issue and replaced my logic as Ingo suggested above.

So, at the beginning, I get my template, loop across all of the form fields and grab their properties into an array of my own. Then I delete all of the form fields.

Later on, I get the field values and draw them all on my own, using that array which I built from the template.

After doing it all, this is the conclusion :

================================================
PROBLEM SOLVED ! THAT IS, THERE WAS NO MORE FIELD CONTENT EXCHANGE, as I posted on several other topics.
================================================

So, again, my conclusion is that FLATTENFORMFIELD is messed up.

Moreover, the time to generate a PDF from this template was dramatically reduced from 5.44 seconds to 0.25 seconds ! Amazing. Because that messy FLATTENFORMFIELD function takes way, way too long to run.

Anyway, now I have to improve this solution in order to deal with other font types and so on.

Will anyone finally recognize that function problem and fix it ?



Posted By: jabaltie
Date Posted: 04 Jan 08 at 11:11AM
Still have some minor problems....

There's a very subtle difference when filling a form field against drawtextbox : the vertical alignment is different...

The script below generates two PDF outputs, one by forms and the other by drawing. You can load them both and see how they're different.

Here's the code :

--------------------------------
OPTION EXPLICIT

DIM objQPDF,lni,lnt
DIM X,Y,W,H,A,S

Set objQPDF = WScript.CreateObject("ISED.QUICKPDF")

objQPDF.unlockkey("yourkeygoesintohere")

objQPDF.NewDocument()
objQPDF.DocumentCount()

objQPDF.LoadFromFile("TEST034.PDF")

' GET TEST034.PDF from http://www.DES.online.unimep.br/au/pub/test034.pdf

lni=3

CALL objQPDF.SetFormFieldValue(lni,"matriculado")

lnt=objQPDF.FORMFIELDCOUNT()
for lni=lnt to 1 step -1
  objQPDF.FlattenFormfield(lni)
next

objQPDF.SaveToFile("TEST034FORM.PDF")

objQPDF.LoadFromFile("TEST034.PDF")

lni=3

X=objQPDF.GetFormFieldBound(lni,0)
Y=objQPDF.GetFormFieldBound(lni,1)
W=objQPDF.GetFormFieldBound(lni,2)
H=objQPDF.GetFormFieldBound(lni,3)
A=objQPDF.GetFormFieldAlignment(lni)
S=objQPDF.GetFormFieldTextSize(lni)

lnt=objQPDF.FORMFIELDCOUNT()
for lni=lnt to 1 step -1
  objQPDF.DeleteFormField(lni)
next

objQPDF.SetTextAlign(A)
objQPDF.SetTextSize(S)

CALL objQPDF.DRAWTEXTBOX(X,Y,W,H,"matriculado",3)

objQPDF.SaveToFile("TEST034DRAW.PDF")

WScript.Echo "OK !"

--------------------

I tried to "rotate" the last DrawTextBox parameter but it wont solve.

I guess I must do some sort of calculation with the form field borders, isnt it ?

Once more, thank you !




Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk