DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, int page_index, double width, double height) { if (!document) return NULL; // CPDF_Parser* pParser = (CPDF_Parser*)document; CPDF_Document* pDoc = (CPDF_Document*)document; if(page_index < 0) page_index = 0; if(pDoc->GetPageCount()<page_index) page_index = pDoc->GetPageCount(); // if (page_index < 0 || page_index >= pDoc->GetPageCount()) // return NULL; CPDF_Dictionary* pPageDict = pDoc->CreateNewPage(page_index); if(!pPageDict) return NULL; CPDF_Array* pMediaBoxArray = FX_NEW CPDF_Array; pMediaBoxArray->Add(FX_NEW CPDF_Number(0)); pMediaBoxArray->Add(FX_NEW CPDF_Number(0)); pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(width))); pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(height))); pPageDict->SetAt("MediaBox", pMediaBoxArray); pPageDict->SetAt("Rotate", FX_NEW CPDF_Number(0)); pPageDict->SetAt("Resources", FX_NEW CPDF_Dictionary); CPDF_Page* pPage = FX_NEW CPDF_Page; pPage->Load(pDoc,pPageDict); pPage->ParseContent(); return pPage; }
DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page, double a, double b, double c, double d, double e, double f) { if(page == NULL) return; CPDF_Page* pPage = (CPDF_Page*)page; CPDF_AnnotList AnnotList(pPage); for (int i=0; i<AnnotList.Count();i++) { CPDF_Annot* pAnnot = AnnotList.GetAt(i); // transformAnnots Rectangle CPDF_Rect rect; pAnnot->GetRect(rect); CFX_AffineMatrix matrix((FX_FLOAT)a,(FX_FLOAT)b,(FX_FLOAT)c,(FX_FLOAT)d,(FX_FLOAT)e,(FX_FLOAT)f); rect.Transform(&matrix); CPDF_Array *pRectArray = NULL; pRectArray = pAnnot->m_pAnnotDict->GetArray("Rect"); if (!pRectArray) pRectArray=CPDF_Array::Create(); pRectArray->SetAt(0,FX_NEW CPDF_Number(rect.left)); pRectArray->SetAt(1,FX_NEW CPDF_Number(rect.bottom)); pRectArray->SetAt(2,FX_NEW CPDF_Number(rect.right)); pRectArray->SetAt(3,FX_NEW CPDF_Number(rect.top)); pAnnot->m_pAnnotDict->SetAt("Rect",pRectArray); //Transform AP's rectangle //To Do } }
DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page, float left, float bottom, float right, float top) { if(!page) return; CPDF_Page* pPage = (CPDF_Page*)page; CPDF_Dictionary* pPageDict = pPage->m_pFormDict; CPDF_Array* pMediaBoxArray = FX_NEW CPDF_Array; pMediaBoxArray->Add(FX_NEW CPDF_Number(left)); pMediaBoxArray->Add(FX_NEW CPDF_Number(bottom)); pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(right))); pMediaBoxArray->Add(FX_NEW CPDF_Number(FX_FLOAT(top))); pPageDict->SetAt("MediaBox", pMediaBoxArray); }
void CPDF_StreamContentParser::EndNumber() { if (m_ObjectSize == 0) { AddNumberParam((FX_LPCSTR)m_pWordBuf, m_WordSize); return; } CPDF_Number *pObj = FX_NEW CPDF_Number(CFX_ByteStringC(m_pWordBuf, m_WordSize)); if (!SetToCurObj(pObj)) { pObj->Release(); } }
DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate) { CPDF_Page* pPage = (CPDF_Page*)page; if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect() || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page")) { return; } CPDF_Dictionary* pDict = pPage->m_pFormDict; rotate %=4; pDict->SetAt("Rotate", FX_NEW CPDF_Number(rotate * 90)); }
//border void CPDFSDK_Annot::SetBorderWidth(int nWidth) { ASSERT(m_pAnnot != NULL); ASSERT(m_pAnnot->m_pAnnotDict != NULL); CPDF_Array* pBorder = m_pAnnot->m_pAnnotDict->GetArray("Border"); if (pBorder) { pBorder->SetAt(2, FX_NEW CPDF_Number(nWidth)); } else { CPDF_Dictionary* pBSDict = m_pAnnot->m_pAnnotDict->GetDict("BS"); if (!pBSDict) { pBSDict = FX_NEW CPDF_Dictionary; m_pAnnot->m_pAnnotDict->SetAt("BS", pBSDict); } pBSDict->SetAtInteger("W", nWidth); } }
void CPDF_Dictionary::SetAtInteger(FX_BSTR key, int i) { SetAt(key, FX_NEW CPDF_Number(i)); }
void CPDF_Array::AddInteger(int i) { ASSERT(this != NULL && m_Type == PDFOBJ_ARRAY); Add(FX_NEW CPDF_Number(i)); }
CPDF_Object* CPDF_Object::CloneInternal(FX_BOOL bDirect, CFX_MapPtrToPtr* visited) const { if (this == NULL) { return NULL; } switch (m_Type) { case PDFOBJ_BOOLEAN: return FX_NEW CPDF_Boolean(((CPDF_Boolean*)this)->m_bValue); case PDFOBJ_NUMBER: return FX_NEW CPDF_Number(((CPDF_Number*)this)->m_bInteger, &((CPDF_Number*)this)->m_Integer); case PDFOBJ_STRING: return FX_NEW CPDF_String(((CPDF_String*)this)->m_String, ((CPDF_String*)this)->IsHex()); case PDFOBJ_NAME: return FX_NEW CPDF_Name(((CPDF_Name*)this)->m_Name); case PDFOBJ_ARRAY: { CPDF_Array* pCopy = FX_NEW CPDF_Array(); CPDF_Array* pThis = (CPDF_Array*)this; int n = pThis->GetCount(); for (int i = 0; i < n; i ++) { CPDF_Object* value = (CPDF_Object*)pThis->m_Objects.GetAt(i); pCopy->m_Objects.Add(value->CloneInternal(bDirect, visited)); } return pCopy; } case PDFOBJ_DICTIONARY: { CPDF_Dictionary* pCopy = FX_NEW CPDF_Dictionary(); CPDF_Dictionary* pThis = (CPDF_Dictionary*)this; FX_POSITION pos = pThis->m_Map.GetStartPosition(); while (pos) { CFX_ByteString key; CPDF_Object* value; pThis->m_Map.GetNextAssoc(pos, key, (void*&)value); pCopy->m_Map.SetAt(key, value->CloneInternal(bDirect, visited)); } return pCopy; } case PDFOBJ_NULL: { return FX_NEW CPDF_Null; } case PDFOBJ_STREAM: { CPDF_Stream* pThis = (CPDF_Stream*)this; CPDF_StreamAcc acc; acc.LoadAllData(pThis, TRUE); FX_DWORD streamSize = acc.GetSize(); CPDF_Stream* pObj = FX_NEW CPDF_Stream(acc.DetachData(), streamSize, (CPDF_Dictionary*)((CPDF_Object*)pThis->GetDict())->CloneInternal(bDirect, visited)); return pObj; } case PDFOBJ_REFERENCE: { CPDF_Reference* pRef = (CPDF_Reference*)this; FX_DWORD obj_num = pRef->m_RefObjNum; if (bDirect && !visited->GetValueAt((void*)(FX_UINTPTR)obj_num)) { visited->SetAt((void*)(FX_UINTPTR)obj_num, (void*)1); CPDF_Object* ret = pRef->GetDirect()->CloneInternal(TRUE, visited); return ret; } else { return FX_NEW CPDF_Reference(pRef->m_pObjList, obj_num); } } } return NULL; }
DLLEXPORT int STDCALL FPDFPage_Flatten( FPDF_PAGE page, int nFlag) { if (!page) { return FLATTEN_FAIL; } CPDF_Page * pPage = (CPDF_Page*)( page ); CPDF_Document * pDocument = pPage->m_pDocument; CPDF_Dictionary * pPageDict = pPage->m_pFormDict; if ( !pDocument || !pPageDict ) { return FLATTEN_FAIL; } CPDF_ObjectArray ObjectArray; CPDF_RectArray RectArray; int iRet = FLATTEN_FAIL; iRet = ParserAnnots( pDocument, pPageDict, &RectArray, &ObjectArray, nFlag); if (iRet == FLATTEN_NOTINGTODO) { return FLATTEN_NOTINGTODO; }else if (iRet == FLATTEN_FAIL) { return FLATTEN_FAIL; } CPDF_Rect rcOriginalCB; CPDF_Rect rcMerger = CalculateRect( &RectArray ); CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox"); if (pPageDict->KeyExist("CropBox")) rcOriginalMB = pPageDict->GetRect("CropBox"); if (rcOriginalMB.IsEmpty()) { rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f); } rcMerger.left = rcMerger.left < rcOriginalMB.left? rcOriginalMB.left : rcMerger.left; rcMerger.right = rcMerger.right > rcOriginalMB.right? rcOriginalMB.right : rcMerger.right; rcMerger.top = rcMerger.top > rcOriginalMB.top? rcOriginalMB.top : rcMerger.top; rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom? rcOriginalMB.bottom : rcMerger.bottom; if (pPageDict->KeyExist("ArtBox")) rcOriginalCB = pPageDict->GetRect("ArtBox"); else rcOriginalCB = rcOriginalMB; if (!rcOriginalMB.IsEmpty()) { CPDF_Array* pMediaBox = FX_NEW CPDF_Array(); pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.left)); pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.bottom)); pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.right)); pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.top)); pPageDict->SetAt("MediaBox",pMediaBox); } if (!rcOriginalCB.IsEmpty()) { CPDF_Array* pCropBox = FX_NEW CPDF_Array(); pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.left)); pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.bottom)); pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.right)); pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.top)); pPageDict->SetAt("ArtBox", pCropBox); } CPDF_Dictionary* pRes = NULL; pRes = pPageDict->GetDict("Resources"); if (!pRes) { pRes = FX_NEW CPDF_Dictionary; pPageDict->SetAt( "Resources", pRes ); } CPDF_Stream* pNewXObject = FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary); FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject); CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject"); if (!pPageXObject) { pPageXObject = FX_NEW CPDF_Dictionary; pRes->SetAt("XObject", pPageXObject); } CFX_ByteString key = ""; int nStreams = ObjectArray.GetSize(); if (nStreams > 0) { for (int iKey = 0; /*iKey < 100*/; iKey++) { char sExtend[5] = {0}; FXSYS_itoa(iKey, sExtend, 10); key = CFX_ByteString("FFT") + CFX_ByteString(sExtend); if (!pPageXObject->KeyExist(key)) break; } } SetPageContents(key, pPageDict, pDocument); CPDF_Dictionary* pNewXORes = NULL; if (!key.IsEmpty()) { pPageXObject->SetAtReference(key, pDocument, dwObjNum); CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict(); pNewXORes = FX_NEW CPDF_Dictionary; pNewOXbjectDic->SetAt("Resources", pNewXORes); pNewOXbjectDic->SetAtName("Type", "XObject"); pNewOXbjectDic->SetAtName("Subtype", "Form"); pNewOXbjectDic->SetAtInteger("FormType", 1); pNewOXbjectDic->SetAtName("Name", "FRM"); CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox"); pNewOXbjectDic->SetAtRect("BBox", rcBBox); } for (int i = 0; i < nStreams; i++) { CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i); if (!pAnnotDic)continue; CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect"); rcAnnot.Normalize(); CFX_ByteString sAnnotState = pAnnotDic->GetString("AS"); CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP"); if (!pAnnotAP)continue; CPDF_Stream* pAPStream = pAnnotAP->GetStream("N"); if (!pAPStream) { CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N"); if (!pAPDic)continue; if (!sAnnotState.IsEmpty()) { pAPStream = pAPDic->GetStream(sAnnotState); } else { FX_POSITION pos = pAPDic->GetStartPos(); if (pos) { CFX_ByteString sKey; CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey); if (pFirstObj) { if (pFirstObj->GetType() == PDFOBJ_REFERENCE) pFirstObj = pFirstObj->GetDirect(); if (pFirstObj->GetType() != PDFOBJ_STREAM) continue; pAPStream = (CPDF_Stream*)pFirstObj; } } } } if (!pAPStream)continue; CPDF_Dictionary* pAPDic = pAPStream->GetDict(); CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix"); CPDF_Rect rcStream; if (pAPDic->KeyExist("Rect")) rcStream = pAPDic->GetRect("Rect"); else if (pAPDic->KeyExist("BBox")) rcStream = pAPDic->GetRect("BBox"); if (rcStream.IsEmpty())continue; CPDF_Object* pObj = pAPStream; if (pObj) { CPDF_Dictionary* pObjDic = pObj->GetDict(); if (pObjDic) { pObjDic->SetAtName("Type", "XObject"); pObjDic->SetAtName("Subtype", "Form"); } } CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject"); if (!pXObject) { pXObject = FX_NEW CPDF_Dictionary; pNewXORes->SetAt("XObject", pXObject); } CFX_ByteString sFormName; sFormName.Format("F%d", i); FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj); pXObject->SetAtReference(sFormName, pDocument, dwObjNum); CPDF_StreamAcc acc; acc.LoadAllData(pNewXObject); FX_LPCBYTE pData = acc.GetData(); CFX_ByteString sStream(pData, acc.GetSize()); CFX_ByteString sTemp; if (matrix.IsIdentity()) { matrix.a = 1.0f; matrix.b = 0.0f; matrix.c = 0.0f; matrix.d = 1.0f; matrix.e = 0.0f; matrix.f = 0.0f; } CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix); sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f, sFormName.c_str()); sStream += sTemp; pNewXObject->SetData((FX_LPCBYTE)sStream, sStream.GetLength(), FALSE, FALSE); } pPageDict->RemoveAt( "Annots" ); ObjectArray.RemoveAll(); RectArray.RemoveAll(); return FLATTEN_SUCCESS; }