---
title: "แปลงเอกสารเป็นPDFในC++"
---


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

## แปลงเอกสารคำเป็นPDF{#convert-a-word-document-to-pdf}

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

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

```

โปรดทราบว่าจำนวนหน้าในเอกสารมีผลต่อเวลาการแปลง.

```

### กำลังแปลงDOCXหรือDOCเป็นPDF{#converting-doc-or-docx-to-pdf}

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

1. ป้อนเอกสารของคุณลงในวัตถุ[Document](https://reference.aspose.com/words/cpp/aspose.words/document/)โดยใช้หนึ่งในตัวสร้างโดยการระบุชื่อเอกสารที่มีนามสกุล.
1. เรียกหนึ่งในวิธีการ[Document.Save](https://reference.aspose.com/words/cpp/aspose.words/document/save/)บนวัตถุ**Document**และระบุรูปแบบผลลัพธ์ที่ต้องการเป็นPDFโดยการป้อนชื่อไฟล์ด้วย"PDF"ส่วนขยาย.

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

```cpp
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Document.docx");
doc->Save(ArtifactsDir + u"BaseConversions.DocxToPdf.pdf");
```

คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จาก [Aspose.Words GitHub](https://github.com/aspose-words/Aspose.Words-for-C/tree/master/Examples).

```

บางครั้งจำเป็นต้องระบุตัวเลือกเพิ่มเติมซึ่งอาจส่งผลต่อผลลัพธ์ของการบันทึกเอกสารเป็นPDF อ็อพชันนี้สามารถระบุได้โดยใช้คลาสของ[PdfSaveOptions](https://reference.aspose.com/words/cpp/aspose.words.saving/pdfsaveoptions/)ซึ่งประกอบด้วยคุณสมบัติที่กำหนดวิธีแสดงผลลัพธ์PDF.

โปรดทราบว่าด้วยเทคนิคเดียวกันคุณสามารถแปลงเอกสารรูปแบบโฟลวเค้าโครงเป็นPDFรูปแ.

```

### แปลงเป็นมาตรฐานPDFที่แตกต่างกัน{#converting-to-various-pdf-standards}

Aspose.Wordsให้การแจงนับ[PdfCompliance](https://reference.aspose.com/words/cpp/aspose.words.saving/pdfsaveoptions/get_compliance/)เพื่อสนับสนุนการแปลงDOCหรือDOCXเป็นมาตรฐานรูปแบบต่างๆPDF(เช่นPDF 1.7, PDF 1.5, ฯลฯ).

ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการแปลงเอกสารเป็นPDF1.7โดยใช้[PdfSaveOptions](https://reference.aspose.com/words/cpp/aspose.words.saving/pdfsaveoptions/)โดยปฏิบัติตามPDF17:

```cpp
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");

auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->set_Compliance(PdfCompliance::Pdf17);

doc->Save(ArtifactsDir + u"WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions);
```

## แปลงภาพเป็นPDF{#convert-an-image-to-pdf}

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

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

```cpp
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
ConvertImageToPdf(ImagesDir + u"Logo.jpg", ArtifactsDir + u"BaseConversions.JpgToPdf.pdf");
ConvertImageToPdf(ImagesDir + u"Transparent background logo.png", ArtifactsDir + u"BaseConversions.PngToPdf.pdf");
ConvertImageToPdf(ImagesDir + u"Windows MetaFile.wmf", ArtifactsDir + u"BaseConversions.WmfToPdf.pdf");
ConvertImageToPdf(ImagesDir + u"Tagged Image File Format.tiff", ArtifactsDir + u"BaseConversions.TiffToPdf.pdf");
ConvertImageToPdf(ImagesDir + u"Graphics Interchange Format.gif", ArtifactsDir + u"BaseConversions.GifToPdf.pdf");
```

```cpp
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
/// <summary>
/// Converts an image to PDF using Aspose.Words for .NET.
/// </summary>
/// <param name="inputFileName">File name of input image file.</param>
/// <param name="outputFileName">Output PDF file name.</param>
void ConvertImageToPdf(String inputFileName, String outputFileName)
{
    std::cout << (String(u"Converting ") + inputFileName + u" to PDF ....") << std::endl;
            
    auto doc = MakeObject<Document>();
    auto builder = MakeObject<DocumentBuilder>(doc);

    // Read the image from file, ensure it is disposed.
    {
        SharedPtr<System::Drawing::Image> image = System::Drawing::Image::FromFile(inputFileName);

			// Insert a section break before each new page, in case of a multi-frame TIFF.
			builder->InsertBreak(BreakType::SectionBreakNewPage);

			// 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.
			SharedPtr<PageSetup> ps = builder->get_PageSetup();
			ps->set_PageWidth(ConvertUtil::PixelToPoint(image->get_Width(), image->get_HorizontalResolution()));
			ps->set_PageHeight(ConvertUtil::PixelToPoint(image->get_Height(), image->get_VerticalResolution()));

			// Insert the image into the document and position it at the top left corner of the page.
			builder->InsertImage(image, RelativeHorizontalPosition::Page, 0, RelativeVerticalPosition::Page, 0, ps->get_PageWidth(), ps->get_PageHeight(), WrapType::None);
    }

    doc->Save(outputFileName);        
}
```

เมื่อต้องการทำให้โค้ดนี้ทำงานคุณต้องเพิ่มการอ้างอิงไปยังAspose.Wordsและ`System.Drawing`กับโครงการของคุณ.

## ดูเพิ่มเติม

- บทความ [การแสดงผล](/words/th/cpp/rendering/) สำหรับข้อมูลเพิ่มเติมเกี่ยวกับหน้าคงที่และรูปแบบเค้าโครงโฟลว์
- บทความ [แปลงเป็นรูปแบบหน้าถาวร](/words/cpp/converting-to-fixed-page-format/#what-is-a-page-layout) สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเค้าโครงหน้า
- บทความ [ระบุตัวเลือกการแสดงผลเมื่อแปลงเป็นPDF](/words/th/cpp/specify-rendering-options-when-converting-to-pdf/) สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้`PdfSaveOptions`คลาส
