Converti un document în PDF

Posibilitatea de a converti cu ușurință și fiabil documente dintr-un format în altul reprezintă o caracteristică cheie a lui Aspose.Words. Unul dintre cele mai populare formate pentru conversie este PDF – un format cu aranjare fixă care păstrează aspectul inițial al unui document în timpul redării pe diferite platforme. Termenul “redare” este folosit în Aspose.Words pentru a descrie procesul de convertire a unui document într-un format de fișier care este paginat sau are conceptul de pagini.

Convertește un document Word în PDF

Conversia de la Word la PDF este un proces destul de complex care necesită mai multe etape de calcul. Aspose.Words motorul de aranjare imită modul în care funcționează motorul de aranjare al Microsoft Word, făcând ca documentele PDF să arate cât mai aproape posibil de cele pe care le puteți vedea în Microsoft Word.

Cu Aspose.Words puteți converti programatic un document din formate de Word, cum ar fi DOC sau DOCX, în PDF fără a folosi Microsoft Office. Acest articol explică cum să efectuezi această conversie.

Converteste DOCX sau DOC în PDF

Conversia de la formatul documentului DOC sau DOCX în formatul PDF în Aspose.Words este foarte ușoară și poate fi realizată cu doar două linii de cod care:

  1. Încărcaţi documentul într-un obiect Document folosind unul dintre constructorii săi specificând numele documentului cu extensia de format.
  2. Invocaţi una din Document.Save metodele pe obiectul Document şi specificaţi formatul de ieşire dorit ca fiind PDF, introducând numele unui fişier cu extensia “.PDF”.

Exemplul de cod următor arată cum să convertesti un document din DOCX în PDF folosind metoda 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

Puteți descărca fișierul șablon al acestui exemplu de la Aspose.Words GitHub.

Converteste la standarde diferite de PDF

Aspose.Words oferă enumerarea PdfCompliace pentru a sprijini conversia de la DOC sau DOCX în diferite standarde PDF (cum ar fi PDF 1.7, PDF 1.5, etc.).

Exemplul următor de cod demonstrează cum să converţi un document la format PDF 1.7 utilizând PdfSaveOptions cu conformitate la 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);

Convertați imaginile în PDF

Conversia în format PDF nu este restricționată de Microsoft Word formate de documente. Orice format acceptat de Aspose.Words, inclusiv cel creat programatic, poate fi de asemenea transformat în PDF. De exemplu, putem converti imagini unipagina precum JPEG, PNG, BMP, EMF sau WMF, precum și imagini multipagina precum TIFF și GIF în format PDF.

Exemplul următor de cod arată cum se pot converti imaginile JPEG și TIFF în 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);
}

Pentru ca acest cod să funcționeze, trebuie să adăugați referințe la Aspose.Words și System.Drawing în proiectul dumneavoastră.

Reducere dimensiune PDF

Când salvezi ca PDF, poți specifica dacă vrei să optimizezi ieșirea. Pentru aceasta trebuie să setezi OptimizeOutput la true, și apoi redundanți canvas-uri încorporate și canvas-uri goale vor fi eliminate, caracterele vecine cu același formatare vor fi concatenate.

Exemplul următor de cod arată cum se optimizează ieşirea:

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

Vezi și