PDF New Page Issues

Discussions related to network, internet and socket programming; also to serial, parallel, Bluetooth, USB etc.
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

PDF New Page Issues

Post by MattC »

Hi All (or whoever's reading - I don't seem to be getting much in the way of responses lately - hope I haven't offended everyone :? ),

I'm trying to work out why I have to create a new file for each individual page when I print to a PDF file. Here's some sample code:

Code: Select all

      PD_NOSELECTION        = &4
      PD_NOPAGENUMS         = &8
      PD_RETURNDC           = &100
      PD_DISABLEPRINTTOFILE = &80000
      PD_HIDEPRINTTOFILE    = &100000
      PD_NONETWORKBUTTON    = &200000

      PrintFlags% = PD_NONETWORKBUTTON + PD_HIDEPRINTTOFILE + PD_DISABLEPRINTTOFILE \
      \           + PD_RETURNDC + PD_NOPAGENUMS + PD_NOSELECTION

      ok% = FN_print_dlg_A(ps{}, PrintFlags%)

      VDU 2
      PRINT "This is page 1"
      VDU 1, 12
      PRINT "This is page 2"
      VDU 1, 12, 3
      END

      DEF FN_print_dlg_A(RETURN ps{}, flags%)
      LOCAL pd{}, ok%, dm%
      IF flags% = -1 THEN flags% = &400
      DIM ps{paper{orient%, size%, length%, width%, npax%, npay%}, \
      \      print{scale%, quality%}, pages{copies%, from%, to%}}
      DIM pd{lStructSize%, hwndOwner%, hDevMode%, hDevNames%, \
      \      hdc%, flags%, nFromPage{l&,h&}, nToPage{l&,h&}, \
      \      nMinPage{l&,h&}, nMaxPage{l&,h&}, nCopies{l&,h&}, \
      \      hInstance%, lCustData%, lpfnPrintHook%, lpfnSetupHook%, \
      \      lpPrintTemplateName%, lpSetupTemplateName%, \
      \      hPrintTemplate%, hSetupTemplate%}
      pd.lStructSize% = DIM(pd{})
      pd.hwndOwner% = @hwnd%
      pd.nMinPage.l& = 1
      pd.nMaxPage.h& = &FF: pd.nMaxPage.l& = &FF
      pd.flags% = flags%
      SYS "PrintDlg", pd{} TO ok%
      IF ok% = 0 THEN = FALSE
      IF flags% <> &400 THEN
        SYS "DeleteDC", @prthdc%
        @prthdc% = pd.hdc%
        *MARGINS 10,10,10,10
      ENDIF
      SYS "GlobalLock", pd.hDevMode% TO dm%
      ps.paper.orient%  = dm%?44           : REM orientation
      ps.paper.size%    = dm%?46           : REM page size number (e.g. 9=A4)
      ps.paper.length%  = dm%!48 AND &FFFF : REM page length in 10ths mm
      ps.paper.width%   = dm%!48 >>> 16    : REM page width in 10ths mm
      ps.print.scale%   = dm%!52 AND &FFFF : REM scale
      ps.print.quality% = dm%!56 >>> 16    : REM print quality (dpi)
      ps.pages.copies%  = dm%!52 >>> 16    : REM copies
      SYS "GlobalUnlock", pd.hDevMode%
      SYS "GetDeviceCaps", @prthdc%, &70 TO ps.paper.npax%
      SYS "GetDeviceCaps", @prthdc%, &71 TO ps.paper.npay%
      ps.pages.from%    = pd.nFromPage.h& * &100 + pd.nFromPage.l&
      ps.pages.to%      = pd.nToPage.h& * &100 + pd.nToPage.l&
      = TRUE
When running, the PDF printer is selected (usually 'Microsoft Print to PDF, but others have the same issue') and the 'Save' dialog box appears. The file is selected and the first page ("This is page 1") is sent to the PDF file. However, another 'Save' dialog box appears to save the next page, "This is page 2".

Why isn't this all in one file? How can I make it so?

Matt
DDRM

Re: PDF New Page Issues

Post by DDRM »

Hi Matt,

You certainly haven't offended me, at least! However, I also usually don't know the answers to your questions.... ;-)

... but in this case, the wiki seems to give you an answer:

https://www.bbcbasic.co.uk/wiki/doku.ph ... es&s[]=pdf

"If you use such a program to create a PDF file from your BBC BASIC program there is one small problem - each individual page will come out as a separate document! This happens because traditional BBC BASIC programs (e.g. as written for the BBC Micro or Acorn Archimedes) have no way of indicating the end of the document, only the end of a page. Therefore BBC BASIC for Windows closes the current 'document' at the end of each page and opens a new 'document' at the start of the next page. This isn't apparent when outputting to a real printer, but is when creating a PDF.

This is easily solved by replacing the code which you would normally use to indicate the end of a printed page (typically VDU 2,1,12,3) with the following code:

SYS "EndPage", @prthdc%
SYS "StartPage", @prthdc%
VDU 2,1,30,3

To close the document, after all the pages have been output, send a VDU 2,1,12,3 as usual. "

Hope that helps!

D
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: PDF New Page Issues

Post by MattC »

Thank for that. I obviously didn't look hard enough. I'm assuming this will also work just as well with paper printing, or I will have to work out how to distinguish between the two types of printer. Thanks for your help.

And it good that my questions aren't normally too easy. :D
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: PDF New Page Issues

Post by MattC »

With some little experimentation, it seem that the 'SYS "StartPage", @prthdc%' part is (at least, normally - or in my situation) redundant.

This basic code seems to work fine on both PDF and paper printers alike, which at least sorts my problem out:

Code: Select all

      [select paper/PDF printer]
      VDU 2
      [loop start]
          [page items printing code]
          SYS "EndPage", @prthdc%
          VDU 1, 30
      [loop end]
      VDU 1, 12
      VDU 3
Is there any situation in which the 'StartPage' command would be useful or even necessary? Neither the Wiki page nor the Windows Help explains reasons why, in this situation, it would be advantageous.

Either way, thanks for your help.

Matt