Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Progress 11.07 KB | None | 0 0
  1.  
  2.  /*------------------------------------------------------------------------
  3.     File        : PDF Wrapper for PDFSuper.p
  4.     Purpose     :
  5.     Syntax      :
  6.     Description :
  7.     Author(s)   : connorrs
  8.     Created     : Fri Aug 19 10:27:02 EDT 2016
  9.     Notes       :
  10.   ----------------------------------------------------------------------*/
  11.  
  12. USING PROGRESS.Lang.*.
  13.  
  14. block-level ON ERROR UNDO, throw.
  15.  
  16. CLASS Business.PDF:
  17.   {Includes/PDFSuper.i}
  18.  
  19.   /* PDF properties */
  20.   DEF public property pdfStream      AS CHAR public GET. private SET.
  21.   DEF public property pdfDirectory   AS CHAR public GET. private SET.
  22.   DEF public property pdfFilename    AS CHAR public GET. private SET.
  23.   DEF public property pdfTitle       AS CHAR public GET. private SET.
  24.   DEF public property pdfOrientation AS CHAR public GET. private SET.
  25.   DEF public property pdfMaxPages    AS INT public GET. private SET.
  26.  
  27.   /* Logo properties */
  28.   DEF public property logoUsed AS LOG public GET. private SET.
  29.   DEF public property logoPath AS CHAR public GET. private SET.
  30.  
  31.   /* Current page info */
  32.   DEF public property pageNumber AS INT public GET. private SET.
  33.   DEF public property pageCount  AS INT public GET. private SET.
  34.   DEF public property pageHeight AS INT public GET. private SET.
  35.   DEF public property pageWidth  AS INT public GET. private SET.
  36.   DEF public property pageMax    AS INT public GET. private SET.
  37.  
  38.   /* Page positions */
  39.   DEF public property pageX AS INT public GET. private SET.
  40.   DEF public property pageY AS INT public GET. private SET.
  41.  
  42.   /* Report properties */
  43.   DEF public property headerLine     AS CHAR public GET. private SET.
  44.   DEF public property headerFont     AS CHAR public GET. private SET.
  45.   DEF public property headerFontSize AS INT public GET. private SET.
  46.  
  47.   DEF public property reportOrganization AS CHAR public GET. private SET.
  48.   DEF public property reportPrivacyText  AS CHAR public GET. private SET.
  49.  
  50.   /* Font properties */
  51.   DEF public property fontType  AS CHAR public GET. private SET.
  52.   DEF public property fontSize  AS INT public GET. private SET.
  53.   DEF public property fontColor AS INT public GET. private SET.
  54.  
  55.   CONSTRUCTOR PDF(pdfDirectory AS CHAR, pdfFileName AS CHAR, pdfTitle AS CHAR, pdfOrientation AS CHAR):
  56.     ASSIGN
  57.       THIS-OBJECT:pdfStream = "spdf"
  58.       THIS-OBJECT:pdfDirectory   = pdfDirectory
  59.       THIS-OBJECT:pdfFileName    = pdfFileName
  60.       THIS-OBJECT:pdfTitle       = pdfTitle
  61.       THIS-OBJECT:pdfOrientation = pdfOrientation
  62.     .
  63.  
  64.     /* Set page height and width. TODO: DOUBLE CHECK THESE NUMBERS */
  65.     IF pdfOrientation EQ "Portrait" THEN ASSIGN
  66.       THIS-OBJECT:pageHeight = 792
  67.       THIS-OBJECT:pageWidth  = 612
  68.     .
  69.     ELSE IF pdfOrientation EQ "Landscape" THEN ASSIGN
  70.       THIS-OBJECT:pageHeight = 612
  71.       THIS-OBJECT:pageWidth  = 792
  72.     .
  73.     ELSE ASSIGN
  74.       THIS-OBJECT:pdfOrientation = "Portrait"
  75.       THIS-OBJECT:pageHeight = 792
  76.       THIS-OBJECT:pageWidth  = 612
  77.     .
  78.  
  79.     /* Get the report defaults */
  80.     RUN Business/GetReportDefaults.p(OUTPUT logoUsed, OUTPUT logoPath, OUTPUT pageMax, OUTPUT pdfTitle, OUTPUT reportOrganization, OUTPUT reportPrivacyText).
  81.   END CONSTRUCTOR.
  82.  
  83.   /**
  84.    * Initializes the PDF and sets various internal PDF properties
  85.    */
  86.   METHOD public void StartPDF():
  87.     RUN PDFStart(
  88.       THIS-OBJECT:pdfDirectory,
  89.       THIS-OBJECT:pdfFilename,
  90.       THIS-OBJECT:pdfOrientation,
  91.       "letter",
  92.       "",
  93.       THIS-OBJECT:pdfTitle,
  94.       "",
  95.       "helvetica",
  96.       9.0,
  97.       "",
  98.       THIS-OBJECT:headerLine,
  99.       "helvetica-bold",
  100.       "9.0",
  101.       "",
  102.       "",
  103.       THIS-OBJECT:pdfTitle,
  104.       ""
  105.     ).
  106.     /* Set page properties */
  107.     ASSIGN
  108.       THIS-OBJECT:pageNumber = 1
  109.       THIS-OBJECT:pageCount = 1
  110.     .
  111.   END METHOD.
  112.  
  113.   /**
  114.    * Creates an entry in the chr(30) delimited list of header column values and position
  115.    *
  116.    * @param char columnText: The label of the column to be added
  117.    * @param char columnPos: The position of the column.
  118.    */
  119.   METHOD public void AddHeaderColumn(columnText AS CHAR, columnPos AS INT):
  120.     /* If the header line is not blank, add another delimiter. Without this, the list will go out of range */
  121.     IF THIS-OBJECT:headerLine NE "" THEN THIS-OBJECT:headerLine = THIS-OBJECT:headerLine + CHR(30).
  122.     THIS-OBJECT:headerLine = THIS-OBJECT:headerLine + columnText + CHR(30) + STRING(columnPos).  
  123.   END METHOD.
  124.  
  125.   METHOD public void SetHeader(headerLine AS CHAR):
  126.     THIS-OBJECT:headerLine = headerLine.
  127.     /* PDFSUPER CALL - sets sv-header */
  128.     RUN reset-header(THIS-OBJECT:headerLine).
  129.   END METHOD.
  130.  
  131.   METHOD public void PrintHeader():
  132.     RUN prt-header(THIS-OBJECT:headerLine, THIS-OBJECT:fontType, THIS-OBJECT:fontSize, THIS-OBJECT:fontColor).
  133.   END METHOD.
  134.  
  135.   /* Print text at current y position with no cutoff */
  136.   METHOD public void PrintText(printedText AS CHAR, x1 AS INT):
  137.     RUN pdf_text_xy(pdfStream, printedText, x1, pdf_textY(pdfStream)). /* TODO: SET THIS TO pageY */
  138.   END METHOD.
  139.  
  140.   /* Print text at current y position with no cutoff - this overloaded function takes an integer and prints it as a char */
  141.   METHOD public void PrintText(printedText AS INT, x1 AS INT):
  142.     RUN pdf_text_xy(pdfStream, STRING(printedText), x1, pdf_textY(pdfStream)). /* TODO: SET THIS TO pageY */
  143.   END METHOD.
  144.  
  145.   /**
  146.    * Skips a number of lines, and pages if necessary, given the current font size and weight.
  147.    *
  148.    * @param int lines: the number of lines to be skipped.
  149.    */
  150.   METHOD public void NextLine(lines AS INT):
  151.     DEF VAR newPageNeeded AS LOG NO-UNDO INIT no.
  152.  
  153.     RUN next-line-class(lines, "", OUTPUT newPageNeeded).
  154.     /* If on last page, create a new page */
  155.     IF newPageNeeded AND THIS-OBJECT:pageNumber EQ THIS-OBJECT:pageCount THEN DO:
  156.       _newPage().
  157.     END.
  158.     /* Goes to the next page, does not page */
  159.     ELSE IF newPageNeeded AND THIS-OBJECT:pageNumber LT THIS-OBJECT:pageCount THEN DO:
  160.       THIS-OBJECT:pageNumber = THIS-OBJECT:pageNumber + 1.
  161.       _setPage(THIS-OBJECT:pageNumber).
  162.     END.
  163.     /* Else does nothing */
  164.   END METHOD.
  165.  
  166.   /**
  167.    * Sets the current y position of the stream, from a top-down perspective (top = 0, bottom = pageHeight)
  168.    */
  169.   METHOD public void SetY(y1 AS INT):
  170.     /* Make sure the page height isn't set beyond the page limits */
  171.     IF y1 LE THIS-OBJECT:pageHeight THEN DO:
  172.       RUN sety(y1).
  173.     END.
  174.     ELSE DO:
  175.       MESSAGE "PDF Set Error: Y has been set beyond the page limits in class method SetY with a position of " + STRING(y1).
  176.     END.
  177.   END METHOD.
  178.  
  179.   /**
  180.    * Returns the current y position of the stream, from a top-down perspective (top = 0, bottom = pageHeight)
  181.    */
  182.   METHOD public INT GetY():
  183.     RETURN THIS-OBJECT:pageHeight - pdf_textY(THIS-OBJECT:pdfStream).
  184.   END METHOD.
  185.  
  186.   /**
  187.    * Creates a new page. Calls the internal new page function
  188.    */
  189.   METHOD public void NewPage():
  190.     _newPage().
  191.   END METHOD.
  192.  
  193.   /**
  194.    * Checks to see if the given number of lines will fit on the current page, given the current font size and weight.
  195.    *
  196.    * @param int lines: The number of lines to be checked.
  197.    * @returns log: If yes, then a new page is required.
  198.    */
  199.   METHOD public LOG RequiresNewPage(lines AS INT):
  200.     DEF VAR newPage AS CHAR NO-UNDO.
  201.    
  202.     newPage = pdf_ckpage2(pdfStream, lines, ""). /* Blank is for color - check to see what it does eventually */
  203.     IF newPage EQ "yes" THEN RETURN yes.
  204.     ELSE IF newPage EQ "no" THEN RETURN no.
  205.     ELSE RETURN no.
  206.   END METHOD.
  207.  
  208.   /**
  209.    * Checks to see if the given number of lines will fit on the current page, given the current font size and weight. If a new page is needed, creates one autmatically
  210.    *
  211.    * @param int lines: The number of lines to be checked.
  212.    */
  213.   METHOD public void CheckPage(lines AS INT):
  214.     DEF VAR newPage AS CHAR NO-UNDO.
  215.    
  216.     newPage = pdf_ckpage(pdfStream, lines, ""). /* Blank is for color - check to see what it does eventually */
  217.     IF newPage EQ "new-page" THEN DO:
  218.       _newPage().
  219.     END.
  220.   END METHOD.
  221.  
  222.   /**
  223.    * Set the page number.
  224.    *
  225.    * @param int pageNum: The page number to set to.
  226.    */
  227.   METHOD public void SetPage(pageNum AS INT):
  228.     _setPage(pageNum).
  229.   END METHOD.
  230.  
  231.   /**
  232.    * Prints a straight line with the given coordinates, with a set line weight.
  233.    *
  234.    * @param int x1
  235.    * @param int x2
  236.    * @param int y1
  237.    * @param int y2
  238.    */
  239.   METHOD public void PrintLine(x1 AS INT, x2 AS INT, y1 AS INT, y2 AS INT):
  240.     RUN pdf_line(pdfStream, x1, y1, x2, y2, 1).
  241.   END METHOD.
  242.  
  243.   /**
  244.    * Prints a straight line with the given coordinates, with a custom line weight.
  245.    *
  246.    * @param int x1
  247.    * @param int x2
  248.    * @param int y1
  249.    * @param int y2
  250.    * @param int lineWeight
  251.    */
  252.   METHOD public void PrintLine(x1 AS INT, x2 AS INT, y1 AS INT, y2 AS INT, lineWeight AS INT):
  253.     RUN pdf_line(pdfStream, x1, y1, x2, y2, lineWeight).
  254.   END METHOD.
  255.  
  256.   /**
  257.    * Sets the current font type
  258.    *
  259.    * @param char fontType
  260.    */
  261.   METHOD public void SetFont(fontType AS CHAR):
  262.     RUN pdf_set_font(THIS-OBJECT:pdfStream, fontType, THIS-OBJECT:fontSize).
  263.   END METHOD.
  264.  
  265.   /**
  266.    * Sets the current font type and font size
  267.    *
  268.    * @param char fontType
  269.    * @param int fontSize
  270.    */
  271.   METHOD public void SetFont(fontType AS CHAR, fontSize AS INT):
  272.     RUN pdf_set_font(THIS-OBJECT:pdfStream, fontType, fontSize).
  273.   END METHOD.
  274.  
  275.   /**
  276.    * Sets the current font type
  277.    *
  278.    * @param char fontType
  279.    */
  280.   METHOD public void SetFontSize(fontSize AS INT):
  281.     RUN pdf_set_font(THIS-OBJECT:pdfStream, THIS-OBJECT:fontType, fontSize).
  282.   END METHOD.
  283.  
  284.   /**
  285.    * Closes the PDF stream
  286.    */
  287.   METHOD public void ClosePDF():
  288.     RUN pdf_close(pdfStream).
  289.   END METHOD.
  290.  
  291.   /**
  292.    * Internal function for handling paging.
  293.    */
  294.   METHOD private void _newPage():
  295.     /* Make sure that the current page is the last page before running a new page */
  296.     IF THIS-OBJECT:pageNumber EQ THIS-OBJECT:pageCount THEN DO:
  297.       RUN new-page.
  298.       ASSIGN
  299.         THIS-OBJECT:pageNumber = THIS-OBJECT:pageNumber + 1.
  300.         THIS-OBJECT:pageCount = THIS-OBJECT:pageNumber.
  301.     END.
  302.     ELSE DO:
  303.       MESSAGE "PDF Paging Error! Paging must happen on the last page!".
  304.     END.
  305.   END METHOD.
  306.  
  307.   /**
  308.    * Internal function for handling setting of pages
  309.    * @param int pageNum: the page number to set the PDF to.
  310.    */
  311.   METHOD private void _setPage(pageNum AS INT):
  312.     /* Make sure the page exists */
  313.     IF pageNum GT 0 AND pageNum LE THIS-OBJECT:pageCount THEN DO:
  314.       RUN pdf_set_page(THIS-OBJECT:pdfStream, pageNum).
  315.       THIS-OBJECT:pageNumber = pageNum.
  316.     END.
  317.     ELSE IF pageNum LE 0 THEN DO:
  318.       MESSAGE "Paging error! Page number must be greater than 0. Page number set is " + STRING(pageNum).
  319.     END.
  320.     ELSE IF pageNum GT THIS-OBJECT:pageCount THEN DO:
  321.       MESSAGE "Paging error! Page number must be less than the current page count. Page number set is " + STRING(pageNum).
  322.     END.
  323.     ELSE DO:
  324.       MESSAGE "Paging error! Something went wrong".
  325.     END.
  326.   END METHOD.
  327. END CLASS.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement