diff --git a/README.md b/README.md index e12a1bd..5ce42ff 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,13 @@ Directory | Description -

+

## General PDF Features - Supports most established PDF standards and PDF specifications. -- Ability to read & export PDFs in multiple image formats including BMP, GIF, JPEG & PNG. +- Ability to read & export PDFs in multiple image formats including BMP, GIF, JPEG & PNG. - Set basic information (e.g. author, creator) of the PDF document. - Configure PDF Page properties (e.g. width, height, cropbox, bleedbox etc.). - Set page numbering, bookmark level, page sizes etc. @@ -75,7 +75,7 @@ Below code snippet follows these steps: 1. Create an instance of the HtmlLoadOptions object. 1. Initialize Document object. -1. Save output PDF document by calling Document.Save() method. +1. Save output PDF document by calling Document.save() method. ```python import aspose.pdf as ap diff --git a/examples/facades_form/exporting_pdf_form_data.py b/examples/facades_form/exporting_pdf_form_data.py new file mode 100644 index 0000000..134d904 --- /dev/null +++ b/examples/facades_form/exporting_pdf_form_data.py @@ -0,0 +1,118 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Export Data to XML +def export_pdf_form_data_to_xml(infile, datafile): + """Export PDF form data to XML file.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Open XML file as stream + with FileIO(datafile, 'w') as xml_output_stream: + # Export data from PDF form fields to XML + pdf_form.export_xml(xml_output_stream) + +# Export Data to FDF +def export_form_data_to_fdf(infile, outfile): + """Export PDF form data to FDF file.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Create FDF file stream + with open(outfile, 'wb') as fdf_output_stream: + # Export form data to FDF file + pdf_form.export_fdf(fdf_output_stream) + +# Export Data to XFDF +def export_pdf_form_to_xfdf(infile, outfile): + """Export PDF form data to XFDF file.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Create XFDF file stream + with open(outfile, "wb") as xfdf_output_stream: + # Export form data to XFDF file + pdf_form.export_xfdf(xfdf_output_stream) + +# Export Data to JSON +def export_form_to_json(infile, outfile): + """Export PDF form field values to JSON file.""" + # Create Form object + form = pdf_facades.Form() + + # Bind PDF document + form.bind_pdf(infile) + + # Create JSON file stream + with FileIO(outfile, 'w') as json_stream: + # Export form field values to JSON + form.export_json(json_stream, indented=True) + +# Extract XFA Data +def export_xfa_data(infile, outfile): + """Export XFA form data.""" + # Create Form object + form = pdf_facades.Form() + + # Bind PDF document + form.bind_pdf(infile) + + with FileIO(outfile, 'w') as stream: + # Export form field values to JSON + form.extract_xfa_data(stream) + +def run_all_examples(data_dir=None, license_path=None): + """Run all import/export form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Export Data to XML", export_pdf_form_data_to_xml, "sample_form.xml"), + ("Export Data to FDF", export_form_data_to_fdf, "sample_form.fdf"), + ("Export Data to XFDF", export_pdf_form_to_xfdf, "sample_form.xfdf"), + ("Export Values to JSON", export_form_to_json, "sample_form.json"), + ("Export XFA Data", export_xfa_data, "sample_form_xfa.xml"), + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "export_xfa_data"): + input_file_name = path.join(input_dir, "sample_xfa_form.pdf") + else: + input_file_name = path.join(input_dir, "sample_form.pdf") + output_file_name = path.join(output_dir, data_file_name) + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Export Form Data examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_form/filling_form_fields.py b/examples/facades_form/filling_form_fields.py new file mode 100644 index 0000000..8b8363c --- /dev/null +++ b/examples/facades_form/filling_form_fields.py @@ -0,0 +1,160 @@ +# Filling PDF Form Fields +# ├── Fill Text Fields +# ├── Fill Check Box Fields +# ├── Fill Radio Button Fields +# ├── Fill List Box / Multi-Select Fields +# ├── Fill Barcode Fields +# └── Fill Fields by Name and Value + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Fill Text Fields +def fill_text_fields(infile, outfile): + """Fill text fields in PDF form.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill text fields by name + pdf_form.fill_field("name", "John Doe") + pdf_form.fill_field("address", "123 Main St, Anytown, USA") + pdf_form.fill_field("email", "john.doe@example.com") + + # Save updated PDF + pdf_form.save(outfile) + +# Fill Check Box Fields +def fill_check_box_fields(infile, outfile): + """Fill check box fields in PDF form.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill check box fields by name + pdf_form.fill_field("subscribe_newsletter", "Yes") + pdf_form.fill_field("accept_terms", "Yes") + + # Save updated PDF + pdf_form.save(outfile) + +# Fill Radio Button Fields +def fill_radio_button_fields(infile, outfile): + """Fill radio button fields in PDF form.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill radio button fields by name + pdf_form.fill_field("gender", 0) # Select male option (index 0) + #pdf_form.fill_field("gender", 1) # Select female option (index 1) + + # Save updated PDF + pdf_form.save(outfile) + +# Fill List Box / Multi-Select Fields +def fill_list_box_fields(infile, outfile): + """Fill list box and multi-select fields in PDF form.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill list box / multi-select fields by name + pdf_form.fill_field("favorite_colors", "Red") + + # Save updated PDF + pdf_form.save(outfile) + +# Fill Barcode Fields +def fill_barcode_fields(infile, outfile): + """Fill barcode fields in PDF form.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill barcode fields by name + pdf_form.fill_field("product_barcode", "123456789012") + + # Save updated PDF + pdf_form.save(outfile) + +# Fill Fields by Name and Value +def fill_fields_by_name_and_value(infile, outfile): + """Fill PDF form fields by name and value.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Fill fields by name and value + fields = { + "name": "Jane Smith", + "address": "456 Elm St, Othertown, USA", + "email": "jane.smith@example.com" + } + + names = list(fields.keys()) + values = list(fields.values()) + + result = pdf_form.fill_fields(names, values, []) + print(f"Filled {result} fields by name and value.") + + # Save updated PDF + pdf_form.save(outfile) + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Fill Text Fields", fill_text_fields), + ("Fill Check Box Fields", fill_check_box_fields), + ("Fill Radio Button Fields", fill_radio_button_fields), + ("Fill List Box / Multi-Select Fields", fill_list_box_fields), + ("Fill Barcode Fields", fill_barcode_fields), + ("Fill Fields by Name and Value", fill_fields_by_name_and_value) + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, f"{func.__name__}_in.pdf") + output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf") + func(input_file_name, output_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Fill Form Fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_form/importing_pdf_form_data.py b/examples/facades_form/importing_pdf_form_data.py new file mode 100644 index 0000000..c760cb7 --- /dev/null +++ b/examples/facades_form/importing_pdf_form_data.py @@ -0,0 +1,136 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Import data from XML +def import_xml_to_pdf_fields(infile, datafile, outfile): + """Import form data from XML file into PDF form fields.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Open XML file as stream + with FileIO(datafile, 'r') as xml_input_stream: + # Import data from XML into PDF form fields + pdf_form.import_xml(xml_input_stream) + + # Save updated PDF + pdf_form.save(outfile) + +# Import Data from FDF +def import_fdf_to_pdf_form(infile, datafile, outfile): + """Import form data from FDF file into PDF form fields.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Open FDF file as stream + with open(datafile, 'rb') as fdf_input_stream: + pdf_form.import_fdf(fdf_input_stream) + + # Save updated PDF + pdf_form.save(outfile) + +# Import Data from XFDF +def import_data_from_xfdf(infile, datafile, outfile): + """Import form data from XFDF file into PDF form fields.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Open XFDF file as stream + with open(datafile, 'rb') as xfdf_input_stream: + # Import data from XFDF into PDF form fields + pdf_form.import_xfdf(xfdf_input_stream) + + # Save updated PDF + pdf_form.save(outfile) + +# Import from JSON +def import_json_to_pdf_form(infile, datafile, outfile): + """Import form data from JSON file into PDF form fields.""" + # Create Form object + form = pdf_facades.Form() + + # Bind PDF document + form.bind_pdf(infile) + + # Open JSON file as stream + with FileIO(datafile, 'r') as json_stream: + # Import data from JSON into PDF form fields + form.import_json(json_stream) + + # Save updated PDF + form.save(outfile) + +# Replace from XFA data +def replace_xfa_data(infile, datafile, outfile): + """Import form data from XFA file into PDF form fields.""" + # Create Form object + form = pdf_facades.Form() + + # Bind PDF document + form.bind_pdf(infile) + + # Open XFA file as stream + with FileIO(datafile, 'r') as xfa_stream: + # Import data from XFA into PDF form fields + form.set_xfa_data(xfa_stream) + + # Save updated PDF + form.save(outfile) + + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Import Data from XML", import_xml_to_pdf_fields, "sample_form.xml"), + ("Import Data from FDF", import_fdf_to_pdf_form, "sample_form.fdf"), + ("Import Data from XFDF", import_data_from_xfdf, "sample_form.xfdf"), + ("Import Values from JSON", import_json_to_pdf_form, "sample_form.json"), + ("Replace XFA Data", replace_xfa_data, "sample_form_xfa.xml"), + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "replace_xfa_data"): + input_file_name = path.join(input_dir, "sample_xfa_form.pdf") + else: + input_file_name = path.join(input_dir, "sample_form_new.pdf") + form_data_file_name = path.join(input_dir, data_file_name) + output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf") + func(input_file_name, form_data_file_name, output_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Import Form Data examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_form/managing-pdf-form-fields.py b/examples/facades_form/managing-pdf-form-fields.py new file mode 100644 index 0000000..7824c87 --- /dev/null +++ b/examples/facades_form/managing-pdf-form-fields.py @@ -0,0 +1,100 @@ +# Flatten Specific Fields +# ├── Flatten All Fields +# └── Rename Form Fields + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Flatten specific fields +def flatten_specific_fields(infile, outfile): + """Flatten specific fields in a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Flatten specific fields by their names + fields_to_flatten = ["First Name", "Last Name"] + for field_name in fields_to_flatten: + pdf_form.flatten_field(field_name) + + # Save updated PDF + pdf_form.save(outfile) + +# Flatten all fields +def flatten_all_fields(infile, outfile): + """Flatten all fields in a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Flatten all fields in the PDF document + pdf_form.flatten_all_fields() + + # Save updated PDF + pdf_form.save(outfile) + +# Rename form fields +def rename_form_fields(infile, outfile): + """Rename form fields in a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Rename form fields by providing a mapping of old names to new names + field_renaming_map = [ + ("First Name", "NewFirstName"), + ("Last Name", "NewLastName") + ] + for old_name, new_name in field_renaming_map: + pdf_form.rename_field(old_name, new_name) + + # Save updated PDF + pdf_form.save(outfile) + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Flatten Specific Fields", flatten_specific_fields), + ("Flatten All Fields", flatten_all_fields), + ("Rename Form Fields", rename_form_fields) + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, "sample_form.pdf") + output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf") + func(input_file_name, output_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll managing PDF form fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_form/reading-and-inspecting-form-data.py b/examples/facades_form/reading-and-inspecting-form-data.py new file mode 100644 index 0000000..f4dd4d2 --- /dev/null +++ b/examples/facades_form/reading-and-inspecting-form-data.py @@ -0,0 +1,145 @@ +# ├── Get Field Values +# ├── Get Rich Text Values +# ├── Get Radio Button Options +# └── Resolve Full Field Names + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + + +# Get field values +def get_field_values(infile): + """Get field values from a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Get field values by their names + field_names = ["First Name", "Last Name"] + for field_name in field_names: + value = pdf_form.get_field(field_name) + print(f"Value of '{field_name}': {value}") + + +# Get rich text values +def get_rich_text_values(infile): + """Get rich text values from a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Get rich text values by their names + field_names = ["Summary"] + for field_name in field_names: + rich_text_value = pdf_form.get_rich_text(field_name) + print(f"Rich text value of '{field_name}': {rich_text_value}") + + +# Get radio button options +def get_radio_button_options(infile): + """Get radio button options from a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Get radio button options by their names + field_names = ["WorkType"] + for field_name in field_names: + options = pdf_form.get_button_option_current_value(field_name) + print(f"Options for '{field_name}': {options}") + + +# Resolve full field names +def resolve_full_field_names(infile): + """Resolve full field names in a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Resolve full field names + for field in pdf_form.field_names: + name= pdf_form.get_full_field_name(field) + print(f"Full field name: {name}") + +# Get required field names +def get_required_field_names(infile): + """Get required field names from a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Get required field names + for field in pdf_form.field_names: + if pdf_form.is_required_field(field): + print(f"Required field: {field}") + +#get field facades +def get_field_facades(infile): + """Get field facades from a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Get field facades + for field in pdf_form.field_names: + facade = pdf_form.get_field_facade(field) + print(f"Field facade for '{field}': {facade.box.location}, {facade.box.size}") + print(f"Field facade for '{field}': {facade.font.name}, {facade.font_size}") + + + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + set_license(license_path) + input_dir, _ = initialize_data_dir(data_dir) + + examples = [ + ("Get Field Values", get_field_values), + ("Get Rich Text Values", get_rich_text_values), + ("Get Radio Button Options", get_radio_button_options), + ("Resolve Full Field Names", resolve_full_field_names), + ("Get Required Field Names", get_required_field_names), + ("Get Field Facades", get_field_facades) + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, f"{func.__name__}_in.pdf") + func(input_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll managing PDF form fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() diff --git a/examples/facades_form/working_with_button_fields.py b/examples/facades_form/working_with_button_fields.py new file mode 100644 index 0000000..015168b --- /dev/null +++ b/examples/facades_form/working_with_button_fields.py @@ -0,0 +1,73 @@ +# Working with Button Fields and Images +# └── Add Image Appearance to Button Fields + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Add image appearance to button fields +def add_image_appearance_to_button_fields(infile, outfile): + """Add image appearance to button fields in a PDF document.""" + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + + # Add image appearance to button fields by providing the field name and image stream + image_path = infile.replace(".pdf", ".jpg") + with open(image_path, "rb") as image_stream: + pdf_form.fill_image_field("Image1_af_image", image_stream) + + # Save updated PDF + pdf_form.save(outfile) + +def get_submit_flags(infile, outfile): + # Create Form object + pdf_form = pdf_facades.Form() + + # Bind PDF document + pdf_form.bind_pdf(infile) + flags=pdf_form.get_submit_flags("Submit1_af_submit") + + print(f"Submit flags: {flags}") + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Add Image Appearance to Button Fields", add_image_appearance_to_button_fields), + ("Get Submit Flags", get_submit_flags) + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, "sample_form_image.pdf") + output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf") + func(input_file_name, output_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll managing PDF form fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_formeditor/adding_scripts_and_submit_actions.py b/examples/facades_formeditor/adding_scripts_and_submit_actions.py new file mode 100644 index 0000000..d9b04ec --- /dev/null +++ b/examples/facades_formeditor/adding_scripts_and_submit_actions.py @@ -0,0 +1,125 @@ +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades +import sys +from os import path + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +def add_field_script(input_file_name, output_file_name): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + + # Open input PDF file + form_editor.bind_pdf(input_file_name) + + # Set JavaScript action for the field + form_editor.set_field_script("Script_Demo_Button", "app.alert('Script 1 has been executed');") + + # Add JavaScript action to the field + form_editor.add_field_script("Script_Demo_Button", "app.alert('Script 2 has been executed');") + + # Save output PDF file + form_editor.save(output_file_name) + +def set_field_script(input_file_name, output_file_name): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + + # Open input PDF file + form_editor.bind_pdf(input_file_name) + + # Add JavaScript action to the field + form_editor.add_field_script("Script_Demo_Button", "app.alert('Script 1 has been executed');") + + # Set JavaScript action for the field + form_editor.set_field_script("Script_Demo_Button", "app.alert('Script 2 has been executed');") + + # Save output PDF file + form_editor.save(output_file_name) + +def remove_field_script(input_file_name, output_file_name): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + + # Open input PDF file + form_editor.bind_pdf(input_file_name) + + # Remove JavaScript action from the field + if not form_editor.remove_field_action("Script_Demo_Button"): + raise Exception("Failed to remove field script") + + # Save output PDF file + form_editor.save(output_file_name) + +def set_submit_flag(input_file_name, output_file_name): + pass +# # Create FormEditor object +# form_editor = pdf_facades.FormEditor() + +# # Open input PDF file +# form_editor.bind_pdf(input_file_name) + +# # Set submit flag for the form +# form_editor.set_submit_flag("Script_Demo_Button", ap.facades.SubmitFlag.IncludeNoValueFields) + +# # Save output PDF file +# form_editor.save(output_file_name) + +def set_submit_url(input_file_name, output_file_name): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + + # Set license + set_license() + + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + + # Open input PDF file + form_editor.bind_pdf(input_file_name) + + # Set submit URL for the button + if not form_editor.set_submit_url("Script_Demo_Button", "http://www.example.com/submit"): + raise Exception("Failed to set submit URL") + + # Save output PDF file + form_editor.save(output_file_name) + +def run_all_examples(data_dir=None, license_path=None): + """Run all examples for adding scripts and submit actions with status reporting. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Add Field Script", add_field_script), + ("Set Field Script", set_field_script), + ("Remove Field Script", remove_field_script), + ("Set Submit Flag", set_submit_flag), + ("Set Submit URL", set_submit_url), + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, func.__name__ + ".pdf") + output_file_name = path.join(output_dir, func.__name__ + ".pdf") + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Modifying Form Fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() diff --git a/examples/facades_formeditor/creating-form-field.py b/examples/facades_formeditor/creating-form-field.py new file mode 100644 index 0000000..4c7f32e --- /dev/null +++ b/examples/facades_formeditor/creating-form-field.py @@ -0,0 +1,103 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# def create_checkbox_field(infile, outfile): +# """Create CheckBox field in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add CheckBox field to PDF form +# pdf_form_editor.add_field(pdf_facades.FieldType.CHECK_BOX, "checkbox1", "Check Box 1", 100, 650, 120, 670) + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def create_combobox_field(infile, outfile): +# """Create ComboBox field in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add ComboBox field to PDF form +# pdf_form_editor.add_field(pdf_facades.FieldType.COMBO_BOX, "combobox1", "Combo Box 1", 100, 550, 200, 570) + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def create_textbox_field(infile, outfile): +# """Create TextBox field in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add TextBox field to PDF form +# pdf_form_editor.add_field(pdf_facades.FieldType.TEXT, "first_name", "Alexander", 100, 700, 200, 720) +# pdf_form_editor.add_field(pdf_facades.FieldType.TEXT, "last_name", "Smith", 100, 670, 200, 690) + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def create_radiobutton_field(infile, outfile): +# """Create RadioButton field in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add RadioButton field to PDF form +# pdf_form_editor.add_field(pdf_facades.FieldType.RADIO_BUTTON, "radiobutton1", "Radio Button 1", 100, 600, 120, 620) + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def create_listbox_field(infile, outfile): +# """Create ListBox field in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add ListBox field to PDF form +# pdf_form_editor.add_field(pdf_facades.FieldType.LIST_BOX, "listbox1", "List Box 1", 100, 500, 200, 520) + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def create_submit_button(infile, outfile): +# """Create Submit Button in PDF document.""" +# pdf_form_editor = pdf_facades.FormEditor() +# pdf_form_editor.bind_pdf(infile) + +# # Add Submit Button to PDF form +# pdf_form_editor.add_submit_btn("submitbtn1", "Submit Button", "http://example.com/submit") + +# # Save updated PDF document with form fields +# pdf_form_editor.save(outfile) + +# def run_all_examples(data_dir=None, license_path=None): +# """Run all TextBox field examples with status reporting.""" +# set_license(license_path) +# input_dir, output_dir = initialize_data_dir(data_dir) + +# examples = [ +# ("Create TextBox Field", create_textbox_field), +# ("Create CheckBox Field", create_checkbox_field), +# ("Create ComboBox Field", create_combobox_field), +# ("Create RadioButton Field", create_radiobutton_field), +# ("Create ListBox Field", create_listbox_field), +# ("Create Submit Button", create_submit_button), +# ] + +# for name, func in examples: +# try: +# infile = path.join(input_dir, "sample_empty.pdf") +# outfile = path.join(output_dir, "textbox_field_out.pdf") +# func(infile, outfile) +# print(f"✅ Success: {name}") +# except Exception as e: +# print(f"❌ Failed: {name} - {str(e)}") + + +if __name__ == "__main__": + raise Exception("Not ready to publish.") \ No newline at end of file diff --git a/examples/facades_formeditor/customizing-field-appearance.py b/examples/facades_formeditor/customizing-field-appearance.py new file mode 100644 index 0000000..d6d910d --- /dev/null +++ b/examples/facades_formeditor/customizing-field-appearance.py @@ -0,0 +1,173 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pydrawing as ap_pydrawing +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + + +def decorate_field(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + form_editor.facade = pdf_facades.FormFieldFacade() + form_editor.facade.background_color = ap_pydrawing.Color.red + form_editor.facade.text_color = ap_pydrawing.Color.blue + form_editor.facade.border_color = ap_pydrawing.Color.green + form_editor.facade.alignment = pdf_facades.FormFieldFacade.ALIGN_CENTER + form_editor.decorate_field("First Name") + + # Save updated document + form_editor.save(outfile) + + +def set_field_alignment(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Set field alignment to center + if (form_editor.set_field_alignment( + "First Name", pdf_facades.FormFieldFacade.ALIGN_CENTER + )): + # Save updated document + form_editor.save(outfile) + else: + raise Exception("Failed to set field alignment. Field may not support alignment.") + + +def set_field_alignment_vertical(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Set field vertical alignment to top + if form_editor.set_field_alignment_v( + "First Name", pdf_facades.FormFieldFacade.ALIGN_BOTTOM + ): + # Save updated document + form_editor.save(outfile) + else: + raise Exception("Failed to set field vertical alignment. Field may not support vertical alignment.") + +def set_field_appearance(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Set field appearance to invisible + if not form_editor.set_field_appearance("First Name", ap.annotations.AnnotationFlags.INVISIBLE): + raise Exception("Failed to set field appearance. Field may not support appearance flags.") + + # Save updated document + form_editor.save(outfile) + + +def set_field_attribute(infile, outfile): + # # Open document + # doc = ap.Document(infile) + + # # Create FormEditor object + # form_editor = pdf_facades.FormEditor(doc) + + # # Set field attribute to "ReadOnly" + # if not form_editor.set_field_attribute("Country", pdf_facades.PropertyFlags.READ_ONLY): + # raise Exception("Failed to set field attribute. Field may not support specified attribute.") + + # Save updated document + # form_editor.save(outfile) + pass + + +def set_field_comb_number(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Set field comb number to 5 + form_editor.set_field_comb_number("PIN", 5) + + # Save updated document + form_editor.save(outfile) + + +def set_field_limit(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Set field limit to 10 + if not form_editor.set_field_limit("Last Name", 10): + raise Exception("Failed to set field limit. Field may not support specified limit.") + + # Save updated document + form_editor.save(outfile) + + +def get_field_appearance(infile, outfile): + # Open document + doc = ap.Document(infile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor(doc) + + # Get field appearance + appearance = form_editor.get_field_appearance("Last Name") + print("Field Appearance: " + str(appearance)) + + +def run_all_examples(data_dir=None, license_path=None): + """ + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Decorate Field", decorate_field), + ("Set Field Alignment", set_field_alignment), + ("Set Field Alignment Vertical", set_field_alignment_vertical), + ("Set Field Appearance", set_field_appearance), + ("Set Field Attribute", set_field_attribute), + ("Set Field Comb Number", set_field_comb_number), + ("Set Field Limit", set_field_limit), + ("Get Field Appearance", get_field_appearance), + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, f"{func.__name__}.pdf") + output_file_name = path.join(output_dir, f"{func.__name__}.pdf") + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Modifying Form Fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() diff --git a/examples/facades_formeditor/modifying-form-fields.py b/examples/facades_formeditor/modifying-form-fields.py new file mode 100644 index 0000000..ec06e4c --- /dev/null +++ b/examples/facades_formeditor/modifying-form-fields.py @@ -0,0 +1,133 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + + +def add_list_item(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Add list item to list box field + form_editor.add_list_item("Country", ["New Zealand","New Zealand"]) + # Save updated document + form_editor.save(outfile) + + +def del_list_item(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Delete list item from list box field + form_editor.del_list_item("Country", "UK") + # Save updated document + form_editor.save(outfile) + + +def move_field(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Move field to new page + form_editor.move_field("Country", 200, 600, 280, 620) + # Save updated document + form_editor.save(outfile) + + +def remove_field(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Remove field from document + form_editor.remove_field("Country") + # Save updated document + form_editor.save(outfile) + + +def rename_field(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Rename field in document + form_editor.rename_field("City", "Town") + # Save updated document + form_editor.save(outfile) + + +def single2multiple(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Change a single-lined text field to a multiple-lined one + form_editor.single_2_multiple("City") + # Save updated document + form_editor.save(outfile) + + +def copy_inner_field(infile, outfile): + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(infile) + # Copies an existing field to a new position specified by both page number and ordinates. + # A new document will be produced, which contains everything the source document has except for the newly copied field. + form_editor.copy_inner_field("First Name", "First Name Copy", 2, 200, 600) + # Save updated document + form_editor.save(outfile) + + +def copy_outer_field(infile, outfile): + # Since copy_outer_field() method needs to copy field from source document to target document, we need to create a new document as target document first. + doc = ap.Document() + doc.pages.add() + doc.save(outfile) + + # Create FormEditor object + form_editor = pdf_facades.FormEditor() + # Bind document to FormEditor + form_editor.bind_pdf(outfile) + # Copies an existing field to a new position specified by both page number and ordinates. + # A new document will be produced, which contains everything the source document has except for the newly copied field. + form_editor.copy_outer_field(infile, "First Name", 1, 200, 600) + # Save updated document + form_editor.save(outfile) + + +def run_all_examples(data_dir=None, license_path=None): + """Run all form field modification examples and report status.""" + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Add List Item", add_list_item), + ("Delete List Item", del_list_item), + ("Move Field", move_field), + ("Remove Field", remove_field), + ("Rename Field", rename_field), + ("Single to Multiple", single2multiple), + ("Copy Inner Field", copy_inner_field), + ("Copy Outer Field", copy_outer_field), + ] + + for name, func in examples: + try: + func(path.join(input_dir, f"{func.__name__}.pdf"), + path.join(output_dir, f"{func.__name__}.pdf")) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + +if __name__ == "__main__": + run_all_examples() diff --git a/examples/facades_pdf_content_editor/adding-annotations-to-existing-pdf-file.py b/examples/facades_pdf_content_editor/adding-annotations-to-existing-pdf-file.py new file mode 100644 index 0000000..0a87f57 --- /dev/null +++ b/examples/facades_pdf_content_editor/adding-annotations-to-existing-pdf-file.py @@ -0,0 +1,110 @@ +import aspose.pdf as ap +import aspose.pydrawing as apd +import aspose.pdf.facades as pdf_facades + +import sys +from os import path + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + + +def add_free_text_annotation(infile, outfile): + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + # Bind PDF document to PdfContentEditor + editor.bind_pdf(infile) + + # Search for the text "PDF" on the first page + tfa = ap.text.TextFragmentAbsorber("PDF") + tfa.visit(editor.document.pages[1]) + + # Define rectangle above the found text fragment + rect = apd.Rectangle( + int(tfa.text_fragments[1].rectangle.llx), + int(tfa.text_fragments[1].rectangle.ury) + 5, + 100, # Width + 18 # Height + ) + + # Add free text annotation on page 1 + editor.create_free_text(rect, "Free Text Demo", 1) + + # Save updated PDF document + editor.save(outfile) + editor.close() + +def add_text_annotation(infile, outfile): + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + # Bind PDF document to PdfContentEditor + editor.bind_pdf(infile) + + # Search for the text "PDF" on the first page + tfa = ap.text.TextFragmentAbsorber("PDF") + tfa.visit(editor.document.pages[1]) + + # Define rectangle above the found text fragment + rect = apd.Rectangle( + int(tfa.text_fragments[1].rectangle.llx), + int(tfa.text_fragments[1].rectangle.ury) + 5, + 100, # Width + 18 # Height + ) + + # Add free text annotation on page 1 + editor.create_text(rect, "Title", "Content", True, "Subject", 1) + + # Save updated PDF document + editor.save(outfile) + editor.close() + +def add_line_annotation(infile, outfile): + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + # Bind PDF document to PdfContentEditor + editor.bind_pdf(infile) + + # Add line annotation on page 1 + editor.create_line( + apd.Rectangle(550, 93, 562, 439), # Bounding rectangle + "A sample line", # Content + 556, 99, # Starting coordinates (X1, Y1) + 556, 443, # Ending coordinates (X2, Y2) + 1, # Starting border style + 2, # Ending border style + apd.Color.dark_red, # Line color + "dash", # Line style + [1, 0, 3], # Dash pattern + ["Open", "Open"] # Line ending styles + ) + + + # Save updated PDF document + editor.save(outfile) + editor.close() + + +def run_all_examples(data_dir=None, license_path=None): + """Run all form field modification examples and report status.""" + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Add Free Text Annotation", add_free_text_annotation), + ("Add Text Annotation", add_text_annotation), + ("Add Line Annotation", add_line_annotation) + ] + + for name, func in examples: + try: + func(path.join(input_dir, f"{func.__name__}.pdf"), + path.join(output_dir, f"{func.__name__}.pdf")) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_pdf_content_editor/adding-javascript-actions_1.py b/examples/facades_pdf_content_editor/adding-javascript-actions_1.py new file mode 100644 index 0000000..b510c6b --- /dev/null +++ b/examples/facades_pdf_content_editor/adding-javascript-actions_1.py @@ -0,0 +1,41 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\adding-javascript-actions +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF and System.Drawing +clr.AddReference("Aspose.PDF") +clr.AddReference("System.Drawing") + +import Aspose.Pdf.Facades as pdf_facades +from System.Drawing import Rectangle, Color + +def add_javascript_action(): + # Path to documents directory + data_dir = "/path/to/documents/" # <-- update to your actual path + + # Create PdfContentEditor + editor = pdf_facades.PdfContentEditor() + + # Bind input PDF + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Define rectangle area for JavaScript link (x, y, width, height) + rect = Rectangle(50, 750, 150, 30) + + # JavaScript code to execute + code = "app.alert('Welcome to Aspose!');" + + # Create JavaScript link annotation on page 1 + editor.CreateJavaScriptLink(code, rect, 1, Color.Green) + + # Save updated PDF + editor.save(os.path.join(data_dir, "JavaScriptAdded_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("JavaScript action added successfully.") diff --git a/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_1.py b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_1.py new file mode 100644 index 0000000..515221b --- /dev/null +++ b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_1.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\extract-image-and-change-position-of-a-stamp +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF and System.Drawing +clr.AddReference("Aspose.PDF") +clr.AddReference("System.Drawing") + +import Aspose.Pdf.Facades as pdf_facades + +def extract_image_from_stamp(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + pdf_content_editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + pdf_content_editor.bind_pdf(os.path.join(data_dir, "ExtractImage-ImageStamp.pdf")) + + # Get stamp info for the first page + infos = pdf_content_editor.GetStamps(1) + + # Get the image from the first stamp + image = infos[0].Image + + # Save the extracted image + image.save(os.path.join(data_dir, "image_out.jpg")) + + # Dispose resources + pdf_content_editor.Dispose() + + print("Image extracted successfully from stamp and saved as image_out.jpg.") diff --git a/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_2.py b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_2.py new file mode 100644 index 0000000..bf4e342 --- /dev/null +++ b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_2.py @@ -0,0 +1,39 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\extract-image-and-change-position-of-a-stamp +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def change_stamp_position(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + pdf_content_editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + pdf_content_editor.bind_pdf(os.path.join(data_dir, "ChangeStampPosition.pdf")) + + # Define page ID, stamp index, and new coordinates + page_id = 1 + stamp_index = 1 + x = 200 + y = 200 + + # Change the position of the stamp to new x and y position + pdf_content_editor.MoveStamp(page_id, stamp_index, x, y) + + # Save updated PDF document + pdf_content_editor.save(os.path.join(data_dir, "ChangeStampPosition_out.pdf")) + + # Dispose resources + pdf_content_editor.Dispose() + + print("Stamp position changed successfully.") diff --git a/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_3.py b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_3.py new file mode 100644 index 0000000..8a9c1ec --- /dev/null +++ b/examples/facades_pdf_content_editor/extract-image-and-change-position-of-a-stamp_3.py @@ -0,0 +1,39 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\extract-image-and-change-position-of-a-stamp +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def move_stamp_by_id(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + pdf_content_editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + pdf_content_editor.bind_pdf(os.path.join(data_dir, "ChangeStampPosition.pdf")) + + # Define page ID, stamp ID, and new coordinates + page_id = 1 + stamp_id = 1 + x = 200 + y = 200 + + # Change the position of the stamp to new x and y position + pdf_content_editor.MoveStamp(page_id, stamp_id, x, y) + + # Save updated PDF document + pdf_content_editor.save(os.path.join(data_dir, "ChangeStampPositionByID_out.pdf")) + + # Dispose resources + pdf_content_editor.Dispose() + + print("Stamp moved successfully by ID.") diff --git a/examples/facades_pdf_content_editor/get-viewer-preference-of-an-existing-pdf-file_1.py b/examples/facades_pdf_content_editor/get-viewer-preference-of-an-existing-pdf-file_1.py new file mode 100644 index 0000000..efa1dbe --- /dev/null +++ b/examples/facades_pdf_content_editor/get-viewer-preference-of-an-existing-pdf-file_1.py @@ -0,0 +1,42 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\get-viewer-preference-of-an-existing-pdf-file +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def get_viewer_preference(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Open PDF document + document = pdf.Document(os.path.join(data_dir, "SetViewerPreference.pdf")) + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor(document) + + # Get Viewer Preferences + preferences = editor.GetViewerPreference() + + # Check specific preferences + if (preferences & pdf_facades.ViewerPreference.CenterWindow) != 0: + print("CenterWindow") + + if (preferences & pdf_facades.ViewerPreference.HideMenubar) != 0: + print("Menu bar hidden") + + if (preferences & pdf_facades.ViewerPreference.PageModeFullScreen) != 0: + print("Page Mode Full Screen") + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Viewer preferences retrieved successfully.") diff --git a/examples/facades_pdf_content_editor/how-to-create-nested-bookmarks_1.py b/examples/facades_pdf_content_editor/how-to-create-nested-bookmarks_1.py new file mode 100644 index 0000000..e6c0a78 --- /dev/null +++ b/examples/facades_pdf_content_editor/how-to-create-nested-bookmarks_1.py @@ -0,0 +1,45 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\how-to-create-nested-bookmarks +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF and System.Drawing +clr.AddReference("Aspose.PDF") +clr.AddReference("System.Drawing") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades +from System.Drawing import Color + +def add_bookmarks_action(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Open PDF document + document = pdf.Document(os.path.join(data_dir, "Sample.pdf")) + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor(document) + + # Create a bookmark with action + editor.CreateBookmarksAction( + "Bookmark 1", # Bookmark title + Color.Green, # Bookmark color + True, # Bold + False, # Italic + "", # Destination (empty string for default) + "GoTo", # Action type + "2" # Destination page number + ) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo_Bookmark_out.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Bookmark with action added successfully.") diff --git a/examples/facades_pdf_content_editor/replace-text-facades_1.py b/examples/facades_pdf_content_editor/replace-text-facades_1.py new file mode 100644 index 0000000..f0625ff --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_1.py @@ -0,0 +1,61 @@ +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + + +def replace_text(input_file, output_file): + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + editor.bind_pdf(input_file) + + # Replace text: "Value" -> "Label" + editor.replace_text("Value", "Label") + + # Save updated PDF document + editor.save(output_file) + + print("Text replaced successfully.") + + +def run_all_examples(data_dir=None, license_path=None): + """Run all import form data examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Replace Text", replace_text) + ] + + for name, func in examples: + try: + input_file_name = path.join(input_dir, f"{func.__name__}_in.pdf") + output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf") + func(input_file_name, output_file_name) + + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll Fill Form Fields examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_pdf_content_editor/replace-text-facades_2.py b/examples/facades_pdf_content_editor/replace-text-facades_2.py new file mode 100644 index 0000000..ea14cde --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_2.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\replace-text-facades +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def replace_text02(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Replace text: "Value" -> "Label" with font size 12 + editor.ReplaceText("Value", "Label", 12) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo02_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("Text replaced successfully with new font size.") diff --git a/examples/facades_pdf_content_editor/replace-text-facades_3.py b/examples/facades_pdf_content_editor/replace-text-facades_3.py new file mode 100644 index 0000000..0851db0 --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_3.py @@ -0,0 +1,40 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\replace-text-facades +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades +from Aspose.Pdf.Text import TextState +import Aspose.Pdf as pdf + +def replace_text03(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Define text state with formatting + text_state = TextState() + text_state.ForegroundColor = pdf.Color.Red + text_state.FontSize = 12 + + # Replace text: "Value" -> "Label" with formatting + editor.ReplaceText("Value", "Label", text_state) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo03_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("Text replaced successfully with formatting applied.") diff --git a/examples/facades_pdf_content_editor/replace-text-facades_4.py b/examples/facades_pdf_content_editor/replace-text-facades_4.py new file mode 100644 index 0000000..4d96cc9 --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_4.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\replace-text-facades +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def replace_text04(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Replace all occurrences of "Value" with "Label" + count = 0 + while editor.ReplaceText("Value", "Label"): + count += 1 + + print(f"{count} occurrences have been replaced.") + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo04_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("Text replacement completed successfully.") diff --git a/examples/facades_pdf_content_editor/replace-text-facades_5.py b/examples/facades_pdf_content_editor/replace-text-facades_5.py new file mode 100644 index 0000000..4f3ac9e --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_5.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\replace-text-facades +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def replace_text05(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor() + + # Bind PDF document + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Replace all occurrences of "9999" with "ABCDE" using font size 2 + count = 0 + while editor.ReplaceText("9999", 2, "ABCDE"): + count += 1 + + print(f"{count} occurrences have been replaced.") + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo05_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("Text replacement completed successfully.") diff --git a/examples/facades_pdf_content_editor/replace-text-facades_6.py b/examples/facades_pdf_content_editor/replace-text-facades_6.py new file mode 100644 index 0000000..9634f63 --- /dev/null +++ b/examples/facades_pdf_content_editor/replace-text-facades_6.py @@ -0,0 +1,38 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\replace-text-facades +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf.Facades as pdf_facades + +def replace_text06(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with ReplaceTextStrategy + strategy = pdf_facades.ReplaceTextStrategy() + strategy.IsRegularExpressionUsed = True + strategy.ReplaceScope = pdf_facades.ReplaceTextStrategy.Scope.ReplaceAll + + editor = pdf_facades.PdfContentEditor() + editor.ReplaceTextStrategy = strategy + + # Bind PDF document + editor.bind_pdf(os.path.join(data_dir, "sample.pdf")) + + # Replace all 4-digit numbers with "ABCDE" + editor.ReplaceText(r"\d{4}", "ABCDE") + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo06_out.pdf")) + + # Dispose resources + editor.Dispose() + + print("Text replaced successfully using regex strategy.") diff --git a/examples/facades_pdf_content_editor/set-viewer-preference-of-an-existing-pdf-file_1.py b/examples/facades_pdf_content_editor/set-viewer-preference-of-an-existing-pdf-file_1.py new file mode 100644 index 0000000..b7d105b --- /dev/null +++ b/examples/facades_pdf_content_editor/set-viewer-preference-of-an-existing-pdf-file_1.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\set-viewer-preference-of-an-existing-pdf-file +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def set_viewer_preference(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Open PDF document + document = pdf.Document(os.path.join(data_dir, "Sample.pdf")) + + # Instantiate PdfContentEditor object + editor = pdf_facades.PdfContentEditor(document) + + # Change Viewer Preferences + editor.ChangeViewerPreference(pdf_facades.ViewerPreference.CenterWindow) + editor.ChangeViewerPreference(pdf_facades.ViewerPreference.HideMenubar) + editor.ChangeViewerPreference(pdf_facades.ViewerPreference.PageModeFullScreen) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo_SetViewerPreference_out.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Viewer preferences set successfully.") diff --git a/examples/facades_pdf_content_editor/working-with-attachments-facades_1.py b/examples/facades_pdf_content_editor/working-with-attachments-facades_1.py new file mode 100644 index 0000000..1ee0fa3 --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-attachments-facades_1.py @@ -0,0 +1,36 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-attachments-facades +# Code fence language: python + + +import os +import clr + +# Add reference to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def add_attachment(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "AddAttachment.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Add an attachment to the PDF + editor.AddDocumentAttachment( + os.path.join(data_dir, "Demo_MP3.mp3"), # File path + "Demo MP3 file" # Description + ) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "AddAttachment_out.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Attachment added successfully.") diff --git a/examples/facades_pdf_content_editor/working-with-attachments-facades_2.py b/examples/facades_pdf_content_editor/working-with-attachments-facades_2.py new file mode 100644 index 0000000..8f2e53e --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-attachments-facades_2.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-attachments-facades +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades +from System.IO import File + +def add_attachment(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "AddAttachment.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Open file stream for the attachment + file_stream = File.OpenRead(os.path.join(data_dir, "Demo_MP3.mp3")) + + # Add attachment to the PDF + editor.AddDocumentAttachment(file_stream, "Demo_MP3.mp3", "Demo MP3 file") + + # Save updated PDF document + editor.save(os.path.join(data_dir, "AddAttachment_out.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Attachment added successfully using file stream.") diff --git a/examples/facades_pdf_content_editor/working-with-attachments-facades_3.py b/examples/facades_pdf_content_editor/working-with-attachments-facades_3.py new file mode 100644 index 0000000..d2b00f3 --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-attachments-facades_3.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-attachments-facades +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def delete_all_attachments(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "DeleteAllAttachments.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Delete all attachments from the PDF + editor.DeleteAttachments() + + # Save updated PDF document + editor.save(os.path.join(data_dir, "DeleteAllAttachments_out.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("All attachments deleted successfully.") diff --git a/examples/facades_pdf_content_editor/working-with-images_1.py b/examples/facades_pdf_content_editor/working-with-images_1.py new file mode 100644 index 0000000..f6419e3 --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-images_1.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-images +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def delete_image(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "sample.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Delete image from page 2, targeting image index 2 + editor.DeleteImage(2, [2]) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo10.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Image deleted successfully from the PDF.") diff --git a/examples/facades_pdf_content_editor/working-with-images_2.py b/examples/facades_pdf_content_editor/working-with-images_2.py new file mode 100644 index 0000000..c1f9a83 --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-images_2.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-images +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def delete_images(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "sample.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Delete all images from the PDF + editor.DeleteImage() + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo11.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("All images deleted successfully from the PDF.") diff --git a/examples/facades_pdf_content_editor/working-with-images_3.py b/examples/facades_pdf_content_editor/working-with-images_3.py new file mode 100644 index 0000000..6d1740f --- /dev/null +++ b/examples/facades_pdf_content_editor/working-with-images_3.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfcontenteditor\working-with-images +# Code fence language: python + + +import os +import clr + +# Add references to Aspose.PDF +clr.AddReference("Aspose.PDF") + +import Aspose.Pdf as pdf +import Aspose.Pdf.Facades as pdf_facades + +def replace_image(): + # Path to documents directory + data_dir = "/path/to/documents/" # <- update this to your actual path + + # Instantiate PdfContentEditor with a PDF document + document = pdf.Document(os.path.join(data_dir, "sample_cats_dogs.pdf")) + editor = pdf_facades.PdfContentEditor(document) + + # Replace image on page 2, image index 4 with a new image + editor.ReplaceImage(2, 4, os.path.join(data_dir, "Image.jpg")) + + # Save updated PDF document + editor.save(os.path.join(data_dir, "PdfContentEditorDemo12.pdf")) + + # Dispose resources + editor.Dispose() + document.Dispose() + + print("Image replaced successfully in the PDF.") diff --git a/examples/facades_pdf_file_editor/booklet-and-nup-layout.py b/examples/facades_pdf_file_editor/booklet-and-nup-layout.py new file mode 100644 index 0000000..761a96e --- /dev/null +++ b/examples/facades_pdf_file_editor/booklet-and-nup-layout.py @@ -0,0 +1,73 @@ +# Booklet & N-Up Layout +#─ Create PDF Booklet +# ─ Create N-Up PDF Document + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Create PDF Booklet +def create_pdf_booklet(): + # Initialize data directory path + data_dir = initialize_data_dir() + # Create BookletMaker object + booklet_maker = pdf_facades.BookletMaker() + # Set input PDF file + booklet_maker.bind_pdf(data_dir + "input.pdf") + # Set output PDF file + booklet_maker.save(data_dir + "booklet_output.pdf") + +# Create N-Up PDF Document +def create_nup_pdf_document(): + # Initialize data directory path + data_dir = initialize_data_dir() + # Create NUpMaker object + nup_maker = pdf_facades.NUpMaker() + # Set input PDF file + nup_maker.bind_pdf(data_dir + "input.pdf") + # Set output PDF file + nup_maker.save(data_dir + "nup_output.pdf") + +def run_all_examples(data_dir=None, license_path=None): + """Run all booklet and N-Up layout examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + + ("Create PDF Booklet", create_pdf_booklet, "booklet_output.pdf"), + ("Create N-Up PDF Document", create_nup_pdf_document, "nup_output.pdf") + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "create_pdf_booklet") or (func.__name__ == "create_nup_pdf_document"): + input_file_name = path.join(input_dir, "input.pdf") + else: + input_file_name = path.join(input_dir, "f") + output_file_name = path.join(output_dir, data_file_name) + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll booklet and N-Up layout examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_pdf_file_editor/page-layout-and-margins.py b/examples/facades_pdf_file_editor/page-layout-and-margins.py new file mode 100644 index 0000000..8c09207 --- /dev/null +++ b/examples/facades_pdf_file_editor/page-layout-and-margins.py @@ -0,0 +1,136 @@ +# Page Layout & Margins +#─ Add Margins to PDF Pages +#─ Resize PDF Page Contents +#─ Add Page Breaks in PDF + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Add Margins to PDF Pages +def add_margins_to_pdf_pages(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the margins to be added (in points) + left_margin = 36 # 0.5 inch + right_margin = 36 # 0.5 inch + top_margin = 36 # 0.5 inch + bottom_margin = 36 # 0.5 inch + + # Add margins to each page in the PDF document + for page in pdf_document.pages: + page.trim_box = ap.Rectangle( + page.trim_box.llx + left_margin, + page.trim_box.lly + bottom_margin, + page.trim_box.urx - right_margin, + page.trim_box.ury - top_margin, + ) + + # Save the modified PDF document + output_path = path.join(data_dir, "output_with_margins.pdf") + pdf_document.save(output_path) + print(f"PDF with added margins saved to: {output_path}") + +# Resize PDF Page Contents +def resize_pdf_page_contents(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the scaling factor (e.g., 0.5 for 50% reduction) + scaling_factor = 0.5 + + # Resize the contents of each page in the PDF document + for page in pdf_document.pages: + page.scale_content(scaling_factor) + + # Save the modified PDF document + output_path = path.join(data_dir, "output_resized.pdf") + pdf_document.save(output_path) + print(f"PDF with resized contents saved to: {output_path}") + +# Add Page Breaks in PDF +def add_page_breaks_in_pdf(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the page break position (e.g., after every 2 pages) + page_break_position = 2 + + # Add page breaks at the specified position + for i in range(page_break_position, len(pdf_document.pages), page_break_position): + pdf_editor.insert_page(pdf_document, i) + + # Save the modified PDF document + output_path = path.join(data_dir, "output_with_page_breaks.pdf") + pdf_document.save(output_path) + print(f"PDF with added page breaks saved to: {output_path}") + +def run_all_examples(data_dir=None, license_path=None): + """Run all page layout and margins examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + + ("Add Margins to PDF Pages", add_margins_to_pdf, "output_with_margins.pdf"), + ("Resize PDF Page Contents", resize_pdf_page_contents, "output_resized.pdf"), + ("Add Page Breaks in PDF", add_page_breaks_in_pdf, "output_with_page_breaks.pdf") + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "add_margins_to_pdf") or (func.__name__ == "resize_pdf_page_contents") or (func.__name__ == "add_page_breaks_in_pdf"): + input_file_name = path.join(input_dir, "input.pdf") + else: + input_file_name = path.join(input_dir, "f") + output_file_name = path.join(output_dir, data_file_name) + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll page layout and margins examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_pdf_file_editor/page-managment.py b/examples/facades_pdf_file_editor/page-managment.py new file mode 100644 index 0000000..3fac246 --- /dev/null +++ b/examples/facades_pdf_file_editor/page-managment.py @@ -0,0 +1,185 @@ +# Page Management +#─ Extract Pages from PDF +#─ Delete Pages from PDF +#─ Insert Pages into PDF +#─ Append Pages to PDF +#─ Concatenate or Merge PDF Files + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Extract Pages from PDF +def extract_pages_from_pdf(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the page numbers to be extracted (1-based index) + pages_to_extract = [1, 3, 5] + + # Extract the specified pages into a new PDF document + output_path = path.join(data_dir, "extracted_pages.pdf") + pdf_editor.extract( + path.join(data_dir, "input.pdf"), + output_path, + pages_to_extract, + ) + print(f"Extracted pages saved to: {output_path}") + +# Delete Pages from PDF +def delete_pages_from_pdf(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the page numbers to be deleted (1-based index) + pages_to_delete = [2, 4] + + # Delete the specified pages from the PDF document + output_path = path.join(data_dir, "pdf_with_deleted_pages.pdf") + pdf_editor.delete( + path.join(data_dir, "input.pdf"), + output_path, + pages_to_delete, + ) + print(f"PDF with deleted pages saved to: {output_path}") + +# Insert Pages into PDF +def insert_pages_into_pdf(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the page number where new pages will be inserted (1-based index) + insert_page_number = 2 + + # Define the path to the PDF document containing pages to be inserted + pages_to_insert_path = path.join(data_dir, "pages_to_insert.pdf") + + # Insert pages from the specified PDF document into the source PDF document + output_path = path.join(data_dir, "pdf_with_inserted_pages.pdf") + pdf_editor.insert( + path.join(data_dir, "input.pdf"), + output_path, + insert_page_number, + pages_to_insert_path, + [1, 2], # Pages to insert from the second PDF (1-based index) + ) + print(f"PDF with inserted pages saved to: {output_path}") + +# Append Pages to PDF +def append_pages_to_pdf(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Open the source PDF document + with ap.Document(path.join(data_dir, "input.pdf")) as pdf_document: + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Define the path to the PDF document containing pages to be appended + pages_to_append_path = path.join(data_dir, "pages_to_append.pdf") + + # Append pages from the specified PDF document to the end of the source PDF document + output_path = path.join(data_dir, "pdf_with_appended_pages.pdf") + pdf_editor.append( + path.join(data_dir, "input.pdf"), + output_path, + pages_to_append_path, + [1, 2], # Pages to append from the second PDF (1-based index) + ) + print(f"PDF with appended pages saved to: {output_path}") + +# Concatenate or Merge PDF Files +def concatenate_or_merge_pdf_files(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Set the license for Aspose.PDF + set_license() + + # Define the paths to the PDF documents to be merged + pdf_files_to_merge = [ + path.join(data_dir, "input.pdf"), + path.join(data_dir, "additional.pdf"), + ] + + # Create a PdfFileEditor object + pdf_editor = pdf_facades.PdfFileEditor() + + # Merge the specified PDF documents into a single PDF document + output_path = path.join(data_dir, "merged_output.pdf") + pdf_editor.merge(pdf_files_to_merge, output_path) + print(f"Merged PDF saved to: {output_path}") + +def run_all_examples(data_dir=None, license_path=None): + """Run all page management examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + + ("Insert Pages into PDF", insert_pages_into_pdf, "pdf_with_inserted_pages.pdf"), + ("Append Pages to PDF", append_pages_to_pdf, "pdf_with_appended_pages.pdf"), + ("Concatenate or Merge PDF Files", concatenate_or_merge_pdf_files, "merged_output.pdf"), + ("Extract Pages from PDF", extract_pages_from_pdf, "extracted_pages.pdf"), + ("Delete Pages from PDF", delete_pages_from_pdf, "pdf_with_deleted_pages.pdf") + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "insert_pages_into_pdf" or func.__name__ == "append_pages_to_pdf"): + input_file_name = path.join(input_dir, "input.pdf") + else: + input_file_name = path.join(input_dir, "f") + output_file_name = path.join(output_dir, data_file_name) + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll page management examples finished.") + + +if __name__ == "__main__": + run_all_examples() \ No newline at end of file diff --git a/examples/facades_pdf_file_editor/page_operations.py b/examples/facades_pdf_file_editor/page_operations.py new file mode 100644 index 0000000..e69de29 diff --git a/examples/facades_pdf_file_editor/split-pdf-documents.py b/examples/facades_pdf_file_editor/split-pdf-documents.py new file mode 100644 index 0000000..bbfd2d0 --- /dev/null +++ b/examples/facades_pdf_file_editor/split-pdf-documents.py @@ -0,0 +1,144 @@ +# Split PDF Documents +#─ Split PDF from Beginning +#─ Split PDF to End +#─ Split PDF into Multiple Documents +#─ Split PDF into Single Pages + +from io import FileIO +import sys +from os import path +import aspose.pdf as ap +import aspose.pdf.facades as pdf_facades + +sys.path.append(path.join(path.dirname(__file__), "..")) + +from config import set_license, initialize_data_dir + +# Split PDF from Beginning +def split_pdf_from_beginning(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Create a new PDF document + pdf_document = ap.Document() + + # Add a page to the PDF document + pdf_document.pages.add() + + # Save the PDF document to the data directory + input_pdf_path = path.join(data_dir, "input.pdf") + pdf_document.save(input_pdf_path) + + # Create an instance of PdfFileEditor + pdf_file_editor = pdf_facades.PdfFileEditor() + + # Split the PDF document from the beginning (starting from page 1) + output_pdf_path = path.join(data_dir, "output_from_beginning.pdf") + pdf_file_editor.split(input_pdf_path, output_pdf_path, 1) + +# Split PDF to End +def split_pdf_to_end(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Create a new PDF document + pdf_document = ap.Document() + + # Add a page to the PDF document + pdf_document.pages.add() + + # Save the PDF document to the data directory + input_pdf_path = path.join(data_dir, "input.pdf") + pdf_document.save(input_pdf_path) + + # Create an instance of PdfFileEditor + pdf_file_editor = pdf_facades.PdfFileEditor() + + # Split the PDF document to the end (starting from page 1) + output_pdf_path = path.join(data_dir, "output_to_end.pdf") + pdf_file_editor.split(input_pdf_path, output_pdf_path, 1, -1) + +# Split PDF into Multiple Documents +def split_pdf_into_multiple_documents(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Create a new PDF document + pdf_document = ap.Document() + + # Add multiple pages to the PDF document + for i in range(1, 6): + pdf_document.pages.add() + + # Save the PDF document to the data directory + input_pdf_path = path.join(data_dir, "input.pdf") + pdf_document.save(input_pdf_path) + + # Create an instance of PdfFileEditor + pdf_file_editor = pdf_facades.PdfFileEditor() + + # Split the PDF document into multiple documents (2 pages each) + output_pdf_path_pattern = path.join(data_dir, "output_split_%d.pdf") + pdf_file_editor.split(input_pdf_path, output_pdf_path_pattern, 1, 2) + +# Split PDF into Single Pages +def split_pdf_into_single_pages(): + # Initialize the data directory + data_dir = initialize_data_dir() + + # Create a new PDF document + pdf_document = ap.Document() + + # Add multiple pages to the PDF document + for i in range(1, 6): + pdf_document.pages.add() + + # Save the PDF document to the data directory + input_pdf_path = path.join(data_dir, "input.pdf") + pdf_document.save(input_pdf_path) + + # Create an instance of PdfFileEditor + pdf_file_editor = pdf_facades.PdfFileEditor() + + # Split the PDF document into single pages + output_pdf_path_pattern = path.join(data_dir, "output_page_%d.pdf") + pdf_file_editor.split(input_pdf_path, output_pdf_path_pattern, 1, 1) + +def run_all_examples(data_dir=None, license_path=None): + """Run all page splitting examples and report status. + + Args: + data_dir (str, optional): Input/output directory override. + license_path (str, optional): Path to Aspose.PDF license file. + + Returns: + None + """ + + set_license(license_path) + input_dir, output_dir = initialize_data_dir(data_dir) + + examples = [ + ("Split PDF from Beginning", split_pdf_from_beginning, "output_from_beginning.pdf"), + ("Split PDF to End", split_pdf_to_end, "output_to_end.pdf"), + ("Split PDF into Multiple Documents", split_pdf_into_multiple_documents, "output_split_1.pdf"), + ("Split PDF into Single Pages", split_pdf_into_single_pages, "output_page_1.pdf") + ] + + for name, func, data_file_name in examples: + try: + if (func.__name__ == "insert_pages_into_pdf" or func.__name__ == "append_pages_to_pdf"): + input_file_name = path.join(input_dir, "") + else: + input_file_name = path.join(input_dir, "f") + output_file_name = path.join(output_dir, data_file_name) + func(input_file_name, output_file_name) + print(f"✅ Success: {name}") + except Exception as e: + print(f"❌ Failed: {name} - {str(e)}") + + print("\nAll page splitting examples finished.") + + +if __name__ == "__main__": + run_all_examples() diff --git a/examples/facades_pdf_file_info/get-pdf-file-information_1.py b/examples/facades_pdf_file_info/get-pdf-file-information_1.py new file mode 100644 index 0000000..a46889d --- /dev/null +++ b/examples/facades_pdf_file_info/get-pdf-file-information_1.py @@ -0,0 +1,28 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffileinfo\get-pdf-file-information +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo + +def get_pdf_info(): + data_dir = RunExamples.get_data_dir_aspose_pdf() + + # Open PDF document + pdf_info = PdfFileInfo(data_dir + "sample.pdf") + + # Get and display PDF information + print(f"Subject: {pdf_info.subject}") + print(f"Title: {pdf_info.title}") + print(f"Keywords: {pdf_info.keywords}") + print(f"Creator: {pdf_info.creator}") + print(f"Creation Date: {pdf_info.creation_date}") + print(f"Modification Date: {pdf_info.mod_date}") + + # Check PDF status + print(f"Is Valid PDF: {pdf_info.is_pdf_file}") + print(f"Is Encrypted: {pdf_info.is_encrypted}") + + # Get dimensions of the first page (1-based index) + print(f"Page width: {pdf_info.get_page_width(1)}") + print(f"Page height: {pdf_info.get_page_height(1)}") diff --git a/examples/facades_pdf_file_info/get-pdf-file-information_2.py b/examples/facades_pdf_file_info/get-pdf-file-information_2.py new file mode 100644 index 0000000..2ebd370 --- /dev/null +++ b/examples/facades_pdf_file_info/get-pdf-file-information_2.py @@ -0,0 +1,22 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffileinfo\get-pdf-file-information +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo + +def get_meta_info(): + data_dir = RunExamples.get_data_dir_aspose_pdf() + + # Create PdfFileInfo object + pdf_info = PdfFileInfo(data_dir + "SetMetaInfo_out.pdf") + + # Retrieve all custom metadata (header dictionary) + meta_info = pdf_info.header + + # Enumerate and display all custom attributes + for key, value in meta_info.items(): + print(f"{key} {value}") + + # Retrieve and display a specific custom attribute + print("Reviewer:", pdf_info.get_meta_info("Reviewer")) diff --git a/examples/facades_pdf_file_info/set-pdf-file-information_1.py b/examples/facades_pdf_file_info/set-pdf-file-information_1.py new file mode 100644 index 0000000..b34abf2 --- /dev/null +++ b/examples/facades_pdf_file_info/set-pdf-file-information_1.py @@ -0,0 +1,21 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffileinfo\set-pdf-file-information +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo + +def set_pdf_info(): + data_dir = RunExamples.get_data_dir_aspose_pdf() + + # Create PdfFileInfo object to work with PDF metadata + pdf_info = PdfFileInfo(data_dir + "sample.pdf") + + # Set PDF information + pdf_info.author = "Aspose" + pdf_info.title = "Hello World!" + pdf_info.keywords = "Peace and Development" + pdf_info.creator = "Aspose" + + # Save the PDF with updated information + pdf_info.save_new_info(data_dir + "SetFileInfo_out.pdf") diff --git a/examples/facades_pdf_file_info/set-pdf-file-information_2.py b/examples/facades_pdf_file_info/set-pdf-file-information_2.py new file mode 100644 index 0000000..338fbc6 --- /dev/null +++ b/examples/facades_pdf_file_info/set-pdf-file-information_2.py @@ -0,0 +1,18 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffileinfo\set-pdf-file-information +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo + +def set_meta_info(): + data_dir = RunExamples.get_data_dir_aspose_pdf() + + # Create PdfFileInfo object + pdf_info = PdfFileInfo(data_dir + "sample.pdf") + + # Set a custom metadata attribute + pdf_info.set_meta_info("Reviewer", "Aspose.PDF user") + + # Save the updated PDF + pdf_info.save_new_info(data_dir + "SetMetaInfo_out.pdf") diff --git a/examples/facades_pdf_file_security/change-password_1.py b/examples/facades_pdf_file_security/change-password_1.py new file mode 100644 index 0000000..d4ccebe --- /dev/null +++ b/examples/facades_pdf_file_security/change-password_1.py @@ -0,0 +1,32 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\change-password +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo, PdfFileSecurity, DocumentPrivilege, KeySize + +def change_password(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + pdf_path = data_dir + "sample_encrypted.pdf" + output_path = data_dir + "sample_encrypted1.pdf" + + # Check if the PDF is encrypted + pdf_info = PdfFileInfo(pdf_path) + if pdf_info.is_encrypted: + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(pdf_path) + + # Change password + file_security.change_password( + "OwnerP@ssw0rd", # current owner password + "Pa$$w0rd1", # new user password + "Pa$$w0rd2", # new owner password + DocumentPrivilege.print, + KeySize.x256 + ) + + # Save updated PDF + file_security.save(output_path) diff --git a/examples/facades_pdf_file_security/control-exception_1.py b/examples/facades_pdf_file_security/control-exception_1.py new file mode 100644 index 0000000..a389b90 --- /dev/null +++ b/examples/facades_pdf_file_security/control-exception_1.py @@ -0,0 +1,28 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\control-exception +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSecurity + +def control_exception_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + pdf_path = data_dir + "sample_encrypted.pdf" + output_path = data_dir + "SampleDecrtypted_out.pdf" + + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(pdf_path) + + # Disallow exceptions (handle errors manually) + file_security.allow_exceptions = False + + # Attempt to decrypt with an incorrect password + if not file_security.decrypt_file("IncorrectPassword"): + print("Something wrong...") + print(f"Last exception: {file_security.last_exception.message}") + + # Save PDF document (will save only if decryption succeeds) + file_security.save(output_path) diff --git a/examples/facades_pdf_file_security/control-exception_2.py b/examples/facades_pdf_file_security/control-exception_2.py new file mode 100644 index 0000000..41d87c4 --- /dev/null +++ b/examples/facades_pdf_file_security/control-exception_2.py @@ -0,0 +1,30 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\control-exception +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSecurity + +def control_exception_pdf_file2(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + pdf_path = data_dir + "sample_encrypted.pdf" + output_path = data_dir + "SampleDecrtypted_out.pdf" + + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(pdf_path) + + # Allow exceptions (raise errors automatically) + file_security.allow_exceptions = True + + try: + # Attempt to decrypt PDF document + file_security.decrypt_file("IncorrectPassword") + except Exception as ex: + print("Something wrong...") + print(f"Exception: {ex}") + + # Save PDF document + file_security.save(output_path) diff --git a/examples/facades_pdf_file_security/decrypt-pdf_1.py b/examples/facades_pdf_file_security/decrypt-pdf_1.py new file mode 100644 index 0000000..a6cd707 --- /dev/null +++ b/examples/facades_pdf_file_security/decrypt-pdf_1.py @@ -0,0 +1,26 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\decrypt-pdf +# Code fence language: python + + +from aspose.pdf.facades import PdfFileInfo, PdfFileSecurity + +def decrypt_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "sample_encrypted.pdf" + output_pdf = data_dir + "SampleDecrtypted_out.pdf" + + # Check whether the PDF is encrypted + pdf_info = PdfFileInfo(input_pdf) + if pdf_info.is_encrypted: + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(input_pdf) + + # Decrypt PDF document using password + file_security.decrypt_file("P@ssw0rd") + + # Save decrypted PDF + file_security.save(output_pdf) diff --git a/examples/facades_pdf_file_security/encrypt-pdf_1.py b/examples/facades_pdf_file_security/encrypt-pdf_1.py new file mode 100644 index 0000000..d51bbd7 --- /dev/null +++ b/examples/facades_pdf_file_security/encrypt-pdf_1.py @@ -0,0 +1,34 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\encrypt-pdf +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileSecurity, + DocumentPrivilege, + KeySize, + Algorithm +) + +def encrypt_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "input.pdf" + output_pdf = data_dir + "SampleEncrypted_out.pdf" + + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(input_pdf) + + # Encrypt PDF using 256-bit AES encryption + file_security.encrypt_file( + "User_P@ssw0rd", # user password + "OwnerP@ssw0rd", # owner password + DocumentPrivilege.print, # permissions + KeySize.x256, # key size + Algorithm.AES # encryption algorithm + ) + + # Save encrypted PDF + file_security.save(output_pdf) diff --git a/examples/facades_pdf_file_security/set-privileges_1.py b/examples/facades_pdf_file_security/set-privileges_1.py new file mode 100644 index 0000000..e714521 --- /dev/null +++ b/examples/facades_pdf_file_security/set-privileges_1.py @@ -0,0 +1,29 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\set-privileges +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSecurity, DocumentPrivilege + +def set_privilege_1(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "sample.pdf" + output_pdf = data_dir + "SamplePrivileges_out.pdf" + + # Create DocumentPrivilege object and configure permissions + privilege = DocumentPrivilege.forbid_all + privilege.change_allow_level = 1 + privilege.allow_print = True + privilege.allow_copy = True + + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(input_pdf) + + # Apply privileges + file_security.set_privilege(privilege) + + # Save PDF with updated privileges + file_security.save(output_pdf) diff --git a/examples/facades_pdf_file_security/set-privileges_2.py b/examples/facades_pdf_file_security/set-privileges_2.py new file mode 100644 index 0000000..d61343b --- /dev/null +++ b/examples/facades_pdf_file_security/set-privileges_2.py @@ -0,0 +1,34 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\set-privileges +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSecurity, DocumentPrivilege + +def set_privilege_with_password(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "sample.pdf" + output_pdf = data_dir + "SamplePrivilegesPassword_out.pdf" + + # Create DocumentPrivilege object and configure permissions + privilege = DocumentPrivilege.forbid_all + privilege.change_allow_level = 1 + privilege.allow_print = True + privilege.allow_copy = True + + file_security = PdfFileSecurity() + + # Bind PDF document + file_security.bind_pdf(input_pdf) + + # Set privilege with passwords + # user_password is empty, owner_password is set + file_security.set_privilege( + "", # user password + "P@ssw0rd", # owner password + privilege + ) + + # Save PDF with updated privileges + file_security.save(output_pdf) diff --git a/examples/facades_pdf_file_security/set-privileges_3.py b/examples/facades_pdf_file_security/set-privileges_3.py new file mode 100644 index 0000000..5aa27b6 --- /dev/null +++ b/examples/facades_pdf_file_security/set-privileges_3.py @@ -0,0 +1,24 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesecurity\set-privileges +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def remove_extended_rights(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + + input_pdf = data_dir + "DigitallySign.pdf" + output_pdf = data_dir + "RemoveRights_out.pdf" + + pdf_sign = PdfFileSignature() + + # Bind PDF document + pdf_sign.bind_pdf(input_pdf) + + # Check and remove usage rights if present + if pdf_sign.contains_usage_rights(): + pdf_sign.remove_usage_rights() + + # Save updated PDF document + pdf_sign.document.save(output_pdf) diff --git a/examples/facades_pdf_file_signature/add-signature-in-pdf-file_1.py b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_1.py new file mode 100644 index 0000000..e71fe51 --- /dev/null +++ b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_1.py @@ -0,0 +1,39 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\add-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileSignature, + PKCS7Detached, + Rectangle +) + +def add_signature_to_pdf(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "input.pdf" + output_pdf = data_dir + "SignedOutput.pdf" + cert_file = data_dir + "certificate.pfx" + cert_password = "your_cert_password" + + # Create PdfFileSignature object + pdf_sign = PdfFileSignature() + + # Bind PDF document (input & output) + pdf_sign.bind_pdf(input_pdf, output_pdf) + + # Define signature placement (page 1, rectangle coordinates) + sig_rect = Rectangle(100, 500, 300, 650) + + # Create PKCS7Detached signature using certificate + signature = PKCS7Detached(cert_file, cert_password) + + # Optionally set signature appearance + pdf_sign.signature_appearance = data_dir + "signature_image.jpg" + + # Sign the PDF + pdf_sign.sign(1, cert_file, cert_password, "Reason for signing", "Contact", "Location", sig_rect, signature) + + # Save the signed document + pdf_sign.save() diff --git a/examples/facades_pdf_file_signature/add-signature-in-pdf-file_2.py b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_2.py new file mode 100644 index 0000000..921e252 --- /dev/null +++ b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_2.py @@ -0,0 +1,59 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\add-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1 +from aspose.pdf import Rectangle + +def add_two_signatures(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "input.pdf" + cert_file = data_dir + "rsa_cert.pfx" + cert_password = "12345" + + # Create PdfFileSignature object + pdf_signature = PdfFileSignature() + + # ========================= + # First signature + # ========================= + pdf_signature.bind_pdf(input_pdf) + + rect1 = Rectangle(10, 10, 300, 50) + signature1 = PKCS1(cert_file, cert_password) + + pdf_signature.sign( + 1, + "I'm document author", + "test@aspose-pdf-demo.local", + "Aspose Pdf Demo, Australia", + True, + rect1, + signature1 + ) + + first_signed_pdf = data_dir + "DigitallySign_out.pdf" + pdf_signature.save(first_signed_pdf) + + # ========================= + # Second signature + # ========================= + pdf_signature.bind_pdf(first_signed_pdf) + + rect2 = Rectangle(10, 10, 300, 50) + signature2 = PKCS1(cert_file, cert_password) + + pdf_signature.sign( + 2, + "I'm document reviewer", + "test02@aspose-pdf-demo.local", + "Aspose Pdf Demo, Australia", + True, + rect2, + signature2 + ) + + pdf_signature.save(data_dir + "DigitallySign2_out.pdf") diff --git a/examples/facades_pdf_file_signature/add-signature-in-pdf-file_3.py b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_3.py new file mode 100644 index 0000000..09f730d --- /dev/null +++ b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_3.py @@ -0,0 +1,35 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\add-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1, SignatureCustomAppearance + +def add_pdf_file_signature_field(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "input.pdf" + output_pdf = data_dir + "DigitallySign_out.pdf" + cert_file = data_dir + "rsa_cert.pfx" + cert_password = "12345" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Create PKCS#1 signature with custom appearance + signature = PKCS1(cert_file, cert_password) + signature.reason = "Sign as Author" + + appearance = SignatureCustomAppearance() + appearance.font_size = 6 + appearance.font_family_name = "Calibri" + signature.custom_appearance = appearance + + # Sign PDF using a named signature field + pdf_signature.sign("Signature1", signature) + + # Save signed PDF + pdf_signature.save(output_pdf) diff --git a/examples/facades_pdf_file_signature/add-signature-in-pdf-file_4.py b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_4.py new file mode 100644 index 0000000..ee8a3d8 --- /dev/null +++ b/examples/facades_pdf_file_signature/add-signature-in-pdf-file_4.py @@ -0,0 +1,47 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\add-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1, SignatureCustomAppearance + +def add_pdf_file_signature_field2(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "input.pdf" + cert_file = data_dir + "rsa_cert.pfx" + cert_password = "12345" + + pdf_signature = PdfFileSignature() + + # ========================= + # First signature field + # ========================= + pdf_signature.bind_pdf(input_pdf) + + signature1 = PKCS1(cert_file, cert_password) + signature1.reason = "Sign as Author" + + appearance1 = SignatureCustomAppearance() + appearance1.font_size = 6 + signature1.custom_appearance = appearance1 + + pdf_signature.sign("Signature1", signature1) + first_signed_pdf = data_dir + "DigitallySign_out.pdf" + pdf_signature.save(first_signed_pdf) + + # ========================= + # Second signature field + # ========================= + pdf_signature.bind_pdf(first_signed_pdf) + + signature2 = PKCS1(cert_file, cert_password) + signature2.reason = "Sign as Reviewer" + + appearance2 = SignatureCustomAppearance() + appearance2.font_size = 6 + signature2.custom_appearance = appearance2 + + pdf_signature.sign("Signature2", signature2) + pdf_signature.save(data_dir + "DigitallySign2_out.pdf") diff --git a/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_1.py b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_1.py new file mode 100644 index 0000000..ad93809 --- /dev/null +++ b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_1.py @@ -0,0 +1,28 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\remove-signature-from-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def remove_signature(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + output_pdf = data_dir + "RemoveSignature_out.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Get list of signature names + signature_names = pdf_signature.get_sign_names() + + # Remove all signatures + for name in signature_names: + print(f"Removed {name}") + pdf_signature.remove_signature(name) + + # Save PDF without signatures + pdf_signature.save(output_pdf) diff --git a/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_2.py b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_2.py new file mode 100644 index 0000000..a5a047d --- /dev/null +++ b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_2.py @@ -0,0 +1,23 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\remove-signature-from-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def remove_signature_but_keep_field(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + output_pdf = data_dir + "RemoveSignature_out.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Remove signature but keep the signature field + pdf_signature.remove_signature("Signature1", False) + + # Save updated PDF + pdf_signature.save(output_pdf) diff --git a/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_3.py b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_3.py new file mode 100644 index 0000000..fbe3af5 --- /dev/null +++ b/examples/facades_pdf_file_signature/remove-signature-from-pdf-file_3.py @@ -0,0 +1,27 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\remove-signature-from-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def remove_signature_but_keep_field2(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + output_pdf = data_dir + "RemoveSignature_out.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Get all signature field names + signature_names = pdf_signature.get_signature_names() + + # Remove signatures but keep fields + for sig_name in signature_names: + pdf_signature.remove_signature(sig_name, False) + + # Save updated PDF + pdf_signature.save(output_pdf) diff --git a/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_1.py b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_1.py new file mode 100644 index 0000000..82b8eb2 --- /dev/null +++ b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_1.py @@ -0,0 +1,20 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\verify-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def is_pdf_signed(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Check if the document contains any signatures + if pdf_signature.contains_signature(): + print("Document Signed") diff --git a/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_2.py b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_2.py new file mode 100644 index 0000000..136d2eb --- /dev/null +++ b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_2.py @@ -0,0 +1,20 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\verify-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def is_pdf_signed_with_given_signature(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Verify specific signature field + if pdf_signature.verify_signature("Signature1"): + print("PDF Signed") diff --git a/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_3.py b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_3.py new file mode 100644 index 0000000..1416cd5 --- /dev/null +++ b/examples/facades_pdf_file_signature/verify-signature-in-pdf-file_3.py @@ -0,0 +1,20 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\verify-signature-in-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def is_pdf_signature_valid(): + data_dir = RunExamples.get_data_dir_aspose_pdf_security_signatures() + + input_pdf = data_dir + "signed_rsa.pdf" + + pdf_signature = PdfFileSignature() + + # Bind PDF document + pdf_signature.bind_pdf(input_pdf) + + # Verify the specified signature + if pdf_signature.verify_signature("Signature1"): + print("Signature Verified") diff --git a/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_1.py b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_1.py new file mode 100644 index 0000000..aed0fd0 --- /dev/null +++ b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_1.py @@ -0,0 +1,22 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\working-with-signature-in-a-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def extract_signature_info(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + + pdf_signature = PdfFileSignature() + pdf_signature.bind_pdf(data_dir + "signed_rsa.pdf") + + signature_names = pdf_signature.get_signature_names() + if signature_names: + sig_name = signature_names[0] + + # Extract certificate as a stream + cer_stream = pdf_signature.extract_certificate(sig_name) + if cer_stream is not None: + with open(data_dir + "extracted_cert.pfx", "wb") as fs: + fs.write(cer_stream.read()) diff --git a/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_2.py b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_2.py new file mode 100644 index 0000000..935ae84 --- /dev/null +++ b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_2.py @@ -0,0 +1,19 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\working-with-signature-in-a-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature + +def extract_signature_image(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + + signature = PdfFileSignature() + signature.bind_pdf(data_dir + "ExtractingImage.pdf") + + if signature.contains_signature(): + for sig_name in signature.get_signature_names(): + image_stream = signature.extract_image(sig_name) + if image_stream: + with open(data_dir + "ExtractedImage_out.jpg", "wb") as fs: + fs.write(image_stream.read()) diff --git a/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_3.py b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_3.py new file mode 100644 index 0000000..d829fd2 --- /dev/null +++ b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_3.py @@ -0,0 +1,29 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\working-with-signature-in-a-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1 +from aspose.pdf import Rectangle + +def suppress_location_and_reason(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + + pdf_signature = PdfFileSignature() + pdf_signature.bind_pdf(data_dir + "input.pdf") + + rect = Rectangle(10, 10, 300, 50) + signature = PKCS1(data_dir + "rsa_cert.pfx", "12345") + + # Suppress reason and location by leaving empty strings + pdf_signature.sign( + 1, + "", # empty reason + "test01@aspose-pdf-demo.local", + "", # empty location + True, + rect, + signature + ) + pdf_signature.save(data_dir + "DigitallySign_out.pdf") diff --git a/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_4.py b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_4.py new file mode 100644 index 0000000..9e80faa --- /dev/null +++ b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_4.py @@ -0,0 +1,26 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\working-with-signature-in-a-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1, SignatureCustomAppearance +from aspose.pdf import Rectangle + +def customize_signature_appearance(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + + pdf_signature = PdfFileSignature() + pdf_signature.bind_pdf(data_dir + "input.pdf") + + rect = Rectangle(10, 10, 300, 50) + signature = PKCS1(data_dir + "rsa_cert.pfx", "12345") + + appearance = SignatureCustomAppearance() + appearance.font_size = 6 + appearance.font_family_name = "Times New Roman" + appearance.digital_signed_label = "Signed by:" + signature.custom_appearance = appearance + + pdf_signature.sign(1, True, rect, signature) + pdf_signature.save(data_dir + "DigitallySign_out.pdf") diff --git a/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_5.py b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_5.py new file mode 100644 index 0000000..486824e --- /dev/null +++ b/examples/facades_pdf_file_signature/working-with-signature-in-a-pdf-file_5.py @@ -0,0 +1,32 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilesignature\working-with-signature-in-a-pdf-file +# Code fence language: python + + +from aspose.pdf.facades import PdfFileSignature +from aspose.pdf.forms import PKCS1, SignatureCustomAppearance +from aspose.pdf import Rectangle + +def signature_with_image(): + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_security_signatures() + image_path = data_dir + "aspose-logo.jpg" + + pdf_signature = PdfFileSignature() + pdf_signature.bind_pdf(data_dir + "input.pdf") + + rect = Rectangle(10, 10, 300, 50) + signature = PKCS1(data_dir + "rsa_cert.pfx", "12345") + + appearance = SignatureCustomAppearance() + appearance.font_size = 6 + appearance.font_family_name = "Times New Roman" + appearance.digital_signed_label = "Signed by:" + appearance.is_foreground_image = True # show image on top + + signature.custom_appearance = appearance + + # Set the appearance image + pdf_signature.signature_appearance = image_path + + pdf_signature.sign(1, True, rect, signature) + pdf_signature.save(data_dir + "DigitallySign_out.pdf") diff --git a/examples/facades_pdf_file_stamp/add-pdf-page-stamp_1.py b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_1.py new file mode 100644 index 0000000..6f7a208 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_1.py @@ -0,0 +1,35 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-pdf-page-stamp +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp, Stamp + +def add_page_stamp_on_all_pages(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "SourcePDF.pdf") + + # Create stamp object + stamp = Stamp() + + # Bind PDF page to be used as stamp (page 1) + stamp.bind_pdf(data_dir + "AddPageStampOnAllPages.pdf", 1) + + # Set stamp position and appearance + stamp.set_origin(20, 20) + stamp.rotation = 90.0 + stamp.is_background = True + + # Add stamp to all pages + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "PageStampOnAllPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-pdf-page-stamp_2.py b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_2.py new file mode 100644 index 0000000..f25991e --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_2.py @@ -0,0 +1,38 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-pdf-page-stamp +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp, Stamp + +def add_page_stamp_on_certain_pages(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "SourcePDF.pdf") + + # Create stamp object + stamp = Stamp() + + # Bind PDF page to be used as stamp (page 1) + stamp.bind_pdf(data_dir + "PageStampOnCertainPages.pdf", 1) + + # Configure stamp properties + stamp.set_origin(20, 20) + stamp.rotation = 90.0 + stamp.is_background = True + + # Apply stamp only to selected pages (1 and 3) + stamp.pages = [1, 3] + + # Add stamp to the PDF + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "PageStampOnCertainPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-pdf-page-stamp_3.py b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_3.py new file mode 100644 index 0000000..9eb6331 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_3.py @@ -0,0 +1,56 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-pdf-page-stamp +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + PdfFileInfo, + FormattedText, + FontStyle, + EncodingType +) +from aspose.pdf.facades import PageNumPosition +from System.Drawing import Color + +def add_page_number_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + input_pdf = data_dir + "StampPDF.pdf" + output_pdf = data_dir + "AddPageNumber_out.pdf" + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind PDF document + file_stamp.bind_pdf(input_pdf) + + # Get total number of pages + pdf_info = PdfFileInfo(input_pdf) + total_pages = pdf_info.number_of_pages + + # Create formatted text for page number ("#" is replaced by current page number) + formatted_text = FormattedText( + f"Page # of {total_pages}", + Color.AntiqueWhite, + Color.Gray, + FontStyle.times_bold_italic, + EncodingType.winansi, + False, + 12 + ) + + # Set starting page number + file_stamp.starting_number = 1 + + # Add page number at upper-right position + file_stamp.add_page_number( + formatted_text, + PageNumPosition.pos_upper_right + ) + + # Save output PDF + file_stamp.save(output_pdf) + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-pdf-page-stamp_4.py b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_4.py new file mode 100644 index 0000000..7e7b5f0 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-pdf-page-stamp_4.py @@ -0,0 +1,61 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-pdf-page-stamp +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + PdfFileInfo, + FormattedText, + FontStyle, + EncodingType, + PageNumPosition +) +from aspose.pdf import NumberingStyle +from System.Drawing import Color + +def add_custom_page_number_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + input_pdf = data_dir + "StampPDF.pdf" + output_pdf = data_dir + "AddCustomPageNumber_out.pdf" + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind PDF document + file_stamp.bind_pdf(input_pdf) + + # Get total number of pages + pdf_info = PdfFileInfo(input_pdf) + total_pages = pdf_info.number_of_pages + + # Create formatted text for page number + # "#" will be replaced by the current page number + formatted_text = FormattedText( + f"Page # of {total_pages}", + Color.AntiqueWhite, + Color.Gray, + FontStyle.times_bold_italic, + EncodingType.winansi, + False, + 12 + ) + + # Specify numbering style (Roman numerals, uppercase) + file_stamp.numbering_style = NumberingStyle.numerals_roman_uppercase + + # Set starting page number + file_stamp.starting_number = 1 + + # Add page number in the upper-right corner + file_stamp.add_page_number( + formatted_text, + PageNumPosition.pos_upper_right + ) + + # Save output PDF + file_stamp.save(output_pdf) + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-text-and-image-stamp_1.py b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_1.py new file mode 100644 index 0000000..b743402 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_1.py @@ -0,0 +1,51 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-text-and-image-stamp +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + Stamp, + FormattedText, + FontStyle, + EncodingType +) +from System.Drawing import Color + +def add_text_stamp_on_all_pages_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create stamp object + stamp = Stamp() + + # Create formatted text and bind it as a logo (text stamp) + text = FormattedText( + "Hello World!", + Color.Blue, + Color.Gray, + FontStyle.helvetica, + EncodingType.winansi, + True, + 14 + ) + stamp.bind_logo(text) + + # Configure stamp properties + stamp.set_origin(10, 400) + stamp.rotation = 90.0 + stamp.is_background = True + + # Add stamp to all pages + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "AddTextStampOnAllPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-text-and-image-stamp_2.py b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_2.py new file mode 100644 index 0000000..75eda6b --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_2.py @@ -0,0 +1,54 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-text-and-image-stamp +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + Stamp, + FormattedText, + FontStyle, + EncodingType +) +from System.Drawing import Color + +def add_text_stamp_on_particular_pages_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create stamp object + stamp = Stamp() + + # Create formatted text and bind it as a logo (text stamp) + text = FormattedText( + "Hello World!", + Color.Blue, + Color.Gray, + FontStyle.helvetica, + EncodingType.winansi, + True, + 14 + ) + stamp.bind_logo(text) + + # Configure stamp properties + stamp.set_origin(10, 400) + stamp.rotation = 90.0 + stamp.is_background = True + + # Apply stamp only to selected pages (page 2) + stamp.pages = [2] + + # Add stamp to the PDF + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "AddTextStampOnParticularPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-text-and-image-stamp_3.py b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_3.py new file mode 100644 index 0000000..ae4d902 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_3.py @@ -0,0 +1,39 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-text-and-image-stamp +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp, Stamp + +def add_image_stamp_on_all_pages_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create stamp object + stamp = Stamp() + + # Bind image to stamp + stamp.bind_image(data_dir + "StampImage.png") + + # Configure stamp properties + stamp.set_origin(10, 200) + stamp.rotation = 90.0 + stamp.is_background = True + + # OPTIONAL: + # If you want to apply the stamp only to selected pages, uncomment below + # stamp.pages = [2] + + # Add stamp to PDF file (applies to all pages by default) + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "AddImageStampOnAllPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/add-text-and-image-stamp_4.py b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_4.py new file mode 100644 index 0000000..af37c23 --- /dev/null +++ b/examples/facades_pdf_file_stamp/add-text-and-image-stamp_4.py @@ -0,0 +1,38 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\add-text-and-image-stamp +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp, Stamp + +def add_image_stamp_on_particular_pages_in_pdf_file(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + # Create PdfFileStamp object + file_stamp = PdfFileStamp() + + # Bind source PDF document + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create stamp object + stamp = Stamp() + + # Bind image to stamp + stamp.bind_image(data_dir + "StampImage.png") + + # Configure stamp properties + stamp.set_origin(10, 200) + stamp.rotation = 90.0 + stamp.is_background = True + + # Apply stamp only to selected pages (e.g., page 2) + stamp.pages = [2] + + # Add stamp to PDF file + file_stamp.add_stamp(stamp) + + # Save output PDF + file_stamp.save(data_dir + "AddImageStampOnParticularPages_out.pdf") + + # Close the stamp object + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/manage-header-and-footer_1.py b/examples/facades_pdf_file_stamp/manage-header-and-footer_1.py new file mode 100644 index 0000000..f173841 --- /dev/null +++ b/examples/facades_pdf_file_stamp/manage-header-and-footer_1.py @@ -0,0 +1,36 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\manage-header-and-footer +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + FormattedText, + FontStyle, + EncodingType +) +from System.Drawing import Color + +def add_header(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + file_stamp = PdfFileStamp() + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create formatted text for header + header_text = FormattedText( + "Aspose - Your File Format Experts!", + Color.Yellow, + Color.Black, + FontStyle.courier, + EncodingType.winansi, + False, + 14 + ) + + # Add header with top margin + file_stamp.add_header(header_text, 10) + + # Save output PDF + file_stamp.save(data_dir + "AddHeader_out.pdf") + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/manage-header-and-footer_2.py b/examples/facades_pdf_file_stamp/manage-header-and-footer_2.py new file mode 100644 index 0000000..09a0657 --- /dev/null +++ b/examples/facades_pdf_file_stamp/manage-header-and-footer_2.py @@ -0,0 +1,36 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\manage-header-and-footer +# Code fence language: python + + +from aspose.pdf.facades import ( + PdfFileStamp, + FormattedText, + FontStyle, + EncodingType +) +from System.Drawing import Color + +def add_footer(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + file_stamp = PdfFileStamp() + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Create formatted text for footer + footer_text = FormattedText( + "Aspose - Your File Format Experts!", + Color.Blue, + Color.Gray, + FontStyle.courier, + EncodingType.winansi, + False, + 14 + ) + + # Add footer with bottom margin + file_stamp.add_footer(footer_text, 10) + + # Save output PDF + file_stamp.save(data_dir + "AddFooter_out.pdf") + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/manage-header-and-footer_3.py b/examples/facades_pdf_file_stamp/manage-header-and-footer_3.py new file mode 100644 index 0000000..7a13695 --- /dev/null +++ b/examples/facades_pdf_file_stamp/manage-header-and-footer_3.py @@ -0,0 +1,20 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\manage-header-and-footer +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp + +def add_image_header(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + file_stamp = PdfFileStamp() + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Open image stream and add as header + with open(data_dir + "ImageHeader.png", "rb") as image_stream: + file_stamp.add_header(image_stream, 10) + + # Save output PDF + file_stamp.save(data_dir + "AddImageHeader_out.pdf") + file_stamp.close() diff --git a/examples/facades_pdf_file_stamp/manage-header-and-footer_4.py b/examples/facades_pdf_file_stamp/manage-header-and-footer_4.py new file mode 100644 index 0000000..442efa0 --- /dev/null +++ b/examples/facades_pdf_file_stamp/manage-header-and-footer_4.py @@ -0,0 +1,20 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdffilestamp\manage-header-and-footer +# Code fence language: python + + +from aspose.pdf.facades import PdfFileStamp + +def add_image_footer(): + data_dir = RunExamples.get_data_dir_aspose_pdf_images() + + file_stamp = PdfFileStamp() + file_stamp.bind_pdf(data_dir + "sample.pdf") + + # Open image stream and add as footer + with open(data_dir + "ImageFooter.png", "rb") as image_stream: + file_stamp.add_footer(image_stream, 10) + + # Save output PDF + file_stamp.save(data_dir + "AddImageFooter_out.pdf") + file_stamp.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_1.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_1.py new file mode 100644 index 0000000..3f673b8 --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_1.py @@ -0,0 +1,33 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +# Prepare viewer +viewer = pdf.facades.PdfViewer() + +# Bind the PDF (open) +viewer.bind_pdf("PrintDocument.pdf") + +# Adjust settings +viewer.auto_resize = True +viewer.auto_rotate = True +viewer.print_page_dialog = False + +# Create printer and page settings +ps = pdf.printing.PrinterSettings() +pgs = pdf.printing.PageSettings() + +# You can explicitly specify printer name (optional) +# ps.printer_name = "Your Printer Name" + +# Example: Set A4 page size +pgs.paper_size = pdf.printing.PaperSizes.A4 + +# Print +viewer.print_document_with_settings(pgs, ps) + +# Release resources +viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_2.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_2.py new file mode 100644 index 0000000..83a1125 --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_2.py @@ -0,0 +1,43 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf +import System +from System.Windows.Forms import PrintDialog, DialogResult + +def printing_pdf_display_print_dialog(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + + # Create PdfViewer object + viewer = pdf.facades.PdfViewer() + + try: + # Bind PDF document + viewer.bind_pdf(data_dir + "PrintDocument.pdf") + + # Set attributes for printing + viewer.auto_resize = True + viewer.auto_rotate = True + + # Show system print dialog + print_dialog = PrintDialog() + + if print_dialog.ShowDialog() == DialogResult.OK: + # Convert .NET PrinterSettings to Aspose equivalents + ps = pdf.printing.PrinterSettings.to_aspose_printer_settings( + print_dialog.PrinterSettings + ) + + pgs = pdf.printing.PageSettings.to_aspose_page_settings( + print_dialog.PrinterSettings.DefaultPageSettings + ) + + # Print document using selected printer and page settings + viewer.print_document_with_settings(pgs, ps) + + finally: + # Release resources + viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_3.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_3.py new file mode 100644 index 0000000..2c3da36 --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_3.py @@ -0,0 +1,26 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +viewer = pdf.facades.PdfViewer() +viewer.bind_pdf("PrintDocument.pdf") + +viewer.auto_resize = True +viewer.auto_rotate = True +viewer.print_page_dialog = False + +ps = pdf.printing.PrinterSettings() +pgs = pdf.printing.PageSettings() + +# Set soft printer and print to file +ps.printer_name = "Adobe PDF" # Or another virtual printer +ps.print_file_name = "OutFile.pdf" +ps.print_to_file = True + +pgs.paper_size = pdf.printing.PaperSizes.A4 + +viewer.print_document_with_settings(pgs, ps) +viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_4.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_4.py new file mode 100644 index 0000000..a6d4ded --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_4.py @@ -0,0 +1,22 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +viewer = pdf.facades.PdfViewer() +viewer.bind_pdf("PrintDocument.pdf") + +viewer.auto_resize = True +viewer.auto_rotate = True +viewer.print_page_dialog = False +viewer.print_as_grayscale = True # Print in grayscale + +ps = pdf.printing.PrinterSettings() +pgs = pdf.printing.PageSettings() +ps.printer_name = "Microsoft XPS Document Writer" +pgs.paper_size = pdf.printing.PaperSizes.A4 + +viewer.print_document_with_settings(pgs, ps) +viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_5.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_5.py new file mode 100644 index 0000000..9574d0f --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_5.py @@ -0,0 +1,45 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +def printing_pdf_hide_print_dialog(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + + # Create PdfViewer object + viewer = pdf.facades.PdfViewer() + + try: + # Bind PDF document + viewer.bind_pdf(data_dir + "PrintDocument.pdf") + + # Set attributes for printing + # Print the file with adjusted size + viewer.auto_resize = True + # Print the file with adjusted rotation + viewer.auto_rotate = True + # Do not show the page number dialog + viewer.print_page_dialog = False + + # Create printer and page settings + ps = pdf.printing.PrinterSettings() + pgs = pdf.printing.PageSettings() + + # Set XPS/PDF printer name + ps.printer_name = "OneNote for Windows 10" + + # Set page size (A4) + pgs.paper_size = pdf.printing.PaperSizes.A4 + + # Set page margins (left, right, top, bottom) + pgs.margins = pdf.devices.Margins(0, 0, 0, 0) + + # Print document using printer and page settings + viewer.print_document_with_settings(pgs, ps) + + finally: + # Close viewer and release resources + viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_6.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_6.py new file mode 100644 index 0000000..25e34de --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_6.py @@ -0,0 +1,47 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +def printing_pdf_to_postscript(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + + # Create PdfViewer object + viewer = pdf.facades.PdfViewer() + + try: + # Bind PDF document + viewer.bind_pdf(data_dir + "PrintDocument.pdf") + + # Set attributes for printing + viewer.auto_resize = True + viewer.auto_rotate = True + viewer.print_page_dialog = False + viewer.print_as_image = False # Do NOT convert pages to images + + # Create printer and page settings + ps = pdf.printing.PrinterSettings() + pgs = pdf.printing.PageSettings() + + # Set PostScript printer name + ps.printer_name = "HP Universal Printing PS (v7.0.0)" + + # Set output file and enable PrintToFile + ps.print_file_name = data_dir + "PdfToPostScript_out.ps" + ps.print_to_file = True + + # Set page size (A4) + pgs.paper_size = pdf.printing.PaperSizes.A4 + + # Set page margins (left, right, top, bottom) + pgs.margins = pdf.devices.Margins(0, 0, 0, 0) + + # Print document using printer and page settings + viewer.print_document_with_settings(pgs, ps) + + finally: + # Release resources + viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_7.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_7.py new file mode 100644 index 0000000..cea4edf --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_7.py @@ -0,0 +1,55 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf + +def checking_print_job_status(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + + # Instantiate PdfViewer object + viewer = pdf.facades.PdfViewer() + + try: + # Bind PDF document + viewer.bind_pdf(data_dir + "PrintDocument.pdf") + + # Set attributes for printing + viewer.auto_resize = True + viewer.auto_rotate = True + viewer.print_page_dialog = False + viewer.print_as_image = False + + # Create printer and page settings + ps = pdf.printing.PrinterSettings() + pgs = pdf.printing.PageSettings() + + # Specify the printer name + ps.printer_name = "Microsoft XPS Document Writer" + + # Set output file name and enable PrintToFile + ps.print_file_name = data_dir + "CheckingPrintJobStatus_out.xps" + ps.print_to_file = True + + # Set page size (A4) + pgs.paper_size = pdf.printing.PaperSizes.A4 + + # Set page margins + pgs.margins = pdf.devices.Margins(0, 0, 0, 0) + + # Print document using printer and page settings + viewer.print_document_with_settings(pgs, ps) + + # Check the print status + if viewer.print_status is not None: + # An exception was thrown during printing + print(str(viewer.print_status)) + else: + # Printing completed successfully + print("Printing completed without any issue.") + + finally: + # Release resources + viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_8.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_8.py new file mode 100644 index 0000000..e17a66f --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_8.py @@ -0,0 +1,108 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf +import os + +class PrintingJobSettings: + def __init__(self, from_page, to_page, output_file, duplex_mode): + self.from_page = from_page + self.to_page = to_page + self.output_file = output_file + self.mode = duplex_mode + + +def printing_pages_in_simplex_and_duplex_mode(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + output_dir = data_dir + + printing_job_index = 0 + printing_jobs = [] + + # Create printing jobs + printing_jobs.append( + PrintingJobSettings( + from_page=1, + to_page=3, + output_file=output_dir + "PrintPageRange_p1-3_out.xps", + duplex_mode=pdf.printing.Duplex.Default + ) + ) + + printing_jobs.append( + PrintingJobSettings( + from_page=4, + to_page=6, + output_file=output_dir + "PrintPageRange_p4-6_out.xps", + duplex_mode=pdf.printing.Duplex.Simplex + ) + ) + + printing_jobs.append( + PrintingJobSettings( + from_page=7, + to_page=7, + output_file=output_dir + "PrintPageRange_p7_out.xps", + duplex_mode=pdf.printing.Duplex.Default + ) + ) + + # Create PdfViewer object + viewer = pdf.facades.PdfViewer() + + try: + # Bind PDF document + viewer.bind_pdf(data_dir + "Print-PageRange.pdf") + + # Set printing attributes + viewer.auto_resize = True + viewer.auto_rotate = True + viewer.print_page_dialog = False + + # Create printer and page settings + ps = pdf.printing.PrinterSettings() + pgs = pdf.printing.PageSettings() + + # Set printer name + ps.printer_name = "Microsoft XPS Document Writer" + + # Set initial job settings + ps.print_to_file = True + ps.print_range = pdf.printing.PrintRange.SomePages + + # Paper size and margins + pgs.paper_size = pdf.printing.PaperSizes.A4 + ps.default_page_settings.paper_size = pgs.paper_size + pgs.margins = pdf.devices.Margins(0, 0, 0, 0) + + # Helper to apply a print job + def apply_print_job(index): + job = printing_jobs[index] + ps.print_file_name = os.path.abspath(job.output_file) + ps.from_page = job.from_page + ps.to_page = job.to_page + ps.duplex = job.mode + + # Apply first job + apply_print_job(printing_job_index) + + # EndPrint event handler (chain next jobs) + def on_end_print(sender, args): + nonlocal printing_job_index + printing_job_index += 1 + + if printing_job_index < len(printing_jobs): + apply_print_job(printing_job_index) + viewer.print_document_with_settings(pgs, ps) + + viewer.end_print += on_end_print + + # Start first print job + viewer.print_document_with_settings(pgs, ps) + + finally: + # Release resources + viewer.close() diff --git a/examples/facades_pdf_viewer/working-with-pdf-printing-facades_9.py b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_9.py new file mode 100644 index 0000000..77a1220 --- /dev/null +++ b/examples/facades_pdf_viewer/working-with-pdf-printing-facades_9.py @@ -0,0 +1,37 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\pdfviewer\working-with-pdf-printing-facades +# Code fence language: python + + +import aspose.pdf as pdf +import os + +def printing_multiple_documents_in_single_job(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_printing() + + # Paths to documents to be printed + path1 = os.path.join(data_dir, "PrintDocument.pdf") + path2 = os.path.join(data_dir, "Print-PageRange.pdf") + path3 = os.path.join(data_dir, "35925_1_3.xps") + + # Create printer settings + printer_settings = pdf.printing.PrinterSettings() + + # Use default system printer (same idea as PrintDocument.PrinterSettings.PrinterName) + # If you want to force a printer, uncomment and set it explicitly: + # printer_settings.printer_name = "Microsoft XPS Document Writer" + + # Create page settings + page_settings = pdf.printing.PageSettings() + page_settings.paper_size = pdf.printing.PaperSizes.A4 + page_settings.margins = pdf.devices.Margins(0, 0, 0, 0) + + # Print multiple documents in a single print job + pdf.facades.PdfViewer.print_documents( + printer_settings, + page_settings, + path1, + path2, + path3 + ) diff --git a/examples/facades_stamp/adding-multi-line-watermark-to-existing-pdf_1.py b/examples/facades_stamp/adding-multi-line-watermark-to-existing-pdf_1.py new file mode 100644 index 0000000..17e479d --- /dev/null +++ b/examples/facades_stamp/adding-multi-line-watermark-to-existing-pdf_1.py @@ -0,0 +1,30 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\stamp\adding-multi-line-watermark-to-existing-pdf +# Code fence language: python + + +import aspose.pdf as pdf +import System +from System.Drawing import Color + +def add_text_stamp_to_pdf(): + # Instantiate a Stamp object + logo_stamp = pdf.facades.Stamp() + + # Create a FormattedText object (first line) + formatted_text = pdf.facades.FormattedText( + "Hello World!", + Color.FromArgb(180, 0, 0), # Semi-transparent red + pdf.facades.FontStyle.TimesItalic, # Font style + pdf.facades.EncodingType.Winansi, # Encoding + False, # Embedded font + 50 # Font size + ) + + # Add another line to the stamp + formatted_text.add_new_line_text("Good Luck") + + # Bind formatted text to the stamp + logo_stamp.bind_logo(formatted_text) + + return logo_stamp diff --git a/examples/facades_stamp/rotating-stamp-about-the-center-point_1.py b/examples/facades_stamp/rotating-stamp-about-the-center-point_1.py new file mode 100644 index 0000000..0f3d91a --- /dev/null +++ b/examples/facades_stamp/rotating-stamp-about-the-center-point_1.py @@ -0,0 +1,53 @@ +# Extracted from: _index.md +# Source folder: E:\Github\Aspose.PDF-Documentation\en\python-net\working-with-facades\stamp\rotating-stamp-about-the-center-point +# Code fence language: python + + +import aspose.pdf as pdf + +def add_rotating_stamp_to_pdf(): + # Path to the documents directory + data_dir = RunExamples.get_data_dir_aspose_pdf_facades_technical_articles() + + # PdfFileInfo is used to get page width and height + file_info = pdf.facades.PdfFileInfo(data_dir + "RotatingStamp.pdf") + + try: + # Create Stamp object + stamp = pdf.facades.Stamp() + + # Bind image to stamp + stamp.bind_image(data_dir + "RotatingStamp.jpg") + + # Specify whether the stamp is background + stamp.is_background = False + + # Specify pages where the stamp will be applied + stamp.pages = [1] + + # Rotate stamp around its center (0–360 degrees) + stamp.rotation = 90 + + # Set the origin (lower-left corner of the stamp) + stamp.set_origin( + file_info.get_page_width(1) / 2, + file_info.get_page_height(1) / 2 + ) + + # Set image size + stamp.set_image_size(100, 100) + + # Open PDF document + document = pdf.Document(data_dir + "RotatingStamp_out.pdf") + + try: + # Create PdfFileStamp to apply the stamp + stamper = pdf.facades.PdfFileStamp(document) + + try: + # Add stamp to the PDF + stamper.add_stamp(stamp) + finally: + stamper.close() + document.close() + file_info.close() diff --git a/examples/working_with_documents/example_formatting_pdf_document.py b/examples/working_with_documents/example_formatting_pdf_document.py index 83482d5..d852e2e 100644 --- a/examples/working_with_documents/example_formatting_pdf_document.py +++ b/examples/working_with_documents/example_formatting_pdf_document.py @@ -129,7 +129,7 @@ def run_all_examples(data_dir=None, license_path=None): ("Configure document window settings", set_document_window), ("Embed fonts in existing PDF", embedded_fonts), ("Embed fonts in new PDF", embedded_fonts_in_new_document), - ("Set default font", set_default_font), + ("Set default font", set_default_font), ("List document fonts", get_all_fonts), ("Improve font embedding", improve_fonts_embedding), ("Set initial zoom factor", set_zoom_factor), diff --git a/sample_data/facades_form/input/fill_barcode_fields_in.pdf b/sample_data/facades_form/input/fill_barcode_fields_in.pdf new file mode 100644 index 0000000..e66a5f1 Binary files /dev/null and b/sample_data/facades_form/input/fill_barcode_fields_in.pdf differ diff --git a/sample_data/facades_form/input/fill_check_box_fields_in.pdf b/sample_data/facades_form/input/fill_check_box_fields_in.pdf new file mode 100644 index 0000000..f25d71d Binary files /dev/null and b/sample_data/facades_form/input/fill_check_box_fields_in.pdf differ diff --git a/sample_data/facades_form/input/fill_fields_by_name_and_value_in.pdf b/sample_data/facades_form/input/fill_fields_by_name_and_value_in.pdf new file mode 100644 index 0000000..9667b00 Binary files /dev/null and b/sample_data/facades_form/input/fill_fields_by_name_and_value_in.pdf differ diff --git a/sample_data/facades_form/input/fill_list_box_fields_in.pdf b/sample_data/facades_form/input/fill_list_box_fields_in.pdf new file mode 100644 index 0000000..81515e8 Binary files /dev/null and b/sample_data/facades_form/input/fill_list_box_fields_in.pdf differ diff --git a/sample_data/facades_form/input/fill_radio_button_fields_in.pdf b/sample_data/facades_form/input/fill_radio_button_fields_in.pdf new file mode 100644 index 0000000..01a8da3 Binary files /dev/null and b/sample_data/facades_form/input/fill_radio_button_fields_in.pdf differ diff --git a/sample_data/facades_form/input/fill_text_fields_in.pdf b/sample_data/facades_form/input/fill_text_fields_in.pdf new file mode 100644 index 0000000..b615bad Binary files /dev/null and b/sample_data/facades_form/input/fill_text_fields_in.pdf differ diff --git a/sample_data/facades_form/input/get_field_facades_in.pdf b/sample_data/facades_form/input/get_field_facades_in.pdf new file mode 100644 index 0000000..1cd33d9 Binary files /dev/null and b/sample_data/facades_form/input/get_field_facades_in.pdf differ diff --git a/sample_data/facades_form/input/get_field_values_in.pdf b/sample_data/facades_form/input/get_field_values_in.pdf new file mode 100644 index 0000000..1cd33d9 Binary files /dev/null and b/sample_data/facades_form/input/get_field_values_in.pdf differ diff --git a/sample_data/facades_form/input/get_radio_button_options_in.pdf b/sample_data/facades_form/input/get_radio_button_options_in.pdf new file mode 100644 index 0000000..0dfb4fb Binary files /dev/null and b/sample_data/facades_form/input/get_radio_button_options_in.pdf differ diff --git a/sample_data/facades_form/input/get_required_field_names_in.pdf b/sample_data/facades_form/input/get_required_field_names_in.pdf new file mode 100644 index 0000000..75d7fe9 Binary files /dev/null and b/sample_data/facades_form/input/get_required_field_names_in.pdf differ diff --git a/sample_data/facades_form/input/get_rich_text_values_in.pdf b/sample_data/facades_form/input/get_rich_text_values_in.pdf new file mode 100644 index 0000000..e673b1f Binary files /dev/null and b/sample_data/facades_form/input/get_rich_text_values_in.pdf differ diff --git a/sample_data/facades_form/input/get_rich_text_values_in_data.xml b/sample_data/facades_form/input/get_rich_text_values_in_data.xml new file mode 100644 index 0000000..00f5ed9 --- /dev/null +++ b/sample_data/facades_form/input/get_rich_text_values_in_data.xml @@ -0,0 +1,12 @@ + +YellowtownAlexanderGreenfieldAspose.PDF for Python via .NET, PDF Processing API that allows the developers to work with PDF documents without needing Microsoft Office® or Adobe Acrobat Automation. \ No newline at end of file diff --git a/sample_data/facades_form/input/resolve_full_field_names_in.pdf b/sample_data/facades_form/input/resolve_full_field_names_in.pdf new file mode 100644 index 0000000..e673b1f Binary files /dev/null and b/sample_data/facades_form/input/resolve_full_field_names_in.pdf differ diff --git a/sample_data/facades_form/input/sample_form.fdf b/sample_data/facades_form/input/sample_form.fdf new file mode 100644 index 0000000..ea61cbc --- /dev/null +++ b/sample_data/facades_form/input/sample_form.fdf @@ -0,0 +1,7 @@ +%FDF-1.2 % 1 0 obj +<><><><><>]/Annots[]>>>> +endobj + +trailer +<> +%%EOF \ No newline at end of file diff --git a/sample_data/facades_form/input/sample_form.json b/sample_data/facades_form/input/sample_form.json new file mode 100644 index 0000000..9ce8ee2 --- /dev/null +++ b/sample_data/facades_form/input/sample_form.json @@ -0,0 +1,26 @@ +[ + { + "Name": "First Name", + "PageIndex": 1, + "Flags": 4, + "Value": "Thomas" + }, + { + "Name": "Last Name", + "PageIndex": 1, + "Flags": 4, + "Value": "Jacksonn" + }, + { + "Name": "City", + "PageIndex": 1, + "Flags": 4, + "Value": "Yellowville" + }, + { + "Name": "Country", + "PageIndex": 1, + "Flags": 4, + "Value": "Kenya" + } +] \ No newline at end of file diff --git a/sample_data/facades_form/input/sample_form.pdf b/sample_data/facades_form/input/sample_form.pdf new file mode 100644 index 0000000..1cd33d9 Binary files /dev/null and b/sample_data/facades_form/input/sample_form.pdf differ diff --git a/sample_data/facades_form/input/sample_form.xfdf b/sample_data/facades_form/input/sample_form.xfdf new file mode 100644 index 0000000..1fde6c6 --- /dev/null +++ b/sample_data/facades_form/input/sample_form.xfdf @@ -0,0 +1,17 @@ + + + + + Alexander + + + Greenfield + + + Yellowtown + + + Redland + + + \ No newline at end of file diff --git a/sample_data/facades_form/input/sample_form.xml b/sample_data/facades_form/input/sample_form.xml new file mode 100644 index 0000000..4292929 --- /dev/null +++ b/sample_data/facades_form/input/sample_form.xml @@ -0,0 +1,15 @@ + + + + Thomas + + + Andersson + + + Seattle + + + Brownland + + \ No newline at end of file diff --git a/sample_data/facades_form/input/sample_form_image.jpg b/sample_data/facades_form/input/sample_form_image.jpg new file mode 100644 index 0000000..6f8d5bd Binary files /dev/null and b/sample_data/facades_form/input/sample_form_image.jpg differ diff --git a/sample_data/facades_form/input/sample_form_image.pdf b/sample_data/facades_form/input/sample_form_image.pdf new file mode 100644 index 0000000..3f54c2b Binary files /dev/null and b/sample_data/facades_form/input/sample_form_image.pdf differ diff --git a/sample_data/facades_form/input/sample_form_new.pdf b/sample_data/facades_form/input/sample_form_new.pdf new file mode 100644 index 0000000..5d7ae0b Binary files /dev/null and b/sample_data/facades_form/input/sample_form_new.pdf differ diff --git a/sample_data/facades_form/input/sample_form_xfa.xml b/sample_data/facades_form/input/sample_form_xfa.xml new file mode 100644 index 0000000..b8d1c80 --- /dev/null +++ b/sample_data/facades_form/input/sample_form_xfa.xml @@ -0,0 +1,60 @@ + + + +
+ 123 + + Aspose + + + + + + + + + + + + + + + + + +
+ + + + + + + + 0 + + 0.00000000 + 0 + + 0.00000000 + 0 + + 0.00000000 + + +