blob: 5514b2b32b283107acfe0eff463a757c1efb9b8e [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez1ed8a212015-05-11 15:25:39 -07007#include "../../public/fpdf_flatten.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07008#include "../include/fsdk_define.h"
Bo Xufdc00a72014-10-28 23:03:33 -07009#include "../include/fpdfxfa/fpdfxfa_doc.h"
10#include "../include/fpdfxfa/fpdfxfa_page.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070011
12typedef CFX_ArrayTemplate<CPDF_Dictionary*> CPDF_ObjectArray;
13typedef CFX_ArrayTemplate<CPDF_Rect> CPDF_RectArray;
14
15enum FPDF_TYPE { MAX, MIN };
16enum FPDF_VALUE { TOP, LEFT, RIGHT, BOTTOM };
17
Nico Weber9d8ec5a2015-08-04 13:00:21 -070018FX_BOOL IsValiableRect(CPDF_Rect rect, CPDF_Rect rcPage) {
19 if (rect.left - rect.right > 0.000001f || rect.bottom - rect.top > 0.000001f)
20 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070021
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 if (rect.left == 0.0f && rect.top == 0.0f && rect.right == 0.0f &&
23 rect.bottom == 0.0f)
24 return FALSE;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070025
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 if (!rcPage.IsEmpty()) {
27 if (rect.left - rcPage.left < -10.000001f ||
28 rect.right - rcPage.right > 10.000001f ||
29 rect.top - rcPage.top > 10.000001f ||
30 rect.bottom - rcPage.bottom < -10.000001f)
31 return FALSE;
32 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035}
36
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037FX_BOOL GetContentsRect(CPDF_Document* pDoc,
38 CPDF_Dictionary* pDict,
39 CPDF_RectArray* pRectArray) {
40 CPDF_Page* pPDFPage = FX_NEW CPDF_Page;
41 pPDFPage->Load(pDoc, pDict, FALSE);
42 pPDFPage->ParseContent();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 FX_POSITION pos = pPDFPage->GetFirstObjectPosition();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 while (pos) {
47 CPDF_PageObject* pPageObject = pPDFPage->GetNextObject(pos);
48 if (!pPageObject)
49 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 CPDF_Rect rc;
52 rc.left = pPageObject->m_Left;
53 rc.right = pPageObject->m_Right;
54 rc.bottom = pPageObject->m_Bottom;
55 rc.top = pPageObject->m_Top;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070056
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 if (IsValiableRect(rc, pDict->GetRect("MediaBox"))) {
58 pRectArray->Add(rc);
Tom Sepez3c3201f2015-05-20 10:20:35 -070059 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
61
62 delete pPDFPage;
63 return TRUE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
65
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066void ParserStream(CPDF_Dictionary* pPageDic,
67 CPDF_Dictionary* pStream,
68 CPDF_RectArray* pRectArray,
69 CPDF_ObjectArray* pObjectArray) {
70 if (!pStream)
71 return;
72 CPDF_Rect rect;
73 if (pStream->KeyExist("Rect"))
74 rect = pStream->GetRect("Rect");
75 else if (pStream->KeyExist("BBox"))
76 rect = pStream->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 if (IsValiableRect(rect, pPageDic->GetRect("MediaBox")))
79 pRectArray->Add(rect);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 pObjectArray->Add(pStream);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084int ParserAnnots(CPDF_Document* pSourceDoc,
85 CPDF_Dictionary* pPageDic,
86 CPDF_RectArray* pRectArray,
87 CPDF_ObjectArray* pObjectArray,
88 int nUsage) {
89 if (!pSourceDoc || !pPageDic)
90 return FLATTEN_FAIL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 GetContentsRect(pSourceDoc, pPageDic, pRectArray);
93 CPDF_Array* pAnnots = pPageDic->GetArray("Annots");
94 if (!pAnnots)
95 return FLATTEN_NOTHINGTODO;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070096
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 FX_DWORD dwSize = pAnnots->GetCount();
98 for (int i = 0; i < (int)dwSize; i++) {
99 CPDF_Object* pObj = pAnnots->GetElementValue(i);
100 if (!pObj || pObj->GetType() != PDFOBJ_DICTIONARY)
101 continue;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 CPDF_Dictionary* pAnnotDic = (CPDF_Dictionary*)pObj;
104 CFX_ByteString sSubtype = pAnnotDic->GetString("Subtype");
105 if (sSubtype == "Popup")
106 continue;
107
108 int nAnnotFlag = pAnnotDic->GetInteger("F");
109 if (nAnnotFlag & ANNOTFLAG_HIDDEN)
110 continue;
111
112 if (nUsage == FLAT_NORMALDISPLAY) {
113 if (nAnnotFlag & ANNOTFLAG_INVISIBLE)
114 continue;
115
116 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
117 } else {
118 if (nAnnotFlag & ANNOTFLAG_PRINT)
119 ParserStream(pPageDic, pAnnotDic, pRectArray, pObjectArray);
120 }
121 }
122 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700123}
124
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125FX_FLOAT GetMinMaxValue(CPDF_RectArray& array,
126 FPDF_TYPE type,
127 FPDF_VALUE value) {
128 int nRects = array.GetSize();
129 FX_FLOAT fRet = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700130
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 if (nRects <= 0)
132 return 0.0f;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 FX_FLOAT* pArray = new FX_FLOAT[nRects];
135 switch (value) {
136 case LEFT: {
137 for (int i = 0; i < nRects; i++)
138 pArray[i] = CPDF_Rect(array.GetAt(i)).left;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700139
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 break;
141 }
142 case TOP: {
143 for (int i = 0; i < nRects; i++)
144 pArray[i] = CPDF_Rect(array.GetAt(i)).top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 break;
147 }
148 case RIGHT: {
149 for (int i = 0; i < nRects; i++)
150 pArray[i] = CPDF_Rect(array.GetAt(i)).right;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 break;
153 }
154 case BOTTOM: {
155 for (int i = 0; i < nRects; i++)
156 pArray[i] = CPDF_Rect(array.GetAt(i)).bottom;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 break;
159 }
160 default:
161 break;
162 }
163 fRet = pArray[0];
164 if (type == MAX) {
165 for (int i = 1; i < nRects; i++)
166 if (fRet <= pArray[i])
167 fRet = pArray[i];
168 } else {
169 for (int i = 1; i < nRects; i++)
170 if (fRet >= pArray[i])
171 fRet = pArray[i];
172 }
173 delete[] pArray;
174 return fRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700176
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177CPDF_Rect CalculateRect(CPDF_RectArray* pRectArray) {
178 CPDF_Rect rcRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700179
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700180 rcRet.left = GetMinMaxValue(*pRectArray, MIN, LEFT);
181 rcRet.top = GetMinMaxValue(*pRectArray, MAX, TOP);
182 rcRet.right = GetMinMaxValue(*pRectArray, MAX, RIGHT);
183 rcRet.bottom = GetMinMaxValue(*pRectArray, MIN, BOTTOM);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 return rcRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186}
187
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188void SetPageContents(CFX_ByteString key,
189 CPDF_Dictionary* pPage,
190 CPDF_Document* pDocument) {
191 CPDF_Object* pContentsObj = pPage->GetStream("Contents");
192 if (!pContentsObj) {
193 pContentsObj = pPage->GetArray("Contents");
194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (!pContentsObj) {
197 // Create a new contents dictionary
198 if (!key.IsEmpty()) {
199 CPDF_Stream* pNewContents =
200 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
201 if (!pNewContents)
202 return;
203 pPage->SetAtReference("Contents", pDocument,
204 pDocument->AddIndirectObject(pNewContents));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 CFX_ByteString sStream;
207 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
208 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
209 FALSE);
210 }
211 return;
212 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 int iType = pContentsObj->GetType();
215 CPDF_Array* pContentsArray = NULL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 switch (iType) {
218 case PDFOBJ_STREAM: {
219 pContentsArray = FX_NEW CPDF_Array;
220 CPDF_Stream* pContents = (CPDF_Stream*)pContentsObj;
221 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContents);
222 CPDF_StreamAcc acc;
223 acc.LoadAllData(pContents);
224 CFX_ByteString sStream = "q\n";
225 CFX_ByteString sBody =
226 CFX_ByteString((const FX_CHAR*)acc.GetData(), acc.GetSize());
227 sStream = sStream + sBody + "\nQ";
228 pContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
229 FALSE);
230 pContentsArray->AddReference(pDocument, dwObjNum);
231 break;
232 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700233
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700234 case PDFOBJ_ARRAY: {
235 pContentsArray = (CPDF_Array*)pContentsObj;
236 break;
237 }
238 default:
239 break;
240 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700241
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 if (!pContentsArray)
243 return;
244
245 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pContentsArray);
246 pPage->SetAtReference("Contents", pDocument, dwObjNum);
247
248 if (!key.IsEmpty()) {
249 CPDF_Stream* pNewContents =
250 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
251 dwObjNum = pDocument->AddIndirectObject(pNewContents);
252 pContentsArray->AddReference(pDocument, dwObjNum);
253
254 CFX_ByteString sStream;
255 sStream.Format("q 1 0 0 1 0 0 cm /%s Do Q", key.c_str());
256 pNewContents->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
257 FALSE);
258 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261CFX_AffineMatrix GetMatrix(CPDF_Rect rcAnnot,
262 CPDF_Rect rcStream,
263 CFX_AffineMatrix matrix) {
264 if (rcStream.IsEmpty())
265 return CFX_AffineMatrix();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700266
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700267 matrix.TransformRect(rcStream);
268 rcStream.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 FX_FLOAT a = rcAnnot.Width() / rcStream.Width();
271 FX_FLOAT d = rcAnnot.Height() / rcStream.Height();
Bo Xufdc00a72014-10-28 23:03:33 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 FX_FLOAT e = rcAnnot.left - rcStream.left * a;
274 FX_FLOAT f = rcAnnot.bottom - rcStream.bottom * d;
275 return CFX_AffineMatrix(a, 0, 0, d, e, f);
276}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278void GetOffset(FX_FLOAT& fa,
279 FX_FLOAT& fd,
280 FX_FLOAT& fe,
281 FX_FLOAT& ff,
282 CPDF_Rect rcAnnot,
283 CPDF_Rect rcStream,
284 CFX_AffineMatrix matrix) {
285 FX_FLOAT fStreamWidth = 0.0f;
286 FX_FLOAT fStreamHeight = 0.0f;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700288 if (matrix.a != 0 && matrix.d != 0) {
289 fStreamWidth = rcStream.right - rcStream.left;
290 fStreamHeight = rcStream.top - rcStream.bottom;
291 } else {
292 fStreamWidth = rcStream.top - rcStream.bottom;
293 fStreamHeight = rcStream.right - rcStream.left;
294 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700296 FX_FLOAT x1 =
297 matrix.a * rcStream.left + matrix.c * rcStream.bottom + matrix.e;
298 FX_FLOAT y1 =
299 matrix.b * rcStream.left + matrix.d * rcStream.bottom + matrix.f;
300 FX_FLOAT x2 = matrix.a * rcStream.left + matrix.c * rcStream.top + matrix.e;
301 FX_FLOAT y2 = matrix.b * rcStream.left + matrix.d * rcStream.top + matrix.f;
302 FX_FLOAT x3 =
303 matrix.a * rcStream.right + matrix.c * rcStream.bottom + matrix.e;
304 FX_FLOAT y3 =
305 matrix.b * rcStream.right + matrix.d * rcStream.bottom + matrix.f;
306 FX_FLOAT x4 = matrix.a * rcStream.right + matrix.c * rcStream.top + matrix.e;
307 FX_FLOAT y4 = matrix.b * rcStream.right + matrix.d * rcStream.top + matrix.f;
Tom Sepez3c3201f2015-05-20 10:20:35 -0700308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309 FX_FLOAT left = FX_MIN(FX_MIN(x1, x2), FX_MIN(x3, x4));
310 FX_FLOAT bottom = FX_MIN(FX_MIN(y1, y2), FX_MIN(y3, y4));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700311
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 fa = (rcAnnot.right - rcAnnot.left) / fStreamWidth;
313 fd = (rcAnnot.top - rcAnnot.bottom) / fStreamHeight;
314 fe = rcAnnot.left - left * fa;
315 ff = rcAnnot.bottom - bottom * fd;
316}
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700317
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
319 if (!page) {
320 return FLATTEN_FAIL;
321 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700322
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700323 CPDF_Page* pPage = ((CPDFXFA_Page*)(page))->GetPDFPage();
324 if (!pPage)
325 return FLATTEN_FAIL;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700326
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700327 CPDF_Document* pDocument = pPage->m_pDocument;
328 CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700329
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 if (!pDocument || !pPageDict) {
331 return FLATTEN_FAIL;
332 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700334 CPDF_ObjectArray ObjectArray;
335 CPDF_RectArray RectArray;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700336
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700337 int iRet = FLATTEN_FAIL;
338 iRet = ParserAnnots(pDocument, pPageDict, &RectArray, &ObjectArray, nFlag);
339 if (iRet == FLATTEN_NOTHINGTODO || iRet == FLATTEN_FAIL)
340 return iRet;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700341
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 CPDF_Rect rcOriginalCB;
343 CPDF_Rect rcMerger = CalculateRect(&RectArray);
344 CPDF_Rect rcOriginalMB = pPageDict->GetRect("MediaBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700345
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 if (pPageDict->KeyExist("CropBox"))
347 rcOriginalMB = pPageDict->GetRect("CropBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700348
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349 if (rcOriginalMB.IsEmpty()) {
350 rcOriginalMB = CPDF_Rect(0.0f, 0.0f, 612.0f, 792.0f);
351 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700352
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700353 rcMerger.left =
354 rcMerger.left < rcOriginalMB.left ? rcOriginalMB.left : rcMerger.left;
355 rcMerger.right =
356 rcMerger.right > rcOriginalMB.right ? rcOriginalMB.right : rcMerger.right;
357 rcMerger.top =
358 rcMerger.top > rcOriginalMB.top ? rcOriginalMB.top : rcMerger.top;
359 rcMerger.bottom = rcMerger.bottom < rcOriginalMB.bottom ? rcOriginalMB.bottom
360 : rcMerger.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700361
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700362 if (pPageDict->KeyExist("ArtBox"))
363 rcOriginalCB = pPageDict->GetRect("ArtBox");
364 else
365 rcOriginalCB = rcOriginalMB;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700366
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 if (!rcOriginalMB.IsEmpty()) {
368 CPDF_Array* pMediaBox = FX_NEW CPDF_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700370 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.left));
371 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.bottom));
372 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.right));
373 pMediaBox->Add(FX_NEW CPDF_Number(rcOriginalMB.top));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700374
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 pPageDict->SetAt("MediaBox", pMediaBox);
376 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700378 if (!rcOriginalCB.IsEmpty()) {
379 CPDF_Array* pCropBox = FX_NEW CPDF_Array();
380 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.left));
381 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.bottom));
382 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.right));
383 pCropBox->Add(FX_NEW CPDF_Number(rcOriginalCB.top));
384 pPageDict->SetAt("ArtBox", pCropBox);
385 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700386
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700387 CPDF_Dictionary* pRes = NULL;
388 pRes = pPageDict->GetDict("Resources");
389 if (!pRes) {
390 pRes = FX_NEW CPDF_Dictionary;
391 pPageDict->SetAt("Resources", pRes);
392 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 CPDF_Stream* pNewXObject =
395 FX_NEW CPDF_Stream(NULL, 0, FX_NEW CPDF_Dictionary);
396 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pNewXObject);
397 CPDF_Dictionary* pPageXObject = pRes->GetDict("XObject");
398 if (!pPageXObject) {
399 pPageXObject = FX_NEW CPDF_Dictionary;
400 pRes->SetAt("XObject", pPageXObject);
401 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700402
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700403 CFX_ByteString key = "";
404 int nStreams = ObjectArray.GetSize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700405
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700406 if (nStreams > 0) {
407 for (int iKey = 0; /*iKey < 100*/; iKey++) {
408 char sExtend[5] = {};
409 FXSYS_itoa(iKey, sExtend, 10);
410 key = CFX_ByteString("FFT") + CFX_ByteString(sExtend);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700412 if (!pPageXObject->KeyExist(key))
413 break;
414 }
415 }
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700416
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700417 SetPageContents(key, pPageDict, pDocument);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700419 CPDF_Dictionary* pNewXORes = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700420
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700421 if (!key.IsEmpty()) {
422 pPageXObject->SetAtReference(key, pDocument, dwObjNum);
423 CPDF_Dictionary* pNewOXbjectDic = pNewXObject->GetDict();
424 pNewXORes = FX_NEW CPDF_Dictionary;
425 pNewOXbjectDic->SetAt("Resources", pNewXORes);
426 pNewOXbjectDic->SetAtName("Type", "XObject");
427 pNewOXbjectDic->SetAtName("Subtype", "Form");
428 pNewOXbjectDic->SetAtInteger("FormType", 1);
429 pNewOXbjectDic->SetAtName("Name", "FRM");
430 CPDF_Rect rcBBox = pPageDict->GetRect("ArtBox");
431 pNewOXbjectDic->SetAtRect("BBox", rcBBox);
432 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700433
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700434 for (int i = 0; i < nStreams; i++) {
435 CPDF_Dictionary* pAnnotDic = ObjectArray.GetAt(i);
436 if (!pAnnotDic)
437 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700438
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700439 CPDF_Rect rcAnnot = pAnnotDic->GetRect("Rect");
440 rcAnnot.Normalize();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700441
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700442 CFX_ByteString sAnnotState = pAnnotDic->GetString("AS");
443 CPDF_Dictionary* pAnnotAP = pAnnotDic->GetDict("AP");
444 if (!pAnnotAP)
445 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700446
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700447 CPDF_Stream* pAPStream = pAnnotAP->GetStream("N");
448 if (!pAPStream) {
449 CPDF_Dictionary* pAPDic = pAnnotAP->GetDict("N");
450 if (!pAPDic)
451 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700452
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 if (!sAnnotState.IsEmpty()) {
454 pAPStream = pAPDic->GetStream(sAnnotState);
455 } else {
456 FX_POSITION pos = pAPDic->GetStartPos();
457 if (pos) {
458 CFX_ByteString sKey;
459 CPDF_Object* pFirstObj = pAPDic->GetNextElement(pos, sKey);
460 if (pFirstObj) {
461 if (pFirstObj->GetType() == PDFOBJ_REFERENCE)
462 pFirstObj = pFirstObj->GetDirect();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700463
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700464 if (pFirstObj->GetType() != PDFOBJ_STREAM)
465 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700466
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700467 pAPStream = (CPDF_Stream*)pFirstObj;
468 }
469 }
470 }
471 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700472
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 if (!pAPStream)
474 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700475
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 CPDF_Dictionary* pAPDic = pAPStream->GetDict();
477 CFX_AffineMatrix matrix = pAPDic->GetMatrix("Matrix");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700478
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700479 CPDF_Rect rcStream;
480 if (pAPDic->KeyExist("Rect"))
481 rcStream = pAPDic->GetRect("Rect");
482 else if (pAPDic->KeyExist("BBox"))
483 rcStream = pAPDic->GetRect("BBox");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700484
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700485 if (rcStream.IsEmpty())
486 continue;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700487
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 CPDF_Object* pObj = pAPStream;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700489
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700490 if (pObj) {
491 CPDF_Dictionary* pObjDic = pObj->GetDict();
492 if (pObjDic) {
493 pObjDic->SetAtName("Type", "XObject");
494 pObjDic->SetAtName("Subtype", "Form");
495 }
496 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700497
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700498 CPDF_Dictionary* pXObject = pNewXORes->GetDict("XObject");
499 if (!pXObject) {
500 pXObject = FX_NEW CPDF_Dictionary;
501 pNewXORes->SetAt("XObject", pXObject);
502 }
503
504 CFX_ByteString sFormName;
505 sFormName.Format("F%d", i);
506 FX_DWORD dwObjNum = pDocument->AddIndirectObject(pObj);
507 pXObject->SetAtReference(sFormName, pDocument, dwObjNum);
508
509 CPDF_StreamAcc acc;
510 acc.LoadAllData(pNewXObject);
511
512 const uint8_t* pData = acc.GetData();
513 CFX_ByteString sStream(pData, acc.GetSize());
514 CFX_ByteString sTemp;
515
516 if (matrix.IsIdentity()) {
517 matrix.a = 1.0f;
518 matrix.b = 0.0f;
519 matrix.c = 0.0f;
520 matrix.d = 1.0f;
521 matrix.e = 0.0f;
522 matrix.f = 0.0f;
523 }
524
525 CFX_AffineMatrix m = GetMatrix(rcAnnot, rcStream, matrix);
526 sTemp.Format("q %f 0 0 %f %f %f cm /%s Do Q\n", m.a, m.d, m.e, m.f,
527 sFormName.c_str());
528 sStream += sTemp;
529
530 pNewXObject->SetData((const uint8_t*)sStream, sStream.GetLength(), FALSE,
531 FALSE);
532 }
533 pPageDict->RemoveAt("Annots");
534
535 ObjectArray.RemoveAll();
536 RectArray.RemoveAll();
537
538 return FLATTEN_SUCCESS;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700539}