Esempio n. 1
0
        public MediaSize GetMediaSize()
        {
            if (m_mediaSize != null)
            {
                return(m_mediaSize);
            }

            String nameCode = GetCode();

            if (nameCode != null)
            {
                //	Get Name
                MediaSizeName  nameMedia = null;
                CMediaSizeName msn       = new CMediaSizeName(4);
                String[]       names     = msn.GetStringTable();
                for (int i = 0; i < names.Length; i++)
                {
                    String name = names[i];
                    if (name.Equals(nameCode, StringComparison.InvariantCultureIgnoreCase))
                    {
                        nameMedia = (MediaSizeName)msn.GetEnumValueTable()[i];
                        log.Finer("Name=" + nameMedia);
                        break;
                    }
                }
                if (nameMedia != null)
                {
                    m_mediaSize = MediaSize.GetMediaSizeForName(nameMedia);
                    log.Fine("Name->Size=" + m_mediaSize);
                }
            }
            //	Create New Media Size
            if (m_mediaSize == null)
            {
                float x = (float)GetSizeX();
                float y = (float)GetSizeY();
                if (x > 0 && y > 0)
                {
                    m_mediaSize = new MediaSize(x, y, GetUnitsInt(), MediaSizeName.A);
                    log.Fine("Size=" + m_mediaSize);
                }
                //	Fallback
                if (m_mediaSize == null)
                {
                    m_mediaSize = GetMediaSizeDefault();
                }
                return(m_mediaSize);
            }
            return(m_mediaSize);
        }
Esempio n. 2
0
        /// <summary>
        /// Calculates a <code>PageFormat</code> with values consistent with those
        /// supported by the current <code>PrintService</code> for this job
        /// (ie the value returned by <code>getPrintService()</code>) and media,
        /// printable area and orientation contained in <code>attributes</code>.
        /// <para>
        /// Calling this method does not update the job.
        /// It is useful for clients that have a set of attributes obtained from
        /// <code>printDialog(PrintRequestAttributeSet attributes)</code>
        /// and need a PageFormat to print a Pageable object.
        /// </para>
        /// </summary>
        /// <param name="attributes"> a set of printing attributes, for example obtained
        /// from calling printDialog. If <code>attributes</code> is null a default
        /// PageFormat is returned. </param>
        /// <returns> a <code>PageFormat</code> whose settings conform with
        /// those of the current service and the specified attributes.
        /// @since 1.6 </returns>
        public virtual PageFormat GetPageFormat(PrintRequestAttributeSet attributes)
        {
            PrintService service = PrintService;
            PageFormat   pf      = DefaultPage();

            if (service == null || attributes == null)
            {
                return(pf);
            }

            Media media = (Media)attributes.get(typeof(Media));
            MediaPrintableArea   mpa       = (MediaPrintableArea)attributes.get(typeof(MediaPrintableArea));
            OrientationRequested orientReq = (OrientationRequested)attributes.get(typeof(OrientationRequested));

            if (media == null && mpa == null && orientReq == null)
            {
                return(pf);
            }
            Paper paper = pf.Paper;

            /* If there's a media but no media printable area, we can try
             * to retrieve the default value for mpa and use that.
             */
            if (mpa == null && media != null && service.isAttributeCategorySupported(typeof(MediaPrintableArea)))
            {
                Object mpaVals = service.getSupportedAttributeValues(typeof(MediaPrintableArea), null, attributes);
                if (mpaVals is MediaPrintableArea[] && ((MediaPrintableArea[])mpaVals).Length > 0)
                {
                    mpa = ((MediaPrintableArea[])mpaVals)[0];
                }
            }

            if (media != null && service.isAttributeValueSupported(media, null, attributes))
            {
                if (media is MediaSizeName)
                {
                    MediaSizeName msn = (MediaSizeName)media;
                    MediaSize     msz = MediaSize.getMediaSizeForName(msn);
                    if (msz != null)
                    {
                        double inch     = 72.0;
                        double paperWid = msz.getX(MediaSize.INCH) * inch;
                        double paperHgt = msz.getY(MediaSize.INCH) * inch;
                        paper.SetSize(paperWid, paperHgt);
                        if (mpa == null)
                        {
                            paper.SetImageableArea(inch, inch, paperWid - 2 * inch, paperHgt - 2 * inch);
                        }
                    }
                }
            }

            if (mpa != null && service.isAttributeValueSupported(mpa, null, attributes))
            {
                float[] printableArea = mpa.getPrintableArea(MediaPrintableArea.INCH);
                for (int i = 0; i < printableArea.Length; i++)
                {
                    printableArea[i] = printableArea[i] * 72.0f;
                }
                paper.SetImageableArea(printableArea[0], printableArea[1], printableArea[2], printableArea[3]);
            }

            if (orientReq != null && service.isAttributeValueSupported(orientReq, null, attributes))
            {
                int orient;
                if (orientReq.Equals(OrientationRequested.REVERSE_LANDSCAPE))
                {
                    orient = PageFormat.REVERSE_LANDSCAPE;
                }
                else if (orientReq.Equals(OrientationRequested.LANDSCAPE))
                {
                    orient = PageFormat.LANDSCAPE;
                }
                else
                {
                    orient = PageFormat.PORTRAIT;
                }
                pf.Orientation = orient;
            }

            pf.Paper = paper;
            pf       = ValidatePage(pf);
            return(pf);
        }