Skip to content

Commit

Permalink
bug fixes...and now i got PDF embedding on :))))))
Browse files Browse the repository at this point in the history
  • Loading branch information
galkahana committed Apr 12, 2011
1 parent df2b4ec commit 28a53bc
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
2 changes: 1 addition & 1 deletion PDFWriter/PDFArray.cpp
Expand Up @@ -26,7 +26,7 @@ SingleValueContainerIterator<PDFObjectVector> PDFArray::GetIterator()

PDFObject* PDFArray::QueryObject(unsigned long i)
{
if(mValues.size() >= i)
if(mValues.size() <= i)
{
return NULL;
}
Expand Down
31 changes: 25 additions & 6 deletions PDFWriter/PDFDocumentHandler.cpp
Expand Up @@ -289,18 +289,31 @@ EStatusCode PDFDocumentHandler::WritePageContentToSingleStream(IByteWriter* inTa
{
EStatusCode status = eSuccess;

RefCountPtr<PDFObject> pageContent(mParser.QueryDictionaryObject(inPageObject,"Content"));
RefCountPtr<PDFObject> pageContent(mParser.QueryDictionaryObject(inPageObject,"Contents"));
if(pageContent->GetType() == ePDFObjectStream)
{
status = WritePDFStreamInputToStream(inTargetStream,(PDFStreamInput*)pageContent.GetPtr());
}
else if(pageContent->GetType() == ePDFObjectArray)
{
SingleValueContainerIterator<PDFObjectVector> it = ((PDFArray*)pageContent.GetPtr())->GetIterator();
PDFObjectCastPtr<PDFStreamInput> contentStream;
PDFObjectCastPtr<PDFIndirectObjectReference> refItem;
while(it.MoveNext() && status == eSuccess)
{
contentStream = it.GetItem();
refItem = it.GetItem();
if(!refItem)
{
status = eFailure;
TRACE_LOG("PDFDocumentHandler::WritePageContentToSingleStream, content stream array contains non-refs");
break;
}
PDFObjectCastPtr<PDFStreamInput> contentStream(mParser.ParseNewObject(refItem->mObjectID));
if(!contentStream)
{
status = eFailure;
TRACE_LOG("PDFDocumentHandler::WritePageContentToSingleStream, content stream array contains references to non streams");
break;
}
status = WritePDFStreamInputToStream(inTargetStream,contentStream.GetPtr());
if(eSuccess == status)
{
Expand Down Expand Up @@ -391,10 +404,14 @@ EStatusCode PDFDocumentHandler::WriteNewObjects(const ObjectIDTypeList& inSource
// copied, so make sure to check that these objects are still required for copying
if(ioCopiedObjects.find(*itNewObjects) == ioCopiedObjects.end())
{
ObjectIDType newObjectID = mObjectsContext->GetInDirectObjectsRegistry().AllocateNewObjectID();
mSourceToTarget.insert(ObjectIDTypeToObjectIDTypeMap::value_type(*itNewObjects,newObjectID));
ObjectIDTypeToObjectIDTypeMap::iterator it = mSourceToTarget.find(*itNewObjects);
if(it == mSourceToTarget.end())
{
ObjectIDType newObjectID = mObjectsContext->GetInDirectObjectsRegistry().AllocateNewObjectID();
it = mSourceToTarget.insert(ObjectIDTypeToObjectIDTypeMap::value_type(*itNewObjects,newObjectID)).first;
}
ioCopiedObjects.insert(*itNewObjects);
status = CopyInDirectObject(*itNewObjects,newObjectID,ioCopiedObjects);
status = CopyInDirectObject(*itNewObjects,it->second,ioCopiedObjects);
}
}
return status;
Expand Down Expand Up @@ -532,6 +549,7 @@ EStatusCode PDFDocumentHandler::WriteObjectByType(PDFObject* inObject,ETokenSepa
outSourceObjectsToAdd.push_back(sourceObjectID);
}
mObjectsContext->WriteIndirectObjectReference(itObjects->second,inSeparator);
break;
}
case ePDFObjectArray:
{
Expand Down Expand Up @@ -651,6 +669,7 @@ EStatusCode PDFDocumentHandler::WriteObjectByType(PDFObject* inObject,Dictionary
outSourceObjectsToAdd.push_back(sourceObjectID);
}
inDictionaryContext->WriteObjectReferenceValue(itObjects->second);
break;
}
case ePDFObjectArray:
{
Expand Down
105 changes: 105 additions & 0 deletions PDFWriterTestPlayground/PDFEmbedTest.cpp
@@ -0,0 +1,105 @@
#include "PDFEmbedTest.h"
#include "TestsRunner.h"
#include "PDFWriter.h"
#include "PageContentContext.h"
#include "PDFPage.h"
#include "PDFFormXObject.h"

#include <iostream>

using namespace std;

PDFEmbedTest::PDFEmbedTest(void)
{
}

PDFEmbedTest::~PDFEmbedTest(void)
{
}


EStatusCode PDFEmbedTest::Run()
{
EStatusCode status;
PDFWriter pdfWriter;

do
{
status = pdfWriter.StartPDF(L"C:\\PDFLibTests\\PDFEmbedTest.PDF",ePDFVersion13);
if(status != eSuccess)
{
wcout<<"failed to start PDF\n";
break;
}

// Create XObjects from PDF to embed
EStatusCodeAndPDFFormXObjectList result = pdfWriter.CreateFormXObjectsFromPDF(L"C:\\PDFLibTests\\TestMaterials\\XObjectContent.PDF",PDFPageRange(),ePDFPageBoxMediaBox);
if(result.first != eSuccess)
{
wcout<<"failed to create PDF XObjects from PDF file\n";
status = result.first;
break;
}

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

PageContentContext* contentContext = pdfWriter.StartPageContentContext(page);

// place the first page in the top left corner of the document
contentContext->q();
contentContext->cm(0.5,0,0,0.5,0,421);
contentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(result.second.front()->GetObjectID()));
contentContext->Q();

contentContext->G(0);
contentContext->w(1);
contentContext->re(0,421,297.5,421);
contentContext->S();


// place the second page in the bottom right corner of the document
contentContext->q();
contentContext->cm(0.5,0,0,0.5,297.5,0);
contentContext->Do(page->GetResourcesDictionary().AddFormXObjectMapping(result.second.back()->GetObjectID()));
contentContext->Q();

contentContext->G(0);
contentContext->w(1);
contentContext->re(297.5,0,297.5,421);
contentContext->S();


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

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

// delete all form xobjects now
PDFFormXObjectList::iterator it = result.second.begin();
for(; it != result.second.end();++it)
delete (*it);
result.second.clear();

status = pdfWriter.EndPDF();
if(status != eSuccess)
{
wcout<<"failed in end PDF\n";
break;
}

}while(false);

return status;
}

ADD_CETEGORIZED_TEST(PDFEmbedTest,"PDFEmbedding")
11 changes: 11 additions & 0 deletions PDFWriterTestPlayground/PDFEmbedTest.h
@@ -0,0 +1,11 @@
#pragma once
#include "ITestUnit.h"

class PDFEmbedTest : public ITestUnit
{
public:
PDFEmbedTest(void);
virtual ~PDFEmbedTest(void);

virtual EStatusCode Run();
};
8 changes: 8 additions & 0 deletions PDFWriterTestPlayground/PDFWriterTestPlayground.vcproj
Expand Up @@ -656,6 +656,14 @@
RelativePath=".\ImportantReadme.txt"
>
</File>
<File
RelativePath=".\PDFEmbedTest.cpp"
>
</File>
<File
RelativePath=".\PDFEmbedTest.h"
>
</File>
</Files>
<Globals>
</Globals>
Expand Down

0 comments on commit 28a53bc

Please sign in to comment.