將文件轉換為 PDF
能夠輕鬆且可靠地將文件從一種格式轉換成另一種格式是 Aspose.Words 的關鍵特點。 最受歡迎的轉檔格式之一是 PDF - 固定格式,在各種平台上呈現文書時會保持其原始外观。 在 Aspose.Words 中," rendering " 這個詞用來描述將一份文件轉換成具有分頁或以分頁為概念的檔案格式的過程。
將 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 非常簡單,只需兩行程式碼即可完成:
- 使用其某個建構函式,以指定文件名稱及其格式延展子來載入您的文件至 Document 物件。
- 在 Document 物件上調用 Document.Save 方法之一,並指定以 PDF 為所需的輸出格式,輸入具有 .PDF 拡張子的檔案名稱。
以下範例程式碼示範了如何透過 Save 方法將檔案從 DOCX 轉換為 PDF:
// 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"); |
你可以從 Aspose.Words GitHub 下載本範例的模板檔案。
有時需要指定額外的選項,以影響儲存文書為 PDF 的結果。 這些選項可透過使用包含確定如何顯示PDF輸出之屬性的PdfSaveOptions類別來指定。
請注意,使用相同的技術,您可以將任何流式排版格式的文件轉換為 PDF 格式。
轉換至不同的 PDF 標準
Aspose.Words提供PdfCompliace 枚數以支援將DOC或DOCX轉換成各種PDF格式標準(例如:PDF 1.7、PDF 1.5等)。
接下來的程式碼範例示範了如何透過 PdfSaveOptions 將文件轉換成符合 PDF17 的 PDF 1.7:
// 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"); |
// 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,然後多餘的嵌套畫布和空白畫布會從中移除,相邻具有相同格式化之字元會進行串聯。
接下來的程式碼範例示範如何優化輸出:
// 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); |
見也
“- 文章 Rendering 提供更多關於固定頁面和流式排版格式的資訊”
“-該文章 Converting to Fixed-page Format 提供關於頁面布局的更多資訊”
“- 該文章 Specify Rendering Options When Converting to PDF 提供有關使用 PdfSaveOptions
的更多資訊。”
-該文章Learn Features of Conversion to PDF/A and PDF/UA描述哪個PDF標準以及與PDF標準相關的ISO以支援Aspose.Words
“-該文章 Which PDF Standard Is Better to Choose 來決定哪些 PDF 標準對哪些案例有意義。”
- 文章 Working with PDF/A or PDF/UA 描述 PDF/A 和 PDF/UA 格式的文件內容要求 - 主要為結構和字體的要求"。
-文章 Accessibility Issue Warnings When Saving to PDF/A and PDF/UA 描述了 PDF/A 和 PDF/UA 的內容存取要求