แปลงเอกสารเป็น 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 บทความนี้จะอธิบายวิธีการแปลงนี้

แปลง DOCX หรือ DOC เป็น 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-.NET.git.
Document doc = new Document(MyDir + "Document.docx");
doc.Save(ArtifactsDir + "BaseConversions.DocxToPdf.pdf");
view raw docx-to-pdf.cs hosted with ❤ by GitHub

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

แปลงเป็นมาตรฐาน PDF ที่แตกต่างกัน

Aspose.Words จัดเตรียม PdfCompliaceenumeration เพื่อรองรับการแปลง 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-.NET.git.
Document doc = new Document(MyDir + "Rendering.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions { Compliance = PdfCompliance.Pdf17 };
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions);

แปลงรูปภาพเป็น 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-.NET.git.
ConvertImageToPdf(ImagesDir + "Logo.jpg", ArtifactsDir + "BaseConversions.JpgToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Transparent background logo.png", ArtifactsDir + "BaseConversions.PngToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Windows MetaFile.wmf", ArtifactsDir + "BaseConversions.WmfToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Tagged Image File Format.tiff", ArtifactsDir + "BaseConversions.TiffToPdf.pdf");
ConvertImageToPdf(ImagesDir + "Graphics Interchange Format.gif", ArtifactsDir + "BaseConversions.GifToPdf.pdf");
view raw image-to-pdf.cs hosted with ❤ by GitHub
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.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>
public void ConvertImageToPdf(string inputFileName, string outputFileName)
{
Console.WriteLine("Converting " + inputFileName + " to PDF ....");
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(inputFileName))
{
// 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".
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
int framesCount = image.GetFrameCount(dimension);
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
image.SelectActiveFrame(dimension, frameIdx);
// 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.
PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.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.PageWidth,
ps.PageHeight,
WrapType.None);
}
}
doc.Save(outputFileName);
}

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

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

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

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการปรับผลลัพธ์ให้เหมาะสม:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Rendering.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions { OptimizeOutput = true };
doc.Save(ArtifactsDir + "WorkingWithPdfSaveOptions.OptimizeOutput.pdf", saveOptions);

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