تبدیل یک سند به PDF

قابلیت تبدیل آسان و مطمئن اسناد از یک فرمت به فرمت دیگر یکی از ویژگی های کلیدی Aspose.Words است. یکی از محبوب‌ترین فرمت‌ها برای تبدیل، پی‌دی‌اف است – قالبی با طرح‌بندی ثابت، که ظاهر اصلی یک سند را در حین رندر کردن آن در پلتفرم‌های مختلف حفظ می‌کند. اصطلاح “رندر” در Aspose.Words برای توصیف فرآیند تبدیل یک سند به فرمت فایلی که صفحه بندی شده یا دارای مفهوم صفحات است استفاده می شود.

تبدیل یک سند Word به PDF

تبدیل از Word به PDF یک فرآیند نسبتاً پیچیده است که نیاز به چندین مرحله محاسبه دارد. موتور طرح‌بندی Aspose.Words روشی را تقلید می‌کند که موتور صفحه‌آرایی Microsoft Word کار می‌کند، و باعث می‌شود اسناد خروجی PDF تا حد امکان به آنچه در Microsoft Word می‌بینید نزدیک‌تر به نظر برسند.

با Aspose.Words می توانید بدون استفاده از Microsoft Office یک سند را از فرمت های Word مانند DOC یا DOCX به PDF تبدیل کنید. این مقاله نحوه انجام این تبدیل را توضیح می دهد.

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);

همچنین ببینید