Ejemplo n.º 1
0
BoxSetter::BoxSetter(const std::string& in, const std::string& out, const std::string& box, const PoDoFo::PdfRect& rect)
	:m_box(box), m_rect(rect)
{
	PoDoFo::PdfMemDocument* source = new PoDoFo::PdfMemDocument(in.c_str());
	int pcount(source->GetPageCount());
	for ( int i = 0; i < pcount ; ++i )
	{
		SetBox(source->GetPage ( i ));
	}

	source->Write(out.c_str());

}
Ejemplo n.º 2
0
void ImageConverter::Work() 
{
    PoDoFo::PdfMemDocument document;

    std::vector<std::string>::const_iterator it = m_vecImages.begin();
    PoDoFo::PdfRect size = PoDoFo::PdfPage::CreateStandardPageSize( PoDoFo::ePdfPageSize_A4, false );
    PoDoFo::PdfPainter painter;
    double dScaleX = 1.0;
    double dScaleY = 1.0;
    double dScale  = 1.0;

    while( it != m_vecImages.end() ) 
    {
        PoDoFo::PdfPage* pPage;
        PoDoFo::PdfImage image( &document );
        image.LoadFromFile( (*it).c_str() );

        if( m_bUseImageSize ) 
        {
            size = PoDoFo::PdfRect( 0.0, 0.0, image.GetWidth(), image.GetHeight() );
        }

        pPage = document.CreatePage( size );
        dScaleX = size.GetWidth() / image.GetWidth();
        dScaleY = size.GetHeight() / image.GetHeight();
        dScale  = PoDoFo::PDF_MIN( dScaleX, dScaleY );

        painter.SetPage( pPage );
        if( dScale < 1.0 ) 
        {
            painter.DrawImage( 0.0, 0.0, &image, dScale, dScale );
        }
        else
        {
            // Center Image
            double dX = (size.GetWidth() - image.GetWidth())/2.0;
            double dY = (size.GetHeight() - image.GetHeight())/2.0;
            painter.DrawImage( dX, dY, &image );
        }

        painter.FinishPage();

        ++it;
    }

    document.Write( m_sOutputFilename.c_str() );
}
Ejemplo n.º 3
0
int main (int argc, char *argv[])
{
    using namespace PoDoFo;
    PoDoFo::PdfMemDocument *doc = NULL;
    int result = 0;

    try {
        PoDoFo::PdfError::EnableDebug(false);
        if (argc != 2 && argc != 4)
        {
            cout << "Syntax" << endl;
            cout << "  " << argv[0] << " <pdf file> - display the XMP in a file (use \"-\" to specify stdin)" << endl;
            cout << "or" << endl;
            cout << "  " << argv[0] << " <src pdf file> <xmp file> <new pdf file> - create a new PDF with the XMP in" << endl;
            return EXIT_FAILURE;
        }

        if ( string("-") == argv[1] )
        {
            cin >> std::noskipws;
#ifdef _MSC_VER
            _setmode(_fileno(stdin), _O_BINARY); // @TODO: MSVC specific binary setmode -- not sure if other platforms need it
            cin.sync_with_stdio();
#endif
            istream_iterator<char> it(std::cin);
            istream_iterator<char> end;
            string buffer(it, end);
            doc = new PoDoFo::PdfMemDocument();
            doc->Load( buffer.c_str(), (long)buffer.size() );
        }
        else
        {
            doc = new PoDoFo::PdfMemDocument(argv[1]);
        }


        if (argc == 2)
        {
            PoDoFo::PdfObject *metadata;
            if ((metadata = doc->GetMetadata()) == NULL)
                cout << "No metadata" << endl;
            else
            {
                PoDoFo::PdfStream *str = metadata->GetStream();
                if (str != NULL)
                {
                    char *buf;
                    PoDoFo::pdf_long len;

                    str->GetFilteredCopy(&buf, &len);
                    for (PoDoFo::pdf_long i = 0; i < len; ++i)
                        printf("%c", buf[i]);
                    printf("\n");
                    fflush(stdout);
                    free(buf);
                }
            }
        }

        if (argc == 4)
        {
            char *xmpBuf;
            FILE *fp;

            if ((fp = fopen(argv[2], "rb")) == NULL)
                cout << "Cannot open " << argv[2] << endl;
            else
            {
                if( fseek( fp, 0, SEEK_END ) == -1 )
                {
                    fclose( fp );
                    PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to seek to the end of the file" );
                }

                long xmpLen = ftell(fp);
                if( xmpLen == -1 )
                {
                    fclose( fp );
                    PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to read size of the file" );
                }

                xmpBuf = new char[xmpLen];
                if( !xmpBuf )
                {
                    fclose( fp );
                    PODOFO_RAISE_ERROR( ePdfError_OutOfMemory );
                }

                if( fseek( fp, 0, SEEK_SET ) == -1 )
                {
                    delete [] xmpBuf;
                    fclose( fp );

                    PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to seek to the beginning of the file" );
                }

                if( static_cast<long>( fread( xmpBuf, 1, xmpLen, fp ) ) != xmpLen )
                {
                    delete [] xmpBuf;
                    fclose( fp );

                    PODOFO_RAISE_ERROR_INFO( ePdfError_InvalidDeviceOperation, "Failed to read whole file into the memory" );
                }

                PoDoFo::PdfObject *metadata;
                if ((metadata = doc->GetMetadata()) != NULL)
                    metadata->GetStream()->Set(xmpBuf, xmpLen, PoDoFo::TVecFilters());
                else
                {
                    metadata = doc->GetObjects().CreateObject("Metadata");
                    metadata->GetDictionary().AddKey(PoDoFo::PdfName("Subtype"), PoDoFo::PdfName("XML"));
                    metadata->GetStream()->Set(xmpBuf, xmpLen, PoDoFo::TVecFilters());
                    doc->GetCatalog()->GetDictionary().AddKey(PoDoFo::PdfName("Metadata"), metadata->Reference());
                }
                delete[] xmpBuf;

                doc->Write(argv[3]);
            }
        }
    } catch( PdfError & e ) {
Ejemplo n.º 4
0
int main (int argc, char *argv[])
{
	PoDoFo::PdfError::EnableDebug(false);
	if (argc != 2 && argc != 4)
    {
		cout << "Syntax" << endl;
		cout << "  " << argv[0] << " <pdf file> - display the XMP in a file" << endl;
		cout << "or" << endl;
		cout << "  " << argv[0] << " <src pdf file> <xmp file> <new pdf file> - create a new PDF with the XMP in" << endl;
		return EXIT_FAILURE;
    }

	PoDoFo::PdfMemDocument *doc = new PoDoFo::PdfMemDocument(argv[1]);

	if (argc == 2)
    {
		PoDoFo::PdfObject *metadata;
		if ((metadata = doc->GetMetadata()) == NULL)
			cout << "No metadata" << endl;
		else
        {
			PoDoFo::PdfStream *str = metadata->GetStream();
			if (str != NULL)
            {
				char *buf;
				PoDoFo::pdf_long len;
	
				str->GetFilteredCopy(&buf, &len);
				for (PoDoFo::pdf_long i = 0; i < len; ++i)
					printf("%c", buf[i]);
				printf("\n");
				fflush(stdout);
				free(buf);
            }
        }
    }

	if (argc == 4)
    {
		char *xmpBuf;
		FILE *fp;

		if ((fp = fopen(argv[2], "rb")) == NULL)
			cout << "Cannot open " << argv[2] << endl;
		else
        {
			fseek(fp, 0, SEEK_END);
			long xmpLen = ftell(fp);
			xmpBuf = new char[xmpLen];
			fseek(fp, 0, SEEK_SET);
			fread(xmpBuf, 1, xmpLen, fp);
			fclose(fp);

			PoDoFo::PdfObject *metadata;
			if ((metadata = doc->GetMetadata()) != NULL)
				metadata->GetStream()->Set(xmpBuf, xmpLen, PoDoFo::TVecFilters());
			else
            {
				metadata = doc->GetObjects().CreateObject("Metadata");
				metadata->GetDictionary().AddKey(PoDoFo::PdfName("Subtype"), PoDoFo::PdfName("XML"));
				metadata->GetStream()->Set(xmpBuf, xmpLen, PoDoFo::TVecFilters());
				doc->GetCatalog()->GetDictionary().AddKey(PoDoFo::PdfName("Metadata"), metadata->Reference());
            }
			delete[] xmpBuf;

			doc->Write(argv[3]);
        }
    }

	delete doc;

	return EXIT_SUCCESS;
}