private void drawOGHeader(
      Document document, UserShopForm sForm, String pImageName, boolean personal)
      throws DocumentException {

    // add the logo
    try {
      Image i = Image.getInstance(pImageName);
      float x = document.leftMargin();
      float y = document.getPageSize().getHeight() - i.getHeight() - 5;
      i.setAbsolutePosition(x, y);
      document.add(i);
    } catch (Exception e) {
      e.printStackTrace();
    }

    if (catalogOnly) {
      return;
    }

    // unfortuanately this means we are limited to PDF rendering only.
    // get the user's personal info
    PdfPTable personalInfo;
    if (personal) {
      personalInfo = getPersonalized(sForm);
    } else {
      personalInfo = getNonPersonalized(sForm);
    }

    if (null != sForm.getAppUser().getUserAccount()
        && null != sForm.getAppUser().getUserAccount().getSkuTag()) {
      String t = sForm.getAppUser().getUserAccount().getSkuTag().getValue();
      if (t != null && t.length() > 0) {
        mSkuTag = t;
      }
    }

    // Display the contact info
    PdfPTable contactInfo = new PdfPTable(2);
    contactInfo.setWidthPercentage(50);
    contactInfo.setWidths(sizes);
    contactInfo.getDefaultCell().setBorder(borderType);
    contactInfo.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
    String orderOnlineStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.orderOnLine:", null);
    contactInfo.addCell(makePhrase(orderOnlineStr + " ", smallHeading, false));
    contactInfo.addCell(
        makePhrase(
            sForm.getAppUser().getUserStore().getStorePrimaryWebAddress().getValue(),
            smallHeading,
            true));

    ContactUsInfo contact = null;
    if (sForm.getAppUser().getContactUsList().size() == 1) {
      contact = (ContactUsInfo) sForm.getAppUser().getContactUsList().get(0);
    }

    String orderFaxNumber = null, bscdesc = null;
    if (sForm.getAppUser().getSite().getBSC() != null
        && sForm.getAppUser().getSite().getBSC().getFaxNumber() != null
        && sForm.getAppUser().getSite().getBSC().getFaxNumber().getPhoneNum() != null) {
      orderFaxNumber = sForm.getAppUser().getSite().getBSC().getFaxNumber().getPhoneNum();
      bscdesc = sForm.getAppUser().getSite().getBSC().getBusEntityData().getLongDesc();
    }
    if (orderFaxNumber == null && contact != null) {
      orderFaxNumber = contact.getFax();
    }

    if (orderFaxNumber != null) {
      String faxOrderStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.faxOrder:", null);
      contactInfo.addCell(makePhrase(faxOrderStr + " ", smallHeading, false));
      contactInfo.addCell(makePhrase(orderFaxNumber, smallHeading, true));
    }

    if (null != bscdesc && bscdesc.length() > 0) {
      // if there is a BSC descripton set, then show
      // that information.
      PdfPCell cell = new PdfPCell(new Paragraph(bscdesc, smallHeading));
      cell.disableBorderSide(PdfPCell.LEFT);
      cell.disableBorderSide(PdfPCell.RIGHT);
      cell.disableBorderSide(PdfPCell.TOP);
      cell.disableBorderSide(PdfPCell.BOTTOM);
      cell.setColspan(2);
      cell.setPaddingTop(6);
      contactInfo.addCell(cell);
    } else {

      // otherwise show the standard contact us information
      // request for dmsi - dhl for eric.  durval 11/1/2005.

      if (contact != null) {
        contactInfo.addCell(makePhrase(" ", smallHeading, false));
        contactInfo.addCell(makePhrase(" ", smallHeading, true));
        String contactUsStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.contactUs", null);
        contactInfo.addCell(makePhrase(contactUsStr + " ", smallHeading, false));
        contactInfo.addCell(makePhrase("", smallHeading, true));

        String phoneStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.phone:", null);
        contactInfo.addCell(makePhrase(phoneStr + " ", smallHeading, false));
        contactInfo.addCell(
            makePhrase(
                "" + contact.getPhone() + "  " + contact.getCallHours(), smallHeading, true));

        String emailStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.email:", null);
        contactInfo.addCell(makePhrase(emailStr + " ", smallHeading, false));
        contactInfo.addCell(makePhrase(contact.getEmail(), smallHeading, true));
      }
    }

    // make the two tables into one
    PdfPTable wholeTable = new PdfPTable(2);
    wholeTable.setWidthPercentage(100);

    wholeTable.setWidths(halves);
    wholeTable.getDefaultCell().setBorder(borderType);

    wholeTable.addCell(personalInfo);
    wholeTable.addCell(contactInfo);
    document.add(wholeTable);

    String ogcomments = "";

    if (null != sForm.getAppUser().getSite()
        && null != sForm.getAppUser().getSite().getComments()) {
      ogcomments = sForm.getAppUser().getSite().getComments().getValue();
    }

    if (null == ogcomments || ogcomments.length() == 0) {
      if (null != sForm.getAppUser().getUserAccount()) {
        ogcomments = sForm.getAppUser().getUserAccount().getComments().getValue();
      }
    }

    // add the comments if there are any
    if (ogcomments != null && ogcomments.length() > 0) {
      document.add(makeBlankLine());
      String commentsStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.comments:", null);
      document.add(makePhrase(commentsStr + " " + ogcomments, smallHeading, true));
    }

    document.add(makeBlankLine());
    String commentsStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.comments:", null);
    document.add(
        makePhrase(
            commentsStr
                + " "
                + "__________________________________________"
                + "__________________________________________"
                + "__________",
            smallHeading,
            true));
    document.add(
        makePhrase(
            "__________"
                + "__________________________________________"
                + "__________________________________________"
                + "__________",
            smallHeading,
            true));
    document.add(
        makePhrase(
            "__________"
                + "__________________________________________"
                + "__________________________________________"
                + "__________",
            smallHeading,
            true));

    // Add an order guide note in the next page if a note is present.
    if (null != sForm.getAppUser().getUserAccount()
        && null != sForm.getAppUser().getUserAccount().getOrderGuideNote()) {
      String t = sForm.getAppUser().getUserAccount().getOrderGuideNote().getValue();
      if (t != null && t.length() > 0) {
        document.newPage();
        pageNumber = pageNumber + 1;
        // document.add(makeBlankLine());
        document.add(makePhrase(t, smallHeading, true));
      }
    }

    PdfPTable sort = new PdfPTable(1);
    sort.setWidthPercentage(100);
    sort.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
    sort.getDefaultCell().setBorder(borderType);
    if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
      String sortedByCategoryStr =
          ClwI18nUtil.getMessage(mRequest, "shop.og.text.sortedBy:Category", null);
      sort.addCell(makePhrase(sortedByCategoryStr + " ", smallHeading, false));
    } else if (sForm.getOrderBy() == Constants.ORDER_BY_CUST_SKU) {
      String sortedByOurSkuNumStr =
          ClwI18nUtil.getMessage(mRequest, "shop.og.text.sortedBy:OurSkuNum", null);
      sort.addCell(makePhrase(sortedByOurSkuNumStr, smallHeading, false));
    } else {
      String sortedByProductNameStr =
          ClwI18nUtil.getMessage(mRequest, "shop.og.text.sortedBy:ProductName", null);
      sort.addCell(makePhrase(sortedByProductNameStr, smallHeading, false));
    }
    document.add(sort);
  }
  public void generatePdf(
      UserShopForm sForm,
      List pItems,
      StoreData pStore,
      OutputStream pOut,
      String pImageName,
      boolean personal,
      boolean pCatalogOnly,
      String catalogLocaleCd)
      throws IOException {
    mSkuTag = ClwI18nUtil.getMessage(mRequest, "shop.og.text.ourSkuNum", null);

    try {
      catalogOnly = pCatalogOnly;
      initColumnsAdnWidths(sForm.getAppUser());
      mSiteData = sForm.getAppUser().getSite();
      AccountData mAccount = sForm.getAppUser().getUserAccount();
      String ogInvDisplay =
          mAccount.getPropertyValue(RefCodeNames.PROPERTY_TYPE_CD.INVENTORY_OG_LIST_UI);
      boolean modernShoppingFl = mSiteData.hasModernInventoryShopping();
      // loop through the items to check if the footer needs to have the pack
      // Disclaimer printed on each page.
      boolean printProblemPackDisclaimer = false,
          invItems = false,
          invAutoOrderItems = false,
          nonInvItems = false,
          addNewPage = false;

      for (int i = 0; i < pItems.size(); i++) {
        ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i);
        if (sci.getProduct().isPackProblemSku()) {
          printProblemPackDisclaimer = true;
        }
        if (sci.getIsaInventoryItem()) {
          invItems = true;
          if (null != mSiteData
              && mSiteData.isAnInventoryAutoOrderItem(sci.getProduct().getProductId())) {
            invAutoOrderItems = true;
          }

        } else {
          nonInvItems = true;
          if (invItems) addNewPage = true;
        }
      }

      Phrase headPhrase = new Phrase(makeChunk("", heading, true));
      if (catalogOnly) {
        String catalogStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.catalog", null);
        headPhrase.add(makeChunk(catalogStr, heading, true));
      } else {
        if (sForm.getAppUser().getSite() != null
            && sForm.getAppUser().getSite().getBSC() != null
            && sForm.getAppUser().getSite().getBSC().getBusEntityData() != null
            && sForm.getAppUser().getSite().getBSC().getBusEntityData().getShortDesc() != null) {
          String subname =
              sForm.getAppUser().getSite().getBSC().getBusEntityData().getShortDesc() + " ";
          headPhrase.add(makeChunk(subname, heading, true));
          headPhrase.add(makeChunk(" ", heading, true));
        }
        String orderGuideStr = ClwI18nUtil.getMessage(mRequest, "shop.og.text.orderGuide", null);
        headPhrase.add(makeChunk(orderGuideStr, heading, true));
        headPhrase.add(makeChunk(" ", heading, true));
        headPhrase.add(
            makeChunk(
                sForm.getAppUser().getUserAccount().getBusEntity().getShortDesc(), heading, true));
      }
      HeaderFooter header = new HeaderFooter(headPhrase, true);
      header.setAlignment(HeaderFooter.ALIGN_RIGHT);

      // setup the document
      // pageSize,  marginLeft, marginRight,  marginTop, marginBottom
      Document document = new Document(PageSize.A4, 10, 15, 30, 15);
      PdfWriter writer = PdfWriter.getInstance(document, pOut);

      String addr =
          pStore.getPrimaryAddress().getAddress1() + " " + pStore.getPrimaryAddress().getAddress2();
      Chunk disclaimer = null;
      String fmsg = "";
      if (invItems) {
        fmsg += ClwI18nUtil.getMessage(mRequest, "shop.og.text.i-inventoryItem", null) + " ";
      }
      if (invAutoOrderItems) {
        fmsg += ClwI18nUtil.getMessage(mRequest, "shop.og.text.a-autoOrderItem", null) + " ";
      }
      if (printProblemPackDisclaimer) {
        fmsg +=
            ClwI18nUtil.getMessage(mRequest, "shop.og.text.*packAndOrUomMayDifferByRegion", null);
      }
      if (fmsg.length() > 0) {
        disclaimer = makeChunk(fmsg, smallItalic, true);
      }

      Phrase footPhrase = makeStoreFooter(pStore, disclaimer, null);

      HeaderFooter footer = new HeaderFooter(footPhrase, false);
      footer.setAlignment(HeaderFooter.ALIGN_CENTER);

      // setup the borders from the header
      header.setBorder(borderType);
      footer.setBorder(HeaderFooter.TOP);

      document.setHeader(header);
      document.setFooter(footer);
      document.open();

      // voc message
      String vocMessage = ClwI18nUtil.getMessage(mRequest, "shop.message.vocNotIncluded", null);
      document.add(makePhrase(vocMessage, smallHeading, true));
      document.add(makePhrase(null, smallHeading, true));

      drawOGHeader(document, sForm, pImageName, personal);
      if (modernShoppingFl && !"SEPARATED LIST".equals(ogInvDisplay)) {
        drawHeader(
            document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true);

        String prevCat = null;
        for (int i = 0; i < pItems.size(); i++) {
          ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i);
          Table itmTable = makeItemElement(sci);

          // if the item data will not fit onto the page,
          // make a new page, and redraw the header.

          if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
            if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
              if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) {
                document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
                document.add(makePhrase(null, smallHeading, true));
              }
              prevCat = sci.getCategoryPath();
            }
          }

          if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
            document.newPage();
            pageNumber = pageNumber + 1;
            drawHeader(
                document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true);
            if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
              document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
              document.add(makePhrase(null, smallHeading, true));
              prevCat = sci.getCategoryPath();
            }
          }
          document.add(itmTable);
        }

      } else {
        if (invItems) {
          drawHeader(
              document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true);

          String prevCat = null;
          for (int i = 0; i < pItems.size(); i++) {
            ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i);
            if (sci.getIsaInventoryItem() == false) {
              continue;
            }
            if (sci.getInventoryParValuesSum() <= 0) {
              // This is an inventory item that is not
              // allowed for this location.
              continue;
            }
            Table itmTable = makeItemElement(sci);

            // if the item data will not fit onto the page,
            // make a new page, and redraw the header.

            if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
              if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
                if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) {
                  document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
                  document.add(makePhrase(null, smallHeading, true));
                }
                prevCat = sci.getCategoryPath();
              }
            }

            if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
              document.newPage();
              pageNumber = pageNumber + 1;
              drawHeader(
                  document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, true);
              if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
                document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
                document.add(makePhrase(null, smallHeading, true));
                prevCat = sci.getCategoryPath();
              }
            }
            document.add(itmTable);
          }
        }

        if (nonInvItems) {

          if (addNewPage) {
            document.newPage();
            pageNumber = pageNumber + 1;
          }

          drawHeader(
              document, pageNumber, pStore.getStoreBusinessName().getValue(), pImageName, false);
          document.add(makeBlankLine());

          String prevCat = null;
          for (int i = 0; i < pItems.size(); i++) {
            ShoppingCartItemData sci = (ShoppingCartItemData) pItems.get(i);
            if (sci.getIsaInventoryItem()) {
              continue;
            }

            Table itmTable = makeItemElement(sci);

            // Check to see if the category needs to be put out.
            if (writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
              if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
                if (null == prevCat || !prevCat.equals(sci.getCategoryPath())) {
                  document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
                  document.add(makePhrase(null, smallHeading, true));
                }
                prevCat = sci.getCategoryPath();
              }
            }

            // if the item data will not fit onto the page,
            // make a new page, and redraw the header.

            if (!writer.fitsPage(itmTable, document.bottomMargin() + 10)) {
              document.newPage();
              pageNumber = pageNumber + 1;
              drawHeader(
                  document,
                  pageNumber,
                  pStore.getStoreBusinessName().getValue(),
                  pImageName,
                  false);
              if (sForm.getOrderBy() == Constants.ORDER_BY_CATEGORY) {
                document.add(makePhrase(sci.getCategoryPath(), smallHeading, true));
                document.add(makePhrase(null, smallHeading, true));
                prevCat = sci.getCategoryPath();
              }
            }
            document.add(itmTable);
          }
        }
      }

      // close out the document
      document.close();
    } catch (DocumentException e) {
      e.printStackTrace();
      throw new IOException(e.getMessage());
    }
  }
  private void drawHeader(
      Document document,
      int pPageNumber,
      String pSenderName,
      String pImageName,
      boolean pInventoryHeader)
      throws DocumentException {
    // add the logo
    try {
      Image i = Image.getInstance(pImageName);
      float x = document.leftMargin();
      float y = document.getPageSize().getHeight() - i.getHeight() - 5;
      i.setAbsolutePosition(x, y);
      document.add(i);
    } catch (Exception e) {
      // e.printStackTrace();
    }

    // deal with header info construct the table/cells with all of
    // the header information must be the pdf specific type or the
    // table will not use the full width of the page.
    // unfortuanately this means we are limited to PDF rendering
    // only.

    // draw a line XXX this is somewhat of a hack, as there seems
    // to be no way to draw a line in the current iText API that is
    // relative to your current position in the document

    document.add(makeLine());

    // add the item header line
    PdfPTable itemHeader = new PdfPTable(mColumnCount);
    itemHeader.setWidthPercentage(100);
    itemHeader.setWidths(itmColumnWidth);
    itemHeader.getDefaultCell().setBorderWidth(2);
    itemHeader.getDefaultCell().setBackgroundColor(java.awt.Color.black);
    itemHeader.getDefaultCell().setHorizontalAlignment(Cell.ALIGN_CENTER);
    itemHeader.getDefaultCell().setVerticalAlignment(Cell.ALIGN_MIDDLE);
    itemHeader.getDefaultCell().setBorderColor(java.awt.Color.white);
    if (!catalogOnly) {
      String c1 = "";
      if (pInventoryHeader) {
        String qtyOnHandStr =
            ClwI18nUtil.getMessage(mRequest, "shop.og.table.header.qtyOnHand", null);
        itemHeader.addCell(makePhrase(qtyOnHandStr, itemHeading, false));
        String requestedQtyStr =
            ClwI18nUtil.getMessage(mRequest, "shop.og.text.requestedQty", null);
        itemHeader.addCell(makePhrase(requestedQtyStr, itemHeading, false));
        mForInventoryShopping = true;
      } else {
        itemHeader.addCell(makePhrase(" ", itemHeading, false));
        if (mForInventoryShopping) {
          c1 = ClwI18nUtil.getMessage(mRequest, "shop.og.text.requestedQty", null);
        } else {
          c1 = ClwI18nUtil.getMessage(mRequest, "shop.og.text.orderQty", null);
        }
        itemHeader.addCell(makePhrase(c1, itemHeading, false));
      }
    }
    if (catalogOnly) {
      String c1 = ClwI18nUtil.getMessage(mRequest, "shop.og.text.orderQty", null);
      itemHeader.addCell(makePhrase(c1, itemHeading, false));
    }

    itemHeader.addCell(makePhrase(mSkuTag, itemHeading, false));

    String c2 = "";
    if (pInventoryHeader) {
      c2 = ClwI18nUtil.getMessage(mRequest, "shop.og.text.inventoryProductName", null);
    } else {
      c2 = ClwI18nUtil.getMessage(mRequest, "shop.og.text.productName", null);
    }

    itemHeader.addCell(makePhrase(c2, itemHeading, false));
    if (mShowSize) {
      String sizeStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.size", null);
      itemHeader.addCell(makePhrase(sizeStr, itemHeading, false));
    }
    /*    String packStr =
      ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.pack", null);
    itemHeader.addCell(makePhrase(packStr, itemHeading, false));
    String uomStr =
      ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.uom", null);
    itemHeader.addCell(makePhrase(uomStr, itemHeading, false));*/

    CurrencyData curr = ClwI18nUtil.getCurrency(mCatalogLocaleCd);
    String currencyCode = "CHF";
    if (curr != null) {
      currencyCode = curr.getGlobalCode();
    }

    if (mShowPrice) {
      String priceStr =
          ClwI18nUtil.getMessage(
              mRequest, "shoppingItems.text.priceIn", new String[] {currencyCode});
      itemHeader.addCell(makePhrase(priceStr, itemHeading, false));
    }
    if (catalogOnly) {
      String splStr = ClwI18nUtil.getMessage(mRequest, "shoppingItems.text.spl", null);
      itemHeader.addCell(makePhrase(splStr, itemHeading, false));
    }
    if (!catalogOnly) {
      String amountStr =
          ClwI18nUtil.getMessage(
              mRequest, "shoppingItems.text.amountIn", new String[] {currencyCode});
      itemHeader.addCell(makePhrase(amountStr, itemHeading, false));
    }

    document.add(itemHeader);
  }