阅读文本 / Reading text
This paper uses synthetic samples to demonstrate the processing method. The code has been verified using examples in an isolated environment; this does not guarantee effectiveness for all real files or products. Before processing important files, please retain the original copies and verify them against the boundaries and acceptance criteria outlined in this document.
When you use code to shrink the CropBox of a PDF page by a small margin, visually it does indeed appear that only the core content remains; however, this does not mean that the cropped area has been erased from the file. To an ordinary reader, this is merely a typographic adjustment; but for engineers who need to handle sensitive documents, conduct forensic analysis, or prepare machine translations, it represents a high-risk cognitive trap. The CropBox is simply a display instruction—it tells the rendering engine “only draw what’s inside this rectangle”—while the original data remains fully intact within the physical page defined by the MediaBox. If the CropBox is mistakenly treated as a means of redaction, confidential information could be recovered at any time.
The essential difference between CropBox and MediaBox
The PDF specification defines several bounding boxes, among which the MediaBox represents the physical dimensions of the page and serves as the absolute container for the content; the CropBox, on the other hand, is an optional display window that defaults to being equal to the MediaBox. Modifying the CropBox merely changes the viewing perspective, much like adding a viewport to an image in a web browser.
overflow: hidden
the CSS styles, rather than performing a deletion operation.
was originally designed to accommodate printing crop marks or the display requirements of different devices, but in data-processing scenarios it poses a hidden risk. Many automated scripts, during the preprocessing stage, adjust the CropBox to “remove white borders” or “focus on the main text,” then pass the file directly to downstream systems. If those downstream systems—such as OCR engines or full-text search services—ignore the CropBox and parse the content stream directly, contract clauses, annotation marks, or metadata that were meant to be concealed will be re-extracted. Understanding this mechanism is the first step in distinguishing between “visual editing” and “data-security processing.”
synthesis example: The cropped text still contains
To verify this behavior, we construct a fully self-contained synthetic case. This example does not rely on any external files; all data is generated in memory, thereby avoiding interference from environmental differences. Note that this is not a test with real-world business data but rather a controlled sample used to demonstrate API behavior. You must run this code in your own locked dependency environment, with a version compatible with the PyMuPDF 1.28.x series.
import fitz # PyMuPDF
from io import BytesIO
def create_test_pdf():
"""创建含敏感信息的内存PDF"""
doc = fitz.open()
page = doc.new_page(width=595, height=842) # A4尺寸
# 在页面中心写入公开内容
page.insert_text((100, 400), "PUBLIC CONTENT", fontsize=20)
# 在页面左上角写入模拟敏感信息
sensitive_rect = fitz.Rect(50, 50, 250, 80)
page.insert_text(sensitive_rect.bl, "CONFIDENTIAL: ID-99283", fontsize=12)
# 保存到内存并重新打开以确保状态一致
buffer = BytesIO()
doc.save(buffer)
doc.close()
buffer.seek(0)
return fitz.open(stream=buffer.read(), filetype="pdf")
# 执行测试
doc = create_test_pdf()
page = doc[0]
# 记录原始文本作为基准
original_text = page.get_text()
assert "CONFIDENTIAL" in original_text, "合成样本构建失败"
# 错误示范:仅修改CropBox试图隐藏敏感信息
visible_rect = fitz.Rect(70, 350, 500, 450)
page.set_cropbox(visible_rect)
# 关键验证:恢复MediaBox后检查内容是否仍在
# 注意:get_text()可能受CropBox影响,但内容并未真正删除
page.set_mediabox(page.mediabox) # 确保边界重置
restored_text = page.get_text()
assert "CONFIDENTIAL" in restored_text, "预期行为:CropBox未删除内容,恢复后仍可提取"
# 正确做法:使用Redaction真正移除内容
sensitive_rect = fitz.Rect(50, 50, 250, 80)
page.add_redact_annot(sensitive_rect)
page.apply_redactions()
# 验证2:应用Redaction后内容才真正消失
final_text = page.get_text()
assert "CONFIDENTIAL" not in final_text, "Redaction应彻底移除文本"
assert "PUBLIC CONTENT" in final_text, "非目标内容应保留"
print("合成示例逻辑校验完成")
This code snippet demonstrates two key facts: first,
set_cropbox()
remains present in the file even when visually invisible, and can be extracted once the boundaries are restored; secondly, only
add_redact_annot()
in conjunction with
apply_redactions()
is the only one that can truly erase the specified region from the content stream. Note that
apply_redactions()
is an irreversible operation; in a production environment, be sure to back up the original file first. Additionally, the coordinate calculations in the example are based on an unrotated page; if the page has rotation attributes, you must first use
derotation_matrix
performs coordinate transformation.
Acceptance Criteria and Manual Review Boundary
cannot rely solely on visual inspection to determine the correctness of results when handling PDF boundary issues. The following table summarizes the differences between code demonstrations and the actual production environment, as well as the scenarios in which manual review must be introduced. These judgments are based on an understanding of PDF structure, rather than on the promises of any specific tool.
check dimension
code demonstration performance
end-to-end actual performance
processing decision
Text extraction integrity
get_text() may be affected by CropBox
Most parsers follow the same logic, but some older OCR systems may only recognize the visible area.
When privacy is involved, relying on CropBox as a filtering mechanism is prohibited
vector graphic residue
Redaction can remove the covered area
Complex layer stacking may result in underlying objects not being fully cleared
Sensitive documents containing signatures/seals require manual sampling inspection of rendering results
metadata association
example does not involve metadata
may contain links, form fields, or annotation references outside the clipping region
Before delivery, the object tree structure must be checked using specialized tools.
rotation coordinate system
example uses an unrotated page
The CropBox coordinates of the rotated page still remain based on the unrotated space
When batch-processing rotated documents, it is necessary to transform the coordinate system before performing validation
specifically emphasizes the scope of inapplicability: when a PDF contains an encryption layer, digital signatures, or embedded attachments, any boundary modifications may compromise the file’s integrity or legal validity. Such files should not be processed automatically by scripts and must instead be reviewed by qualified professionals. Furthermore, the assertions in the code only verify the logical path of the current synthesized sample and do not constitute a guarantee of compatibility with all PDF variants.
References and Next Steps
PyMuPDF Page.set_cropbox official documentation
PyMuPDF Redactions User Guide
Next, we recommend that you: reproduce the aforementioned synthesis example in your own controlled test environment to confirm that your understanding of CropBox and Redaction behavior is correct; review the existing document-processing pipeline to identify any steps where CropBox may have been improperly used for redaction; conduct a sampling audit of previously processed files, with particular attention to whether sensitive documents that were once “cropped” can still have hidden content extracted. For scenarios involving legal, medical, or financial data, it is essential to establish a dual-verification mechanism to ensure strict alignment between technical measures and compliance requirements.
uses this checklist for document translation
first ensures that the page order, source locations, and text integrity have been thoroughly checked, then submits the same de-identified sample to the translation process. After translation, it is still necessary to verify that the headings, body text, and figure captions correspond correctly; one should not rely solely on whether the text flows smoothly.
OmniDoc (product entry point)
provides an entry point for translating multi-format documents, and the official website highlights its OCR, terminology database, and bilingual alignment features. For teams that wish to start processing directly from files, this is a path worth evaluating with real-world samples. Please use de-identified files that you are authorized to upload, and verify the results against the checklist provided in this document; we do not claim that OmniDoc utilizes the open-source libraries or internal algorithms referenced herein, nor do we substitute examples for formal product performance testing.
- 1 -
Updated 2026-10-08