示例#1
0
PdfAnnotation::PdfAnnotation( PdfPage* pPage, EPdfAnnotation eAnnot, const PdfRect & rRect, PdfVecObjects* pParent )
    : PdfElement( "Annot", pParent ), m_eAnnotation( eAnnot ), m_pAction( NULL ), m_pFileSpec( NULL ), m_pPage( pPage )
{
    PdfVariant    rect;
    PdfDate       date;
    PdfString     sDate;
    const PdfName name( TypeNameForIndex( eAnnot, s_names, s_lNumActions ) );

    if( !name.GetLength() )
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }

    rRect.ToVariant( rect );

    m_pObject->GetDictionary().AddKey( PdfName::KeyRect, rect );

    rRect.ToVariant( rect );
    date.ToString( sDate );
    
    m_pObject->GetDictionary().AddKey( PdfName::KeySubtype, name );
    m_pObject->GetDictionary().AddKey( PdfName::KeyRect, rect );
    m_pObject->GetDictionary().AddKey( "P", pPage->GetObject()->Reference() );
    m_pObject->GetDictionary().AddKey( "M", sDate );
}
示例#2
0
void WatermarkFile( const char* pszInFilename, const char* pszOutFilename )
{
    printf("Running watermark test\n");

    PdfMemDocument doc( pszInFilename );
    PdfPainter     painter;
    PdfPage*       pPage;
    PdfRect        rect;
    int            i;

    for(i=0;i<doc.GetPageCount();i++)
    {
        pPage = doc.GetPage( i );
        if( !pPage ) 
        {
            PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
        }
        
        rect = pPage->GetPageSize();

        painter.SetPage( pPage );
        painter.SetStrokingColor( 1.0, 0.0, 0.0 );
        painter.SetStrokeWidth( 5 );
        painter.DrawLine( 0.0, 0.0, rect.GetWidth(), rect.GetHeight() );
        painter.DrawLine( 0, rect.GetHeight(), rect.GetWidth(), 0.0 );
        painter.FinishPage();
    }

    printf("writing document back\n");
    doc.Write( pszOutFilename );
}
示例#3
0
const PdfRect PdfPage::GetPageBox( const char* inBox ) const
{
    PdfRect	 pageBox;
    PdfObject*   pObj;
        
    // Take advantage of inherited values - walking up the tree if necessary
    pObj = GetInheritedKeyFromObject( inBox, m_pObject );
    
    // assign the value of the box from the array
    if ( pObj && pObj->IsArray() )
        pageBox.FromArray( pObj->GetArray() );
    
    return pageBox;
}
示例#4
0
void draw( char* pszBuffer, PdfDocument* pDocument )
{
    PdfPage*        pPage;
    PdfPainter      painter;
    PdfFont*        pFont;
    PdfRect         size;

    double dX       = BORDER_LEFT;
    double dY       = BORDER_TOP;
    char*  pszStart = pszBuffer;

    size            = PdfPage::CreateStandardPageSize( ePdfPageSize_A4 );
    pFont = pDocument->CreateFont( "Arial" );
    pPage = pDocument->CreatePage( size );

    if( !pFont )
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }
    pFont->SetFontSize( FONT_SIZE );
    
    painter.SetPage( pPage );
    painter.SetFont( pFont );

    while( *pszBuffer )
    {
        if( *pszBuffer == '\n' )
        {
            painter.DrawText( dX, dY, pszStart, pszBuffer-pszStart );
    
            pszStart = (++pszBuffer);            

            dY += pFont->GetFontMetrics()->GetLineSpacing();
            if( dY > (size.GetHeight() -  BORDER_TOP) )
            {
                pPage = pDocument->CreatePage( size );
                painter.SetPage( pPage );
                dY       = BORDER_TOP;
            }
        }
        else
            ++pszBuffer;
    }

    painter.FinishPage();
}
PdfDestination::PdfDestination( const PdfPage* pPage, const PdfRect & rRect )
{
    PdfVariant var;

    rRect.ToVariant( var );

    m_array.push_back( pPage->GetObject()->Reference() );
    m_array.push_back( PdfName("FitR") );
    m_array.insert( m_array.end(), var.GetArray().begin(), var.GetArray().end() );
    m_pObject = pPage->GetObject()->GetOwner()->CreateObject( m_array );
}
示例#6
0
err_info *Pdfwrite::addPage (const Filepage *mp)
   {
   try
      {
      Q_ASSERT (_doc);
//       _doc = new PdfMemDocument (_fname.latin1 ());
      PdfPage *page;
      PdfPainter painter;

      page = _doc->CreatePage (PdfPage::CreateStandardPageSize (ePdfPageSize_A4));
      if (!page)
         PODOFO_RAISE_ERROR (ePdfError_InvalidHandle);
      painter.SetPage (page);

      PdfImage *image = new PdfImage (_doc);

      QByteArray ba = mp->invertData ();
      PdfMemoryInputStream input (ba.constData (), mp->_size);

      image->SetImageColorSpace (mp->_depth <= 8 ? ePdfColorSpace_DeviceGray
            : ePdfColorSpace_DeviceRGB);
      image->SetImageData (mp->_width, mp->_height, mp->_depth == 1 ? 1 : 8, &input);
      PdfRect rect = page->GetPageSize ();
      double w = image->GetWidth ();
      double h = image->GetHeight ();
      double xscale = rect.GetWidth () / w;
      double yscale = rect.GetHeight () / h;
      double scale = xscale;
      if (scale > yscale)
         scale = yscale;
      painter.DrawImage (rect.GetLeft (), rect.GetBottom (), image, scale, scale);
      painter.FinishPage();
      }
   catch (const PdfError &eCode)
      {
      return err_make (ERRFN, E_pdf_creation_error1, eCode.ErrorMessage (eCode.GetError()));
      }
   return NULL;
   }
示例#7
0
void PdfPage::InitNewPage( const PdfRect & rSize )
{
    PdfVariant mediabox;
    rSize.ToVariant( mediabox );
    m_pObject->GetDictionary().AddKey( "MediaBox", mediabox );

    // The PDF specification suggests that we send all available PDF Procedure sets
    m_pObject->GetDictionary().AddKey( "Resources", PdfObject( PdfDictionary() ) );

    m_pResources = m_pObject->GetIndirectKey( "Resources" );
    m_pResources->GetDictionary().AddKey( "ProcSet", PdfCanvas::GetProcSet() );

    // Add contents to page object
    // m_pContents must have been initialized before calling this method
    m_pObject->GetDictionary().AddKey( PdfName::KeyContents, m_pContents->GetContents()->Reference());
}
示例#8
0
void PdfXObject::InitXObject( const PdfRect & rRect, const char* pszPrefix )
{
    PdfVariant    var;
    ostringstream out;
    PdfLocaleImbue(out);

    // Initialize static data
    if( s_matrix.empty() )
    {
        // This matrix is the same for all PdfXObjects so cache it
        s_matrix.push_back( PdfVariant( 1LL ) );
        s_matrix.push_back( PdfVariant( 0LL ) );
        s_matrix.push_back( PdfVariant( 0LL ) );
        s_matrix.push_back( PdfVariant( 1LL ) );
        s_matrix.push_back( PdfVariant( 0LL ) );
        s_matrix.push_back( PdfVariant( 0LL ) );
    }

    rRect.ToVariant( var );
    m_pObject->GetDictionary().AddKey( "BBox", var );
    m_pObject->GetDictionary().AddKey( PdfName::KeySubtype, PdfName("Form") );
    m_pObject->GetDictionary().AddKey( "FormType", PdfVariant( 1LL ) ); // only 1 is only defined in the specification.
    m_pObject->GetDictionary().AddKey( "Matrix", s_matrix );

    // The PDF specification suggests that we send all available PDF Procedure sets
    m_pObject->GetDictionary().AddKey( "Resources", PdfObject( PdfDictionary() ) );
    m_pResources = m_pObject->GetDictionary().GetKey( "Resources" );
    m_pResources->GetDictionary().AddKey( "ProcSet", PdfCanvas::GetProcSet() );

    // Implementation note: the identifier is always
    // Prefix+ObjectNo. Prefix is /XOb for XObject.
	if ( pszPrefix == NULL )
	    out << "XOb" << m_pObject->Reference().ObjectNumber();
	else
	    out << pszPrefix << m_pObject->Reference().ObjectNumber();

    m_Identifier = PdfName( out.str().c_str() );
    m_Reference  = m_pObject->Reference();
}
示例#9
0
PdfRect PdfPage::CreateStandardPageSize( const EPdfPageSize ePageSize, bool bLandscape )
{
    PdfRect rect;

    switch( ePageSize ) 
    {
        case ePdfPageSize_A0:
            rect.SetWidth( 2384.0 );
            rect.SetHeight( 3370.0 );
            break;

        case ePdfPageSize_A1:
            rect.SetWidth( 1684.0 );
            rect.SetHeight( 2384.0 );
            break;

        case ePdfPageSize_A2:
            rect.SetWidth( 1191.0 );
            rect.SetHeight( 1684.0 );
            break;
            
        case ePdfPageSize_A3:
            rect.SetWidth( 842.0 );
            rect.SetHeight( 1190.0 );
            break;

        case ePdfPageSize_A4:
            rect.SetWidth( 595.0 );
            rect.SetHeight( 842.0 );
            break;

        case ePdfPageSize_A5:
            rect.SetWidth( 420.0 );
            rect.SetHeight( 595.0 );
            break;

        case ePdfPageSize_A6:
            rect.SetWidth( 297.0 );
            rect.SetHeight( 420.0 );
            break;

        case ePdfPageSize_Letter:
            rect.SetWidth( 612.0 );
            rect.SetHeight( 792.0 );
            break;
            
        case ePdfPageSize_Legal:
            rect.SetWidth( 612.0 );
            rect.SetHeight( 1008.0 );
            break;

        case ePdfPageSize_Tabloid:
            rect.SetWidth( 792.0 );
            rect.SetHeight( 1224.0 );
            break;

        default:
            break;
    }

    if( bLandscape ) 
    {
        double dTmp = rect.GetWidth();
        rect.SetWidth ( rect.GetHeight() );
        rect.SetHeight(  dTmp );
    }

    return rect;
}
示例#10
0
void draw( char* pszBuffer, PdfDocument* pDocument, bool bUtf8, const char* pszFontName )
{
    PdfPage*           pPage;
    PdfPainter         painter;
    PdfFont*           pFont;
    PdfRect            size;
    const PdfEncoding* pEncoding;

    double dX       = BORDER_LEFT;
    double dY       = BORDER_TOP;
    char*  pszStart = pszBuffer;

    if( bUtf8 ) 
    {
        pEncoding = new PdfIdentityEncoding();
    }
    else
    {
        pEncoding = PdfEncodingFactory::GlobalWinAnsiEncodingInstance();
    }

    size            = PdfPage::CreateStandardPageSize( ePdfPageSize_A4 );
    pFont = pDocument->CreateFont( pszFontName, pEncoding );
    pPage = pDocument->CreatePage( size );
    dY    = size.GetHeight() - dY;

    if( !pFont )
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }
    pFont->SetFontSize( FONT_SIZE );
    
    painter.SetPage( pPage );
    painter.SetFont( pFont );

    while( *pszBuffer )
    {
        if( *pszBuffer == '\n' )
        {
            if( bUtf8 ) 
            {
                painter.DrawText( dX, dY, PdfString( reinterpret_cast<const pdf_utf8*>(pszStart), 
                                                     pszBuffer-pszStart ) );
            }
            else
            {
                painter.DrawText( dX, dY, pszStart, pszBuffer-pszStart );
            }
    
            pszStart = (++pszBuffer);            

            dY -= pFont->GetFontMetrics()->GetLineSpacing();
            if( dY < BORDER_TOP )
            {
                pPage = pDocument->CreatePage( size );
                painter.SetPage( pPage );
                dY       = size.GetHeight() - dY;
            }
        }
        else
            ++pszBuffer;
    }

    painter.FinishPage();
}
void PdfTilingPattern::Init( EPdfTilingPatternType eTilingType,
		 double strokeR, double strokeG, double strokeB,
		 bool doFill, double fillR, double fillG, double fillB,
		 double offsetX, double offsetY,
		 PdfImage *pImage)
{
	if (eTilingType == ePdfTilingPatternType_Image && pImage == NULL) {
		PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
	}

	if (eTilingType != ePdfTilingPatternType_Image && pImage != NULL) {
		PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
	}

	PdfRect rRect;
	rRect.SetLeft(0);
	rRect.SetBottom(0);

	if (pImage) {
		rRect.SetWidth(pImage->GetWidth());
		rRect.SetHeight(-pImage->GetHeight());
	} else {
		rRect.SetWidth(8);
		rRect.SetHeight(8);
	}

	PdfVariant var;
   rRect.ToVariant( var );

	this->GetObject()->GetDictionary().AddKey( PdfName("PatternType"), static_cast<pdf_int64>(1L) ); // Tiling pattern
	this->GetObject()->GetDictionary().AddKey( PdfName("PaintType"), static_cast<pdf_int64>(1L) ); // Colored
	this->GetObject()->GetDictionary().AddKey( PdfName("TilingType"), static_cast<pdf_int64>(1L) ); // Constant spacing
	this->GetObject()->GetDictionary().AddKey( PdfName("BBox"), var );
	this->GetObject()->GetDictionary().AddKey( PdfName("XStep"), static_cast<pdf_int64>(rRect.GetWidth()) );
	this->GetObject()->GetDictionary().AddKey( PdfName("YStep"), static_cast<pdf_int64>(rRect.GetHeight()) );
	this->GetObject()->GetDictionary().AddKey( PdfName("Resources"), PdfObject( PdfDictionary() ) );

	if (offsetX < -1e-9 || offsetX > 1e-9 || offsetY < -1e-9 || offsetY > 1e-9) {
		PdfArray array;

		array.push_back (static_cast<pdf_int64>(1));
		array.push_back (static_cast<pdf_int64>(0));
		array.push_back (static_cast<pdf_int64>(0));
		array.push_back (static_cast<pdf_int64>(1));
		array.push_back (offsetX);
		array.push_back (offsetY);

		this->GetObject()->GetDictionary().AddKey( PdfName("Matrix"), array );
	}

   std::ostringstream out;
   out.flags( std::ios_base::fixed );
   out.precision( 1L /* clPainterDefaultPrecision */ );
   PdfLocaleImbue(out);

	if (pImage) {
		AddToResources(pImage->GetIdentifier(), pImage->GetObjectReference(), PdfName("XObject"));

      out << rRect.GetWidth() << " 0 0 "
          << rRect.GetHeight() << " "
          << rRect.GetLeft() << " " 
          << rRect.GetBottom() << " cm" << std::endl;
		out << "/" << pImage->GetIdentifier().GetName() << " Do" << std::endl;
	} else {
		if (doFill) {
			out << fillR << " " << fillG << " " << fillB << " rg" << " ";
			out << rRect.GetLeft() << " " << rRect.GetBottom() << " " << rRect.GetWidth() << " " << rRect.GetHeight() << " re" << " ";
			out << "f" <<  " "; //fill rect
		}

		out << strokeR << " " << strokeG << " " << strokeB << " RG" << " ";
		out << "2 J" << " "; // line capability style
		out << "0.5 w" <<  " "; //line width

		double left, bottom, right, top, whalf, hhalf;
		left = rRect.GetLeft();
		bottom = rRect.GetBottom();
		right = left + rRect.GetWidth();
		top = bottom + rRect.GetHeight();
		whalf = rRect.GetWidth() / 2;
		hhalf = rRect.GetHeight() / 2;

		switch (eTilingType) {
		case ePdfTilingPatternType_BDiagonal:
			out << left          << " " << bottom         << " m " << right         << " " << top            << " l ";
			out << left - whalf  << " " << top - hhalf    << " m " << left + whalf  << " " << top + hhalf    << " l ";
			out << right - whalf << " " << bottom - hhalf << " m " << right + whalf << " " << bottom + hhalf << " l" << std::endl;
			break;
		case ePdfTilingPatternType_Cross:
			out << left          << " " << bottom + hhalf << " m " << right         << " " << bottom + hhalf << " l ";
			out << left + whalf  << " " << bottom         << " m " << left + whalf  << " " << top            << " l" << std::endl;
			break;
		case ePdfTilingPatternType_DiagCross:
			out << left          << " " << bottom         << " m " << right         << " " << top            << " l ";
			out << left          << " " << top            << " m " << right         << " " << bottom         << " l" << std::endl;
			break;
		case ePdfTilingPatternType_FDiagonal:
			out << left          << " " << top            << " m " << right         << " " << bottom         << " l ";
			out << left - whalf  << " " << bottom + hhalf << " m " << left + whalf  << " " << bottom - hhalf << " l ";
			out << right - whalf << " " << top + hhalf    << " m " << right + whalf << " " << top - hhalf    << " l" << std::endl;
			break;
		case ePdfTilingPatternType_Horizontal:
			out << left          << " " << bottom + hhalf << " m " << right         << " " << bottom + hhalf << " l ";
			break;
		case ePdfTilingPatternType_Vertical:
			out << left + whalf  << " " << bottom         << " m " << left + whalf  << " " << top            << " l" << std::endl;
			break;
		case ePdfTilingPatternType_Image:
			/* This is handled above, based on the 'pImage' variable */
		default:
			PODOFO_RAISE_ERROR (ePdfError_InvalidEnumValue);
			break;

		}

		out << "S"; //stroke path
	}

	TVecFilters vecFlate;
	vecFlate.push_back( ePdfFilter_FlateDecode );

	std::string str = out.str();
	PdfMemoryInputStream stream(str.c_str(), str.length());

	this->GetObject()->GetStream()->Set(&stream, vecFlate);
}
示例#12
0
void PdfTable::Draw( double dX, double dY, PdfPainter* pPainter, const PdfRect & rClipRect,
                     double* pdLastX, double* pdLastY )
{
    if( !pPainter ) 
    {
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
    }

    //RG: TODO Should dCurY variable be initialised with 0? (line 257 may fall through without initialisation!)
    int i = 0;
    int j = 0;
    double  dCurX = 0.0;
    double  dCurY = 0.0;

    double  dWidth = 0.0;
    double  dHeight = 0.0;
    double  dVertical = 0.0;
    double* pdColWidths  = new double[this->GetCols()];
    double* pdRowHeights = new double[this->GetRows()];

	bool bBorders = !m_pModel || (m_pModel && m_pModel->HasBorders() );

    // Calculate all necessary sizes
    this->CalculateTableSize( dX, dY, pPainter->GetPage(), 
                              pdColWidths, pdRowHeights,
                              &dWidth, &dHeight );
 
    if( !(!static_cast<int>(rClipRect.GetBottom()) && 
          !static_cast<int>(rClipRect.GetLeft()) &&
          !static_cast<int>(rClipRect.GetWidth()) && 
          !static_cast<int>(rClipRect.GetWidth())) ) 
        m_curClipRect = rClipRect;
    else
    {
        m_curClipRect = PdfRect( 0.0, dX, 
                                 pPainter->GetPage()->GetPageSize().GetWidth() - dX,
                                 dY );
    }

    // Draw the table
    pPainter->Save();
    PdfFont* pDefaultFont = pPainter->GetFont(); // get the default font
    PdfFont* pFont;

    // draw contents
    if( m_pModel ) 
    {
		pPainter->SetStrokeWidth( m_pModel->GetBorderWidth() );

		if( bBorders ) // draw top border
            this->DrawHorizontalBorders( 0, dX, dY, pPainter, pdColWidths );

        for( j=0;j<m_nRows;j++ )
        {
			if( this->CheckForNewPage( &dY, &dCurY, pdRowHeights[j], pPainter ) && bBorders )
                // draw top border on new page
                this->DrawHorizontalBorders( j, dX, dY, pPainter, pdColWidths );
    
			dCurX  = 0.0;	
			dCurY += pdRowHeights[j];

            for( i=0;i<m_nCols;i++ ) 
            {
	            // set a clipping rectangle
                pPainter->Save();
                pPainter->SetClipRect( dX + dCurX, dY - dCurY, pdColWidths[i], pdRowHeights[j] );

                // Draw background
				double dBorder = bBorders ? m_pModel->GetBorderWidth()/2.0 : 0.0;
                if( m_pModel->HasBackgroundColor( i, j ) ) 
                {
                    pPainter->Save();
                    pPainter->SetColor( m_pModel->GetBackgroundColor( i, j ) );
					// Make sure that FillRect only fills inside the border
					// rectangle and not over the border. This is necessary
					// because we draw the border first and than the contents.
                    pPainter->FillRect( dX + dCurX + dBorder, dY - dCurY + dBorder, 
						                pdColWidths[i] - 2.0 * dBorder, 
										pdRowHeights[j] - 2.0 * dBorder );
                    pPainter->Restore();
                }

                // draw an image
                PdfImage* pImage = m_pModel->GetImage( i, j );
                double dImageWidth = 0.0;
                if( m_pModel->HasImage( i, j ) && pImage )
                {
                    double dScaleX = (pdColWidths[i])  / pImage->GetPageSize().GetWidth();
                    double dScaleY = (pdRowHeights[j] - 2.0 * dBorder) / pImage->GetPageSize().GetHeight();
                    double dScale  = PDF_MIN( dScaleX, dScaleY );

                    dImageWidth = pImage->GetPageSize().GetWidth() * dScale;

                    pPainter->DrawImage( dX + dCurX, dY - dCurY + dBorder, pImage, dScale, dScale );
                }

                // Set the correct font
                pFont = m_pModel->GetFont( i, j );
                pFont = pFont ? pFont : pDefaultFont;
                pPainter->SetFont( pFont );
				pPainter->SetColor( m_pModel->GetForegroundColor( i, j ) );

                // draw text
				if( m_pModel->HasWordWrap( i, j ) )
				{
					// Make sure we have at least 1 dot free space at each side of the rectangle
					pPainter->DrawMultiLineText( dX + dCurX + 1.0 + dImageWidth, dY - dCurY, 
                                                 pdColWidths[i] - 2.0 - dImageWidth, pdRowHeights[j],
												 m_pModel->GetText( i, j ), m_pModel->GetAlignment( i, j ),
												 m_pModel->GetVerticalAlignment( i, j ) );
				}
				else
				{
					// calculate vertical alignment
					switch( m_pModel->GetVerticalAlignment( i, j ) ) 
					{
						default:
						case ePdfVerticalAlignment_Top:
							dVertical = 0.0;
							break;
						case ePdfVerticalAlignment_Center:
							dVertical = (pdRowHeights[j] - pFont->GetFontMetrics()->GetLineSpacing()) / 2.0;
							break;
						case ePdfVerticalAlignment_Bottom:
							dVertical = (pdRowHeights[j] - pFont->GetFontMetrics()->GetLineSpacing());
							break;
					}

					// Make sure we have at least 1 dot free space at each side of the rectangle
					pPainter->DrawTextAligned( dX + dCurX + 1 + dImageWidth, dY - dCurY + dVertical, 
                                               pdColWidths[i] - 2.0 - dImageWidth, m_pModel->GetText( i, j ), m_pModel->GetAlignment( i, j ) );
				}
                
                pPainter->Restore();
				if( bBorders ) // draw left x border
                {
                    // use always the border color of the left to the current cell
                    pPainter->SetStrokingColor( m_pModel->GetBorderColor( i>0 ? i-1 : i, j ) );
					pPainter->DrawLine( dX + dCurX, dY - dCurY, dX + dCurX, dY - dCurY + pdRowHeights[j] );
                }

		        dCurX += pdColWidths[i];    
            }

			if( bBorders ) 
			{
				// Draw last X border
                if( i > 0 )
                {
                    pPainter->SetStrokingColor( m_pModel->GetBorderColor( i-1, j ) );
				    pPainter->DrawLine( dX + dCurX, dY - dCurY, dX + dCurX, dY - dCurY + pdRowHeights[j] );
                }

                // draw border below row    
                this->DrawHorizontalBorders( j, dX, dY - dCurY, pPainter, pdColWidths );
    		}
		}    
	}
    pPainter->Restore();

    if( pdLastX )
        *pdLastX = dX + dWidth;

    if( pdLastY )
        *pdLastY = dY - dCurY;

    // Free allocated memory
    delete [] pdColWidths;
    delete [] pdRowHeights;
}