C++ (Qt)void setPageSize ( const QSizeF & size )void QTextDocument::drawContents ( QPainter * p, const QRectF & rect = QRectF() )
class PageFormat{public: PageFormat( QPaintDevice *device, const QString &format = "A4", bool isPortrait = true, qreal mmLeftMargin = 15.0, qreal mmTopMargin = 15.0, qreal mmRightMargin = 15.0, qreal mmBottomMargin = 15.0 ); void update( const QString &format = "A4", bool isPortrait = true, qreal mmLeftMargin = 15.0, qreal mmTopMargin = 15.0, qreal mmRightMargin = 15.0, qreal mmBottomMargin = 15.0 ); void update( int formatId, bool isPortrait = true, qreal mmLeftMargin = 15.0, qreal mmTopMargin = 15.0, qreal mmRightMargin = 15.0, qreal mmBottomMargin = 15.0 ); inline qreal leftMargin() const { return m_leftMargin; } inline qreal topMargin() const { return m_topMargin; } inline qreal rightMargin() const { return m_rightMargin; } inline qreal bottomMargin() const { return m_bottomMargin; } inline qreal paperWidth() const { return m_paperWidth; } inline qreal pageWidth() const { return m_pageWidth; } inline qreal paperHeight() const { return m_paperHeight; } inline qreal pageHeight() const { return m_pageHeight; } inline int formatId() const { return m_formatId; } inline bool isPortrait() const { return m_isPortrait; } inline qreal mmLeftMargin() const { return m_mmLeftMargin; } inline qreal mmTopMargin() const { return m_mmTopMargin; } inline qreal mmRightMargin() const { return m_mmRightMargin; } inline qreal mmBottomMargin() const { return m_mmBottomMargin; } static QSizeF sizeForFormatId( int formatId, bool isPortrait );private: QString m_format; // Name of Page Format <=> QPrinter::PaperSize ("A4 (default), "Letter", "B5"...) bool m_isPortrait; // true for Portrait Orientation qreal m_mmLeftMargin; // Left Margin in mm qreal m_mmTopMargin; // Top Margin in mm qreal m_mmRightMargin; // Right Margin in mm qreal m_mmBottomMargin; // Right Margin in mm qreal m_leftMargin; // Left Margin in px qreal m_topMargin; // Top Margin in px qreal m_rightMargin; // Right Margin in px qreal m_bottomMargin; // Right Margin in px qreal m_paperWidth; // Paper Width in px qreal m_pageWidth; // Page Width in px (m_paperWidth - m_leftMargin - m_rightMargin) qreal m_paperHeight; // Paper Height in px qreal m_pageHeight; // Page Height in px (m_paperHeight - m_topMargin - m_bottomMargin) QPaintDevice *m_device; int m_formatId;};
static inline double mmToInches(double mm) { return mm * 0.039370147; }typedef struct tagPageFormatSize { QString formatName; qreal wmm; qreal hmm;} PageFormatSize;static PageFormatSize formats[30] = { { "A0", 841.0, 1189.0 }, { "A1", 594.0, 841.0 }, { "A2", 420.0, 594.0 }, { "A3", 297.0, 420.0 }, { "A4", 210.0, 297.0 }, { "A5", 148.0, 210.0 }, { "A6", 105.0, 148.0 }, { "A7", 74.0, 105.0 }, { "A8", 52.0, 74.0 }, { "A9", 37.0, 52.0 }, { "B0", 1030.0, 1456.0 }, { "B1", 728.0, 1030.0 }, { "B2", 515.0, 728.0 }, { "B3", 364.0, 515.0 }, { "B4", 257.0, 364.0 }, { "B5", 182.0, 257.0 }, { "B6", 128.0, 182.0 }, { "B7", 91.0, 128.0 }, { "B8", 64.0, 91.0 }, { "B9", 45.0, 64.0 }, { "B10", 32.0, 45.0 }, { "C5E", 163.0, 229.0 }, { "Comm10E", 105.0, 241.0 }, { "DLE", 110.0, 220.0 }, { "Executive", 191.0, 254.0 }, { "Folio", 210.0, 330.0 }, { "Ledger", 432.0, 279.0 }, { "Legal", 216.0, 356.0 }, { "Letter", 216.0, 279.0 }, { "Tabloid", 279.0, 432.0 }};static QSizeF sizeForPageFormat( const QString &format, bool isPortrait, int &formatId ){ for (int i = 0; i < 30; i++) if ( !format.compare(formats[i].formatName, Qt::CaseInsensitive) ) { formatId = i; return isPortrait ? QSizeF(formats[i].wmm, formats[i].hmm) : QSizeF(formats[i].hmm, formats[i].wmm); } formatId = 4; return isPortrait ? QSizeF(210.0, 297.0) : QSizeF(297.0, 210.0);}PageFormat::PageFormat( QPaintDevice *device, const QString &format, bool isPortrait, qreal mmLeftMargin, qreal mmTopMargin, qreal mmRightMargin, qreal mmBottomMargin ) : m_device(device){ update(format, isPortrait, mmLeftMargin, mmTopMargin, mmRightMargin, mmBottomMargin);}void PageFormat::update( const QString &format, bool isPortrait, qreal mmLeftMargin, qreal mmTopMargin, qreal mmRightMargin, qreal mmBottomMargin ){ m_format = format; m_isPortrait = isPortrait; m_mmLeftMargin = mmLeftMargin; m_mmTopMargin = mmTopMargin; m_mmRightMargin = mmRightMargin; m_mmBottomMargin = mmBottomMargin; QSizeF mmSize = sizeForPageFormat(m_format, m_isPortrait, m_formatId); m_pageWidth = mmSize.width() - mmLeftMargin - mmRightMargin; m_pageHeight = mmSize.height() - mmTopMargin - mmBottomMargin; m_paperWidth = mmToInches(m_paperWidth) * m_device->logicalDpiX(); m_pageWidth = mmToInches(m_pageWidth) * m_device->logicalDpiX(); m_leftMargin = mmToInches(m_leftMargin) * m_device->logicalDpiX(); m_rightMargin = mmToInches(m_rightMargin) * m_device->logicalDpiX(); m_paperHeight = mmToInches(m_paperHeight) * m_device->logicalDpiY(); m_pageHeight = mmToInches(m_pageHeight) * m_device->logicalDpiY(); m_topMargin = mmToInches(m_topMargin) * m_device->logicalDpiY(); m_bottomMargin = mmToInches(m_bottomMargin) * m_device->logicalDpiY();}void PageFormat::update( int formatId, bool isPortrait, qreal mmLeftMargin, qreal mmTopMargin, qreal mmRightMargin, qreal mmBottomMargin ){ m_format = formats[formatId].formatName; m_isPortrait = isPortrait; m_formatId = formatId; m_mmLeftMargin = mmLeftMargin; m_mmTopMargin = mmTopMargin; m_mmRightMargin = mmRightMargin; m_mmBottomMargin = mmBottomMargin; if ( isPortrait ) { m_paperWidth = formats[formatId].wmm; m_paperHeight = formats[formatId].hmm; } else { m_paperHeight = formats[formatId].wmm; m_paperWidth = formats[formatId].hmm; } m_pageWidth = m_paperWidth - mmLeftMargin - mmRightMargin; m_pageHeight = m_paperHeight - mmTopMargin - mmBottomMargin; m_paperWidth = mmToInches(m_paperWidth) * m_device->logicalDpiX(); m_pageWidth = mmToInches(m_pageWidth) * m_device->logicalDpiX(); m_leftMargin = mmToInches(m_leftMargin) * m_device->logicalDpiX(); m_rightMargin = mmToInches(m_rightMargin) * m_device->logicalDpiX(); m_paperHeight = mmToInches(m_paperHeight) * m_device->logicalDpiY(); m_pageHeight = mmToInches(m_pageHeight) * m_device->logicalDpiY(); m_topMargin = mmToInches(m_topMargin) * m_device->logicalDpiY(); m_bottomMargin = mmToInches(m_bottomMargin) * m_device->logicalDpiY();}QSizeF PageFormat::sizeForFormatId( int formatId, bool isPortrait ){ if ( isPortrait ) return QSizeF( formats[formatId].wmm, formats[formatId].hmm ); return QSizeF( formats[formatId].hmm, formats[formatId].wmm );}
verticalScrollBar()->setRange(0, m_drawPageSize.height() * (document()->pageCount()) - viewport()->size().height());
C++ (Qt)qreal mmLeftMarginqreal mmTopMarginqreal mmRightMarginqreal mmBottomMargin