Konwertuj dokument na format PDF

Możliwość łatwej i niezawodnej konwersji dokumentów z jednego formatu na inny jest kluczową cechą Aspose.Words. Jednym z najpopularniejszych formatów do konwersji jest PDF – format o stałym układzie, który zachowuje oryginalny wygląd dokumentu podczas jego renderowania na różnych platformach. Termin “renderowanie” jest używany w Aspose.Words do opisania procesu konwertowania dokumentu do formatu pliku podzielonego na strony lub mającego koncepcję stron.

Konwertuj dokument Word na format PDF

Konwersja z programu Word do formatu PDF jest dość złożonym procesem, który wymaga kilku etapów obliczeń. Silnik układu Aspose.Words naśladuje sposób działania silnika układu strony Microsoft Word, dzięki czemu dokumenty wyjściowe PDF wyglądają jak najbliżej tego, co można zobaczyć w Microsoft Word.

Dzięki Aspose.Words możesz programowo konwertować dokument z formatów Word, takich jak DOC lub DOCX, do formatu PDF bez korzystania z pakietu Microsoft Office. W tym artykule wyjaśniono, jak przeprowadzić tę konwersję.

Konwertuj DOCX lub DOC na PDF

Konwersja z formatu dokumentu DOC lub DOCX na format PDF w formacie Aspose.Words jest bardzo łatwa i można ją wykonać za pomocą zaledwie dwóch linii kodu, które:

  1. Załaduj dokument do obiektu Document, korzystając z jednego z jego konstruktorów, podając nazwę dokumentu wraz z rozszerzeniem formatu.
  2. Wywołaj jedną z metod Document.Save na obiekcie Document i określ żądany format wyjściowy jako PDF, wpisując nazwę pliku z rozszerzeniem “.PDF”.

Poniższy przykład kodu pokazuje, jak przekonwertować dokument z formatu DOCX na format PDF przy użyciu metody 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

Możesz pobrać plik szablonu tego przykładu z Aspose.Words GitHub.

Konwertuj na różne standardy PDF

Aspose.Words udostępnia Zgodność PDFenumerację do obsługi konwersji DOC lub DOCX na różne standardy formatu PDF (takie jak PDF 1.7, PDF 1.5 itp.).

Poniższy przykład kodu demonstruje, jak przekonwertować dokument do formatu PDF 1.7 przy użyciu PdfSaveOptions zgodnie z 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);

Konwertuj obrazy do formatu PDF

Konwersja do formatu PDF nie jest ograniczona formatami dokumentów Microsoft Word. Dowolny format obsługiwany przez Aspose.Words, w tym utworzony programowo, można również przekonwertować na format PDF. Na przykład możemy konwertować obrazy jednostronicowe, takie jak JPEG, PNG, BMP, EMF lub WMF, a także obrazy wielostronicowe, takie jak TIFF i GIF, do formatu PDF.

Poniższy przykład kodu pokazuje, jak konwertować obrazy JPEG i TIFF do formatu 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);
}

Aby ten kod działał, musisz dodać do swojego projektu odniesienia do Aspose.Words i System.Drawing.

Zmniejsz rozmiar wyjściowy pliku PDF

Podczas zapisywania w formacie PDF możesz określić, czy chcesz zoptymalizować wydruk. Aby to zrobić, należy ustawić flagę OptimizeOutput na true, a następnie zbędne zagnieżdżone i puste płótna zostaną usunięte, a sąsiadujące pliki glyph o tym samym formatowaniu zostaną połączone.

Poniższy przykład kodu pokazuje, jak zoptymalizować dane wyjściowe:

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

Zobacz też