Пример #1
0
EStatusCode MergeToPDFForm::Run(const TestConfiguration& inTestConfiguration)
{
	PDFWriter pdfWriter;
	EStatusCode status;
    PDFDocumentCopyingContext* copyingContext = NULL;
    
	do
	{
		status = pdfWriter.StartPDF(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"MergeToPDFForm.pdf"),
                                    ePDFVersion13,
                                    LogConfiguration(true,true,RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"MergeToPDFForm.txt")));
		if(status != PDFHummus::eSuccess)
			break;
        
        
        // in this test we will merge 2 pages into a PDF form, and place it twice, forming a 2X2 design. amazing.
        
		PDFPage* page = new PDFPage();
		page->SetMediaBox(PDFRectangle(0,0,595,842));
        
        
        copyingContext = pdfWriter.CreatePDFCopyingContext(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"TestMaterials/Linearized.pdf"));
        if(status != PDFHummus::eSuccess)
            break;
        
        // create form for two pages.
        PDFFormXObject* newFormXObject = pdfWriter.StartFormXObject(PDFRectangle(0,0,297.5,842));
        
		XObjectContentContext* xobjectContentContext = newFormXObject->GetContentContext();
        
        xobjectContentContext->q();
        xobjectContentContext->cm(0.5,0,0,0.5,0,0);
        status = copyingContext->MergePDFPageToFormXObject(newFormXObject,1);
        if(status != eSuccess)
            break;
        xobjectContentContext->Q();
        
        xobjectContentContext->q();
        xobjectContentContext->cm(0.5,0,0,0.5,0,421);
        status = copyingContext->MergePDFPageToFormXObject(newFormXObject,0);
        if(status != eSuccess)
            break;
        xobjectContentContext->Q();
        
        ObjectIDType formID = newFormXObject->GetObjectID();
        status = pdfWriter.EndFormXObjectAndRelease(newFormXObject);
        if(status != eSuccess)
            break;
        
        // now place it in the page
        PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(page);
        
        
        string formName = page->GetResourcesDictionary().AddFormXObjectMapping(formID);
  
        pageContentContext->q();
        pageContentContext->Do(formName);
        pageContentContext->cm(1,0,0,1,297.5,0);
        pageContentContext->Do(formName);
        pageContentContext->Q();
        
        status = pdfWriter.EndPageContentContext(pageContentContext);
        if(status != eSuccess)
            break;
        
		status = pdfWriter.WritePageAndRelease(page);
		if(status != PDFHummus::eSuccess)
			break;
        
		status = pdfWriter.EndPDF();
		if(status != PDFHummus::eSuccess)
			break;
	}while(false);
    
    delete copyingContext;
	
	return status;
}
EStatusCode ImagesAndFormsForwardReferenceTest::Run(const TestConfiguration& inTestConfiguration)
{
	PDFWriter pdfWriter;
	EStatusCode status; 

	do
	{

		status = pdfWriter.StartPDF(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"ImagesAndFormsForwardReferenceTest.PDF"),ePDFVersion13);
		if(status != PDFHummus::eSuccess)
		{
			cout<<"failed to start PDF\n";
			break;
		}	

		PDFPage* page = new PDFPage();
		page->SetMediaBox(PDFRectangle(0,0,595,842));

		PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(page);
		if(NULL == pageContentContext)
		{
			status = PDFHummus::eFailure;
			cout<<"failed to create content context for page\n";
		}



		// continue page drawing size the image to 500,400
		pageContentContext->q();
		pageContentContext->cm(500,0,0,400,0,0);
		
		ObjectIDType imageXObjectID = pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID();
		pageContentContext->Do(page->GetResourcesDictionary().AddImageXObjectMapping(imageXObjectID));

		// optionally i can also add the necessary PDF Procsets. i'll just add all that might be relevant
		page->GetResourcesDictionary().AddProcsetResource(KProcsetImageB);
		page->GetResourcesDictionary().AddProcsetResource(KProcsetImageC);
		page->GetResourcesDictionary().AddProcsetResource(KProcsetImageI);

		pageContentContext->Q();

		// continue page drawing size the image to 500,400
		pageContentContext->q();
		pageContentContext->cm(1,0,0,1,0,400);
		ObjectIDType formXObjectID = pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID();
		pageContentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(formXObjectID));
		pageContentContext->Q();

#ifndef NO_TIFF
		pageContentContext->q();
		ObjectIDType tiffFormXObjectID = pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID();
		pageContentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(tiffFormXObjectID));
		pageContentContext->Q();
#endif
        
		pageContentContext->q();
		pageContentContext->cm(1,0,0,1,100,500);
		ObjectIDType simpleFormXObjectID = pdfWriter.GetObjectsContext().GetInDirectObjectsRegistry().AllocateNewObjectID();
		pageContentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(simpleFormXObjectID));
		pageContentContext->Q();

		status = pdfWriter.EndPageContentContext(pageContentContext);
		if(status != PDFHummus::eSuccess)
		{
			cout<<"failed to end page content context\n";
			break;
		}

		status = pdfWriter.WritePageAndRelease(page);
		if(status != PDFHummus::eSuccess)
		{
			cout<<"failed to write page\n";
			break;
		}


		// Create image xobject  
		PDFImageXObject* imageXObject  = pdfWriter.CreateImageXObjectFromJPGFile(RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"TestMaterials/images/otherStage.JPG"),imageXObjectID);
		if(!imageXObject)
		{
			cout<<"failed to create image XObject from file\n";
			status = PDFHummus::eFailure;
			break;
		}

		// now create form xobject
		PDFFormXObject*  formXObject = pdfWriter.CreateFormXObjectFromJPGFile(
            RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"TestMaterials/images/otherStage.JPG"),formXObjectID);
		if(!formXObject)
		{
			cout<<"failed to create form XObject from file\n";
			status = PDFHummus::eFailure;
			break;
		}

#ifndef NO_TIFF
		PDFFormXObject* tiffFormXObject = pdfWriter.CreateFormXObjectFromTIFFFile(
            RelativeURLToLocalPath(inTestConfiguration.mSampleFileBase,"TestMaterials/images/tiff/jim___ah.tif"),tiffFormXObjectID);
		if(!tiffFormXObject)
		{
			cout<<"failed to create image form XObject from file, for file\n";
			status = PDFHummus::eFailure;
			break;
		}
		delete tiffFormXObject;
#endif

		delete imageXObject;
		delete formXObject;


		// define an xobject form to draw a 200X100 points red rectangle
		PDFFormXObject* xobjectForm = pdfWriter.StartFormXObject(PDFRectangle(0,0,200,100),simpleFormXObjectID);

		XObjectContentContext* xobjectContentContext = xobjectForm->GetContentContext();
		xobjectContentContext->q();
		xobjectContentContext->k(0,100,100,0);
		xobjectContentContext->re(0,0,200,100);
		xobjectContentContext->f();
		xobjectContentContext->Q();

		status = pdfWriter.EndFormXObjectAndRelease(xobjectForm);
		if(status != PDFHummus::eSuccess)
		{
			cout<<"failed to write XObject form\n";
			break;
		}


		status = pdfWriter.EndPDF();
		if(status != PDFHummus::eSuccess)
		{
			cout<<"failed in end PDF\n";
			break;
		}
	}while(false);
	return status;		
}
Пример #3
0
int main(int argc, char* argv[])
{
	PDFWriter pdfWriter;
	EStatusCode status;

	do
	{
		// Initial Setup for file,page and page content
		status = pdfWriter.StartPDF(scBasePath + "DrawingShapes.pdf",ePDFVersion13);
		if(status != eSuccess)
			break;

		PDFPage* pdfPage = new PDFPage();
		pdfPage->SetMediaBox(PDFRectangle(0,0,595,842));
		PageContentContext* pageContentContext = pdfWriter.StartPageContentContext(pdfPage);

		// Start adding content to page

		// Draw a Line, stroke
		pageContentContext->q();
		pageContentContext->w(2);
		pageContentContext->K(0,0,1,0);

		pageContentContext->m(10,500);
		pageContentContext->l(30,700);
		pageContentContext->s();

		pageContentContext->Q();

		// Draw a Polygon, stroke
		pageContentContext->q();
		pageContentContext->w(2);
		pageContentContext->K(0,1,0,0);

		pageContentContext->m(40,500);
		pageContentContext->l(60,700);
		pageContentContext->l(160,700);
		pageContentContext->l(140,500);
		pageContentContext->s();

		pageContentContext->Q();

		// Draw a Rectangle, fill
		pageContentContext->q();
		pageContentContext->k(0,1,0,0);

		pageContentContext->re(200,400,100,300);
		pageContentContext->f();

		pageContentContext->Q();

		// Pausing content context, to allow form definition
		pdfWriter.PausePageContentContext(pageContentContext);

		// Create a form with a triangle pattern, RGB colorspace, fill
		PDFFormXObject* formXObject = pdfWriter.StartFormXObject(PDFRectangle(0,0,400,400));
		XObjectContentContext* xobjectContent = formXObject->GetContentContext();

		xobjectContent->rg(1,0,0);
		xobjectContent->m(0,0);
		xobjectContent->l(200,400);
		xobjectContent->l(400,0);
		xobjectContent->f();

		ObjectIDType formObjectID = formXObject->GetObjectID();
		status = pdfWriter.EndFormXObjectAndRelease(formXObject);
		if(status != eSuccess)
			break;

		// Place the form in multiple locations in the page
		string formNameInPage = pdfPage->GetResourcesDictionary().AddFormXObjectMapping(formObjectID);

		pageContentContext->q();
		pageContentContext->cm(0.5,0,0,0.5,120,100);
		pageContentContext->Do(formNameInPage);
		pageContentContext->Q();

		pageContentContext->q();
		pageContentContext->cm(0.2,0,0,0.2,350,100);
		pageContentContext->Do(formNameInPage);
		pageContentContext->Q();

		// End adding content to page

		// end page content, write page object and finalize PDF
		status = pdfWriter.EndPageContentContext(pageContentContext);
		if(status != eSuccess)
			break;

		status = pdfWriter.WritePageAndRelease(pdfPage);
		if(status != eSuccess)
			break;

		status = pdfWriter.EndPDF();
	}while(false);

	if(eSuccess == status)
		cout<<"Succeeded in creating PDF file\n";
	else
		cout<<"Failed in creating PDF file\n";

	return 0;
}