تغيير نوع الخط على أحرف يونيكود محددة عند الحفظ إلى PDF باستخدام Python.NET

مقارنة أمثلة

تعرض لقطات الشاشة أدناه المخرجات مع إعدادات مختلفة. يُظهر ملف PDF الأول استبدال الخط الكامل للنص، بينما يغير الملف الثاني خط الحرف المحدد فقط.

الاستبدال الكامل للنص الاستبدال على مستوى الحرف
تغيير الخط الكامل تغيير الخط الانتقائي

خطوات التنفيذ

لتمكين استبدال الخط على مستوى الحرف:

  1. إنشاء كائن Workbook
  2. الوصول إلى خلايا ورقة العمل باستخدام خاصية Worksheet.cells
  3. تعيين قيم الخلايا التي تحتوي على رموز يونيكود خاصة
  4. تكوين PdfSaveOptions مع:
    • is_font_substitution_char_granularity = True
  5. حفظ ملف العمل بامتداد PDF
import os
from aspose.cells import Workbook, PdfSaveOptions

# For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")

if not os.path.exists(data_dir):
    os.makedirs(data_dir)

# Create workbook object
workbook = Workbook()

# Access the first worksheet
worksheet = workbook.worksheets[0]

# Access cells
cell1 = worksheet.cells.get("A1")
cell2 = worksheet.cells.get("B1")

# Set the styles of both cells to Times New Roman
style = cell1.get_style()
style.font.name = "Times New Roman"
cell1.set_style(style)
cell2.set_style(style)

# Put the values inside the cell
cell1.put_value("Hello without Non-Breaking Hyphen")
cell2.put_value("Hello" + chr(8209) + " with Non-Breaking Hyphen")

# Autofit the columns
worksheet.auto_fit_columns()

# Save to Pdf without setting PdfSaveOptions.is_font_substitution_char_granularity
workbook.save(os.path.join(data_dir, "SampleOutput_out.pdf"))

# Save to Pdf after setting PdfSaveOptions.is_font_substitution_char_granularity to true
opts = PdfSaveOptions()
opts.is_font_substitution_char_granularity = True
workbook.save(os.path.join(data_dir, "SampleOutput2_out.pdf"), opts)

مفتاح التكوين

استخدم مكونات واجهة برمجة التطبيقات الأساسية التالية:

  • فئة PdfSaveOptions لإعدادات عرض PDF
  • خاصية is_font_substitution_char_granularity لاستبدال الخطوط على مستوى الأحرف
  • طريقة Workbook.save لإنشاء المخرجات