0

I have postscript file of 100 lines. I'm trying to merge 2 pdfs in to the single one with the help of ghostscript. What I want to do is, merge both pdf into one and than apply my postscript code in following manner:

1) first 50 lines of the postscript file should be applied on only page 1 of the generated pdf

2) remaining 50 lines to be applied on the 2nd page of the generated pdf.

I have used following code to do that:

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf test.ps pdf_1.pdf pdf_2.pdf;

I have tried showpage option of postscript, but that insert a blank page in the generated output pdf and that's not my requirement.

Any one knows how can I do that ?

Smit
  • 1,559
  • 17
  • 38

3 Answers3

3

showpage is not a Ghostscript 'option' its a PostScript operator.

Because your input files are PDF this is a non-trivial task. The PDF interpreter will execute the system definition of showpage for every page in the input PDF file, overriding anything you put into PostScript.

Without trying this I believe what you need to do is create an EndPage procedure which takes different action depending on the page count (this is a PostScript programming technique).

Eg:

/DoPage1 {
    %% Your code goes in here
} def
/DoPage2 {
    %% Your code goes in here
}
<<
/EndPage {
    2 lt{
        1 eq {
            DoPage1
        }{
            DoPage2
        } ifelse
    }{
        pop
    } ifelse
}
>> setpagedevice

You will have to run this code before the 2 PDF files, eg:

gs .... setup.ps file1.pdf file2.pdf

luser droog
  • 18,988
  • 3
  • 53
  • 105
KenS
  • 30,202
  • 3
  • 34
  • 51
  • Ok, seems like it works for the given example.. Now say I have dynamic generated pdfs and of different page numbers. In that case what should be the code for the postscript ? I'm very beginner to postscript and has very less idea of that. – Smit Mar 20 '12 at 09:29
  • 2
    I don't know. How will you know which set of PostScript to attach to which page if you have a variable number of pages ? PostScript is a programming language so when you know what you want to do, you can program for it. – KenS Mar 21 '12 at 08:34
2

You can provide all the ps file names in single command, just next to each pdf output file name. That will generate the require PDFs those are having the PS code output and then you can merge them as per the requirement using any PDF library.

0

You can break down the PS code in to multiple iteration. Just create one pdf with one part of PS code and then generate another pdf with remaining part of PS. After doing that you can merge both PDF with the help of ghost script and get the desire output.

I know this is not a wise solution, but for the time you can move ahead with that.

Thanks

Smit
  • 1,559
  • 17
  • 38