แปลงเอกสารเป็น PDF

ความสามารถในการแปลงเอกสารจากรูปแบบหนึ่งไปเป็นอีกรูปแบบหนึ่งได้อย่างง่ายดายและเชื่อถือได้คือคุณสมบัติหลักของ Aspose.Words รูปแบบการแปลงที่ได้รับความนิยมมากที่สุดรูปแบบหนึ่งคือ PDF ซึ่งเป็นรูปแบบเค้าโครงคงที่ ซึ่งคงรูปลักษณ์ดั้งเดิมของเอกสารในระหว่างการเรนเดอร์บนแพลตฟอร์มต่างๆ คำว่า “การแสดงผล” ใช้ใน Aspose.Words เพื่ออธิบายกระบวนการแปลงเอกสารเป็นรูปแบบไฟล์ที่มีการแบ่งหน้าหรือมีแนวคิดเรื่องหน้า

แปลงเอกสาร Word เป็น PDF

การแปลงจาก Word เป็น PDF เป็นกระบวนการที่ค่อนข้างซับซ้อนซึ่งต้องใช้การคำนวณหลายขั้นตอน โปรแกรมเค้าโครง Aspose.Words เลียนแบบวิธีการทำงานของกลไกเค้าโครงหน้าของ Microsoft Word ทำให้เอกสารเอาต์พุต PDF ดูใกล้เคียงกับสิ่งที่คุณเห็นใน Microsoft Word มากที่สุด

ด้วย Aspose.Words คุณสามารถแปลงเอกสารจากรูปแบบ Word เช่น DOC หรือ DOCX เป็น PDF โดยทางโปรแกรมโดยไม่ต้องใช้ Microsoft Office บทความนี้จะอธิบายวิธีการแปลงนี้

แปลง DOC หรือ DOCX เป็น PDF

การแปลงจากรูปแบบเอกสาร DOC หรือ DOCX เป็นรูปแบบ PDF ใน Aspose.Words นั้นง่ายมาก และสามารถทำได้โดยใช้โค้ดเพียงสองบรรทัดที่:

  1. โหลดเอกสารของคุณลงในออบเจ็กต์ Document โดยใช้ตัวสร้างตัวใดตัวหนึ่งโดยระบุชื่อเอกสารพร้อมนามสกุลของรูปแบบ
  2. เรียกใช้หนึ่งในวิธี Document.save บนออบเจ็กต์ Document และระบุรูปแบบเอาต์พุตที่ต้องการเป็น PDF โดยการป้อนชื่อไฟล์ที่มีนามสกุล “.PDF”

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแปลงเอกสารจาก DOCX เป็น PDF โดยใช้วิธี save:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Document.docx")
doc.save(ARTIFACTS_DIR + "BaseConversions.docx_to_pdf.pdf")
view raw docx-to-pdf.py hosted with ❤ by GitHub

คุณสามารถดาวน์โหลดไฟล์เทมเพลตของตัวอย่างนี้ได้จาก Aspose.Words GitHub

แปลงเป็น PDF มาตรฐาน {#convert-to-various-pdf-standards} ต่างๆ

Aspose.Words จัดเตรียมการแจงนับ PdfCompliace เพื่อรองรับการแปลง DOC หรือ DOCX ไปเป็นมาตรฐานรูปแบบ PDF ต่างๆ (เช่น PDF 1.7, PDF 1.5 เป็นต้น)

ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีการแปลงเอกสารเป็น PDF 1.7 โดยใช้ PdfSaveOptions โดยสอดคล้องกับ PDF17:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Rendering.docx")
save_options = aw.saving.PdfSaveOptions()
save_options.compliance = aw.saving.PdfCompliance.PDF17
doc.save(ARTIFACTS_DIR + "WorkingWithPdfSaveOptions.conversion_to_pdf_17.pdf", save_options)

แปลงรูปภาพเป็น PDF

การแปลงเป็น PDF ไม่ได้ถูกจำกัดด้วยรูปแบบเอกสาร Microsoft Word รูปแบบใดๆ ที่สนับสนุนโดย Aspose.Words รวมถึงที่สร้างขึ้นโดยทางโปรแกรมก็สามารถแปลงเป็น PDF ได้เช่นกัน ตัวอย่างเช่น เราสามารถแปลงรูปภาพหน้าเดียว เช่น JPEG, PNG, BMP, EMF หรือ WMF รวมถึงรูปภาพหลายหน้า เช่น TIFF และ GIF เป็น PDF

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแปลงรูปภาพ JPEG และ TIFF เป็น PDF:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
self.convert_image_to_pdf(IMAGES_DIR + "Logo.jpg",
ARTIFACTS_DIR + "BaseConversions.JpgToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Transparent background logo.png",
ARTIFACTS_DIR + "BaseConversions.PngToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Windows MetaFile.wmf",
ARTIFACTS_DIR + "BaseConversions.WmfToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Tagged Image File Format.tiff",
ARTIFACTS_DIR + "BaseConversions.TiffToPdf.pdf")
self.convert_image_to_pdf(IMAGES_DIR + "Graphics Interchange Format.gif",
ARTIFACTS_DIR + "BaseConversions.GifToPdf.pdf")
view raw image-to-pdf.py hosted with ❤ by GitHub
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
@staticmethod
def convert_image_to_pdf(input_file_name: str, output_file_name: str):
"""Converts an image to PDF using Aspose.Words for .NET.
:param input_file_name: File name of input image file.
:param output_file_name: Output PDF file name.
"""
print("Converting " + input_file_name + " to PDF...")
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
# Read the image from file
with drawing.Image.from_file(input_file_name) as image:
# Find which dimension the frames in this image represent. For example
# the frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
dimension = drawing.imaging.FrameDimension(image.frame_dimensions_list[0])
frames_count = image.get_frame_count(dimension)
for frame_idx in range(frames_count):
# Insert a section break before each new page, in case of a multi-frame TIFF.
if frame_idx != 0:
builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE)
image.select_active_frame(dimension, frame_idx)
frame_stream = io.BytesIO()
image.save(frame_stream, drawing.imaging.ImageFormat.png)
# We want the size of the page to be the same as the size of the image.
# Convert pixels to points to size the page to the actual image size.
page_setup = builder.page_setup
page_setup.page_width = aw.ConvertUtil.pixel_to_point(image.width, image.horizontal_resolution)
page_setup.page_height = aw.ConvertUtil.pixel_to_point(image.height, image.vertical_resolution)
# Insert the image into the document and position it at the top left corner of the page.
builder.insert_image(
frame_stream,
aw.drawing.RelativeHorizontalPosition.PAGE,
0,
aw.drawing.RelativeVerticalPosition.PAGE,
0,
page_setup.page_width,
page_setup.page_height,
aw.drawing.WrapType.NONE)
doc.save(output_file_name)

เพื่อให้โค้ดนี้ใช้งานได้ คุณต้องเพิ่มการอ้างอิงถึง Aspose.Words และ aspose.pydrawing ให้กับโปรเจ็กต์ของคุณ

ลดขนาดเอาต์พุต PDF

เมื่อบันทึกเป็น PDF คุณสามารถระบุว่าคุณต้องการปรับผลลัพธ์ให้เหมาะสมหรือไม่ ในการดำเนินการนี้ คุณต้องตั้งค่าสถานะ optimize_output เป็น true จากนั้นระบบจะลบแคนวาสที่ซ้อนกันและแคนวาสเปล่าที่ซ้ำซ้อนออก ส่วน glyph เพื่อนบ้านที่มีการจัดรูปแบบเดียวกันจะถูกต่อเข้าด้วยกัน

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git.
doc = aw.Document(MY_DIR + "Rendering.docx")
save_options = aw.saving.PdfSaveOptions()
save_options.optimize_output = True
doc.save(ARTIFACTS_DIR + "PdfSaveOptions.OptimizeOutput.pdf", save_options)

ลดขนาดเอาต์พุต PDF

เมื่อบันทึกเป็น PDF คุณสามารถระบุว่าคุณต้องการปรับผลลัพธ์ให้เหมาะสมหรือไม่ ในการดำเนินการนี้ คุณต้องตั้งค่าสถานะ optimize_output เป็น true จากนั้นระบบจะลบแคนวาสที่ซ้อนกันและแคนวาสเปล่าที่ซ้ำซ้อนออก ส่วน glyph เพื่อนบ้านที่มีการจัดรูปแบบเดียวกันจะถูกต่อเข้าด้วยกัน

ดูสิ่งนี้ด้วย