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 UseCopyingContext() { PDFWriter pdfWriter; EStatusCode status; PDFDocumentCopyingContext* firstContext = NULL; PDFDocumentCopyingContext* secondContext = NULL; do { status = pdfWriter.StartPDF(scBasePath + "UseCopyingContext.PDF",ePDFVersion13); if(status != eSuccess) break; firstContext = pdfWriter.CreatePDFCopyingContext(scBasePath + "XObjectContent.PDF"); if(!firstContext) { status = eFailure; break; } secondContext = pdfWriter.CreatePDFCopyingContext(scBasePath + "BasicTIFFImagesTest.PDF"); if(!firstContext) { status = eFailure; break; } // appending pages, first from XObjectContent, then from BasicTIFFImageTest, and then again from XObjectContent status = firstContext->AppendPDFPageFromPDF(0).first; if(status != eSuccess) break; status = secondContext->AppendPDFPageFromPDF(0).first; if(status != eSuccess) break; status = firstContext->AppendPDFPageFromPDF(1).first; if(status != eSuccess) break; // placing pages as xobjects, from multiple PDFs EStatusCodeAndObjectIDType resultFirst = firstContext->CreateFormXObjectFromPDFPage(0,ePDFPageBoxMediaBox); if(resultFirst.first != eSuccess) { status = eFailure; break; } EStatusCodeAndObjectIDType resultSecond = secondContext->CreateFormXObjectFromPDFPage(0,ePDFPageBoxMediaBox); if(resultSecond.first != eSuccess) { status = eFailure; break; } PDFPage* page = new PDFPage(); page->SetMediaBox(PDFRectangle(0,0,595,842)); PageContentContext* contentContext = pdfWriter.StartPageContentContext(page); // placing the pages in a result page contentContext->q(); contentContext->cm(0.5,0,0,0.5,0,421); contentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(resultFirst.second)); contentContext->Q(); contentContext->q(); contentContext->cm(0.5,0,0,0.5,297.5,0); contentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(resultSecond.second)); contentContext->Q(); status = pdfWriter.EndPageContentContext(contentContext); if(status != eSuccess) break; status = pdfWriter.WritePageAndRelease(page); if(status != eSuccess) break; status = pdfWriter.EndPDF(); if(status != eSuccess) break; }while(false); delete firstContext; delete secondContext; return status; }
EStatusCode PDFCopyingContextTest::Run() { EStatusCode status; PDFWriter pdfWriter; PDFDocumentCopyingContext* copyingContext = NULL; do { status = pdfWriter.StartPDF("C:\\PDFLibTests\\PDFCopyingContextTest.PDF",ePDFVersion13,LogConfiguration(true,true,"c:\\pdflibtests\\PDFCopyingContextTest.txt")); if(status != PDFHummus::eSuccess) { cout<<"failed to start PDF\n"; break; } copyingContext = pdfWriter.CreatePDFCopyingContext("C:\\PDFLibTests\\TestMaterials\\BasicTIFFImagesTest.PDF"); if(!copyingContext) { cout<<"failed to initialize copying context from BasicTIFFImagesTest\n"; status = PDFHummus::eFailure; break; } EStatusCodeAndObjectIDType result = copyingContext->AppendPDFPageFromPDF(1); if(result.first != PDFHummus::eSuccess) { cout<<"failed to append page 1 from BasicTIFFImagesTest.PDF\n"; status = result.first; break; } result = copyingContext->AppendPDFPageFromPDF(18); if(result.first != PDFHummus::eSuccess) { cout<<"failed to append page 18 from BasicTIFFImagesTest.PDF\n"; status = result.first; break; } result = copyingContext->AppendPDFPageFromPDF(4); if(result.first != PDFHummus::eSuccess) { cout<<"failed to append page 4 from BasicTIFFImagesTest.PDF\n"; status = result.first; break; } copyingContext->End(); // delete will call End() as well...so can avoid delete copyingContext; copyingContext = NULL; status = pdfWriter.EndPDF(); if(status != PDFHummus::eSuccess) { cout<<"failed in end PDF\n"; break; } }while(false); delete copyingContext; return status; }