परिचय
Aspose.PDF FOSS for Python Document क्लास पर केंद्रित है, जो pages, form, outlines, tagged_content, और attachments को संरचनात्मक, दस्तावेज़-स्तरीय संपादन के प्रवेश बिंदु के रूप में उजागर करती है। पेज कंटेंट जोड़ने के अलावा, लाइब्रेरी उन ऑपरेशनों को कवर करती है जो PDF को एक पूर्ण, वितरित करने योग्य आर्टिफैक्ट बनाते हैं: इंटरैक्टिव फ़ॉर्म फ़ील्ड जो संरचित डेटा एकत्रित और उजागर करते हैं, दस्तावेज़-स्तरीय फ़ाइल अटैचमेंट्स, टेक्स्ट लेखन के लिए फ़ॉन्ट खोज और एम्बेडिंग, और नेविगेशन के लिए बुकमार्क ट्री। इन सभी क्षेत्रों में त्रुटियों को AsposePdfException में जड़ित एक ही एक्सेप्शन हाइरार्की के माध्यम से उठाया जाता है, जिससे कॉलर पैकेज-विशिष्ट विफलताओं को बिना मॉड्यूल दर मॉड्यूल एक्सेप्शन प्रकारों का अनुमान लगाए पकड़ सकते हैं।
यह गाइड दिखाता है कि उस दस्तावेज़-प्रबंधन सतह के साथ कैसे काम किया जाए: AsposePdfException सबक्लासेज़ को पकड़ना और अलग करना, Form और Field के साथ AcroForm फ़ील्ड बनाना और पढ़ना, FileSpecification के माध्यम से अटैचमेंट्स को एम्बेड और पुनः प्राप्त करना, FontRepository और FontRegistry के साथ फ़ॉन्ट्स को हल करना, और OutlineCollection और OutlineItem के साथ बुकमार्क ट्री बनाना। प्रत्येक भाग केवल वर्तमान पैकेज में मौजूद क्लासेज़ और मेथड्स का उपयोग करता है।
Aspose.PDF FOSS for Python एक Python पैकेज है, aspose-pdf-foss-for-python, जो MIT लाइसेंस के तहत जारी किया गया है, और Python 3.11 या बाद के संस्करण की आवश्यकता रखता है। इसका टॉप-लेवल मॉड्यूल नाम aspose_pdf है। कोर पैकेज केवल cryptography और asn1crypto पर निर्भर करता है; वैकल्पिक एक्स्ट्राज़ Pillow-आधारित छवि डिकोडिंग, Brotli-आधारित WOFF2 फ़ॉन्ट सपोर्ट, और HarfBuzz-आधारित जटिल टेक्स्ट लेआउट जोड़ते हैं।
मुख्य विशेषताएँ
AsposePdfException के साथ अपवाद हैंडलिंग
Aspose.PDF FOSS for Python में प्रत्येक पैकेज-विशिष्ट त्रुटि AsposePdfException से व्युत्पन्न होती है। अधिकांश दस्तावेज़-प्रसंस्करण विफलताएँ उसके PdfException सबक्लास के अंतर्गत आती हैं, जो बदले में PdfParseException (गलत इनपुट), PdfSecurityException (एन्क्रिप्शन और पासवर्ड विफलताएँ, जिसमें InvalidPasswordException शामिल है), और PdfValidationException (संरचनात्मक या अनुपालन विफलताएँ) के आधार हैं। अधिक विशिष्ट सबक्लासेज़ के बाद AsposePdfException को अंत में पकड़ना, कॉलर को खराब पासवर्ड और भ्रष्ट फ़ाइल के बीच अलग-अलग प्रतिक्रिया देने की अनुमति देता है, जबकि पैकेज द्वारा उठाई जा सकने वाली सभी अन्य त्रुटियों के लिए एक ही फॉलबैक रहता है।
from aspose_pdf import Document
from aspose_pdf.exceptions import (
AsposePdfException,
InvalidPasswordException,
PdfParseException,
)
def open_document(path, password=None):
try:
document = Document()
document.load_from(path, password=password)
return document
except InvalidPasswordException:
print(f"{path}: a correct password is required")
except PdfParseException as error:
print(f"{path}: not a valid PDF ({error})")
except AsposePdfException as error:
# Catches every other aspose_pdf-specific error not handled above.
print(f"{path}: PDF operation failed ({error})")
return None
इंटरैक्टिव फ़ॉर्म फ़ील्ड्स
Document.form दस्तावेज़ के AcroForm फ़ील्ड्स के ऊपर एक Form फ़ैसाड लौटाता है। Form.add_text_field(), add_checkbox(), और add_radio_group() पेज और एक विजेट आयत से जुड़े नए टर्मिनल फ़ील्ड्स बनाते हैं, प्रत्येक एक Field लौटाते हैं। Field name, value, और field_type को उजागर करता है ताकि मौजूदा फ़ील्ड्स को नाम से निरीक्षण और अपडेट किया जा सके, और Field.remove() फ़ील्ड को पूरी तरह हटाता है।
from aspose_pdf import Document
with Document() as document:
page = document.pages.add()
document.form.add_text_field("customer_name", page, (72, 700, 300, 720))
document.form.add_checkbox("subscribe", page, (72, 670, 90, 688), on_value="Yes")
document.form.add_radio_group(
"plan",
page,
{"Basic": (72, 630, 90, 648), "Pro": (72, 600, 90, 618)},
value="Basic",
)
for field in document.form.fields:
print(field.name, field.field_type, field.value)
for field in document.form.fields:
if field.name == "customer_name":
field.value = "Jane Doe"
document.form.generate_appearances()
document.save("form.pdf")
एम्बेडेड फ़ाइलें और संलग्नक
Document.add_attachment बाइट्स को दस्तावेज़-स्तर के फ़ाइल अटैचमेंट के रूप में एम्बेड करता है, जो सेव पर PDF के /Names /EmbeddedFiles नाम ट्री में लिखा जाता है, साथ ही एक वैकल्पिक MIME प्रकार, विवरण, और निर्माण/संशोधन तिथियों के साथ। Document.embedded_files प्रत्येक अटैचमेंट को टाइप्ड FileSpecification (name, contents, mime_type, description, size) के रूप में पढ़ता है, और Document.get_embedded_file नाम से एक को खोजता है। FileSpecification.save() पुनः प्राप्त बाइट्स को डिस्क पर लिखता है।
from aspose_pdf import Document
with Document() as document:
document.pages.add()
document.add_attachment(
"notes.txt",
b"Reviewed and approved.",
mime="text/plain",
description="Reviewer notes",
)
document.save("with-attachment.pdf")
with Document() as document:
document.load_from("with-attachment.pdf")
for spec in document.embedded_files:
print(spec.name, spec.mime_type, spec.size)
notes = document.get_embedded_file("notes.txt")
if notes is not None:
notes.save("notes-recovered.txt")
फ़ॉन्ट खोज और एम्बेडिंग
FontRepository फ़ॉन्ट स्रोतों को एकत्र करता है और दस्तावेज़ में नाम के आधार पर फ़ॉन्ट्स को हल करता है। FontRepository.add_source() एक FontSource जैसे FolderFontSource (डायरेक्टरी को स्कैन करता है, वैकल्पिक रूप से पुनरावृत्त) को रजिस्टर करता है; FontRepository.find_font() और search() फिर फ़ॉन्ट को फ़ैमिली, पूर्ण, या PostScript नाम से हल करते हैं, और मानक-फ़ॉन्ट रजिस्ट्री पर fallback करते हैं। प्रत्येक मिलान एक FontDescriptor है, जिसे सीधे Page.add_text को पास किया जा सकता है ताकि फ़ॉन्ट को एम्बेड और सबसेट किया जा सके। FontRegistry सामान्य गैर-मानक नामों (Arial, Times New Roman, और समान) को उनके निकटतम Standard-14 समतुल्य में search_font_by_name() के माध्यम से मैप करता है।
from aspose_pdf import Document, FolderFontSource, FontRepository
from aspose_pdf.font_registry import FontRegistry
FontRepository.add_source(FolderFontSource("./fonts", scan_subdirectories=True))
descriptor = FontRepository.find_font("Open Sans")
if descriptor is None:
# Fall back to the closest Standard-14 match for a common font name.
descriptor = FontRegistry().search_font_by_name("Arial")
with Document() as document:
page = document.pages.add()
page.add_text(
"Rendered with a resolved font",
x=72,
y=700,
font_size=14,
font=descriptor,
)
document.save("font-sample.pdf")
दस्तावेज़ रूपरेखा और बुकमार्क्स
Document.outlines एक OutlineCollection लौटाता है, जो PDF के बुकमार्क ट्री के शीर्ष-स्तर कंटेनर है। OutlineCollection.add एक शीर्ष-स्तर OutlineItem जोड़ता है; OutlineItem.add() मौजूदा बुकमार्क के नीचे एक चाइल्ड बुकमार्क को नेस्ट करता है। प्रत्येक OutlineItem में एक title, एक लक्ष्य page_index, और is_bold / is_italic डिस्प्ले फ़्लैग्स होते हैं, और यह अपना स्वयं का children सूची उजागर करता है।
from aspose_pdf import Document
from aspose_pdf.outlines import OutlineItem
with Document() as document:
document.pages.add()
document.pages.add()
chapter = OutlineItem("Chapter 1: Overview", page_index=0)
document.outlines.add(chapter)
chapter.add(OutlineItem("Section 1.1", page_index=0, is_italic=True))
document.outlines.add(OutlineItem("Chapter 2: Details", page_index=1, is_bold=True))
document.save("bookmarked.pdf")
त्वरित प्रारम्भ
पैकेज स्थापित करें, फिर एक स्क्रिप्ट में बुकमार्क, दस्तावेज़ मेटाडेटा और अटैचमेंट को संयोजित करने वाला दस्तावेज़ बनाएं।
asposefoss/pdf is not yet published — build from source until it ships. See the project README for build instructions.from aspose_pdf import Document
from aspose_pdf.outlines import OutlineItem
with Document() as document:
page = document.pages.add()
page.add_text("Quarterly Report", x=72, y=740, font_size=20)
document.outlines.add(OutlineItem("Quarterly Report", page_index=0))
document.info = {"Title": "Quarterly Report"}
document.add_attachment(
"source-data.csv", b"quarter,total\nQ1,1000\n", mime="text/csv"
)
document.save("report.pdf")
with Document() as reopened:
reopened.load_from("report.pdf")
print(reopened.page_count, "page(s),", reopened.info.get("Title"))
print([spec.name for spec in reopened.embedded_files])
समर्थित प्रारूप
| फ़ॉर्मेट | एक्सटेंशन | पढ़ें | लिखें |
|---|---|---|---|
| हाँ | हाँ | ||
| TIFF | tiff | - | हाँ |
Page.render, Page.save_as_image, और Document.save_page_as_image भी TIFF के साथ PNG रास्टर आउटपुट उत्पन्न करते हैं।
ये पेज-रास्टराइज़ेशन आउटपुट हैं, न कि दस्तावेज़-लोडिंग फ़ॉर्मेट्स।
ओपन सोर्स एवं लाइसेंसिंग
Aspose.PDF FOSS for Python MIT लाइसेंस के तहत जारी किया गया है: कोई उपयोग प्रतिबंध नहीं, कोई रनटाइम शुल्क नहीं, और व्यावसायिक या व्यक्तिगत उपयोग के लिए कोई पंजीकरण आवश्यकता नहीं। स्रोत कोड github.com/aspose-pdf-foss/Aspose-PDF-FOSS-for-Python पर होस्ट किया गया है, और पैकेज PyPI पर aspose-pdf-foss-for-python के रूप में प्रकाशित किया गया है।