Konwertuj dokument do PDF

Możliwość łatwej i niezawodnej konwersji dokumentów z jednego formatu na inny jest kluczową cechą Aspose.Words. Jednym z najbardziej popularnych formatów do konwersji jest PDF - utrwalony format układu, który zachowuje oryginalny wygląd dokumentu podczas renderowania na różnych platformach. Określenie “renderowanie” jest używane w Aspose.Words opis procesu konwersji dokumentu na format pliku, który jest paginowany lub ma pojęcie stron.

Konwertuj Word Document do PDF

Konwersja z Worda na PDF jest raczej skomplikowanym procesem wymagającym kilku etapów obliczeń. Aspose.Words układ silnika naśladuje sposób Microsoft Wordsilnik układu strony działa, dzięki czemu dokumenty wyjściowe PDF wyglądają jak najbliżej tego, co można zobaczyć w Microsoft Word.

Z Aspose.Words można programowo przekonwertować dokument z formatu DOC lub DOCX do PDF bez użycia Microsoft Biuro. Ten artykuł wyjaśnia jak przeprowadzić tę konwersję.

Konwersja DOCX lub DOC na PDF

Konwersja z formatu dokumentu DOC lub DOCX na format PDF w Aspose.Words jest bardzo łatwe i może być wykonane tylko z dwóch linii kodu, które:

  1. Wczytaj dokument do Document obiekt wykorzystujący jeden z jego konstruktorów poprzez podanie nazwy dokumentu wraz z rozszerzeniem jego formatu.
  2. Wywołaj jednego z Document.Save metody Document obiekt i określić pożądany format wyjścia jako PDF, wprowadzając nazwę pliku z rozszerzeniem. “PDF”.

Poniższy przykład kodu pokazuje, jak przekonwertować dokument z DOCX do PDF za pomocą Save Metoda:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Document.docx");
doc.save(getArtifactsDir() + "BaseConversions.DocxToPdf.pdf");

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

Konwertuj do różnych plików PDF Normy

Aspose.Words zapewnia PdfCompliacewyliczenie wspierające konwersję DOC lub DOCX na różne standardy formatu PDF (takie jak PDF 1.7, PDF 1.5 itp.).

Poniższy przykład kodu pokazuje, jak przekonwertować dokument do PDF 1.7 za pomocą PdfSaveOptions zgodnie z PDF17:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Rendering.docx");
PdfSaveOptions saveOptions = new PdfSaveOptions(); { saveOptions.setCompliance(PdfCompliance.PDF_17); }
doc.save(getArtifactsDir() + "WorkingWithPdfSaveOptions.ConversionToPdf17.pdf", saveOptions);

Konwertuj obrazy do PDF

Konwersja na PDF nie jest ograniczona przez Microsoft Word formaty dokumentów. Każdy format obsługiwany przez Aspose.Words, w tym programowo utworzone, mogą być również konwertowane do PDF. Na przykład, możemy przekonwertować obrazy jednostronicowe, takie jak JPEG, PNG, BMP, EMF lub WMF, jak również obrazy wielostronicowe, takie jak TIFF i GIF, do PDF.

Poniższy przykład kodu pokazuje jak przekonwertować obrazy JPEG i TIFF do PDF:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
convertImageToPDF(getImagesDir() + "Logo.jpg", getArtifactsDir() + "BaseConversions.JpgToPdf.pdf");
convertImageToPDF(getImagesDir() + "Transparent background logo.png", getArtifactsDir() + "BaseConversions.PngToPdf.pdf");
convertImageToPDF(getImagesDir() + "Windows MetaFile.wmf", getArtifactsDir() + "BaseConversions.WmfToPdf.pdf");
convertImageToPDF(getImagesDir() + "Tagged Image File Format.tiff", getArtifactsDir() + "BaseConversions.TiffToPdf.pdf");
convertImageToPDF(getImagesDir() + "Graphics Interchange Format.gif", getArtifactsDir() + "BaseConversions.GifToPdf.pdf");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
/**
* Converts an image to PDF using Aspose.Words for Java.
*
* @param inputFileName File name of input image file.
* @param outputFileName Output PDF file name.
* @throws Exception
*/
private void convertImageToPDF(String inputFileName, String outputFileName) throws Exception {
// Create Aspose.Words.Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Load images from the disk using the appropriate reader.
// The file formats that can be loaded depends on the image readers available on the machine.
ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName));
ImageReader reader = ImageIO.getImageReaders(iis).next();
reader.setInput(iis, false);
// Get the number of frames in the image.
int framesCount = reader.getNumImages(true);
// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) {
// Insert a section break before each new page, in case of a multi-frame image.
if (frameIdx != 0)
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
// Select active frame.
BufferedImage image = reader.read(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.getPageSetup();
ps.setPageWidth(ConvertUtil.pixelToPoint(image.getWidth()));
ps.setPageHeight(ConvertUtil.pixelToPoint(image.getHeight()));
// 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.getPageWidth(),
ps.getPageHeight(),
WrapType.NONE);
}
if (iis != null) {
iis.close();
reader.dispose();
}
doc.save(outputFileName);
}

Aby ten kod działał, musisz dodać odniesienia do Aspose.Words, Java.awt.image i javax. imageio klasy pakietów do projektu.

Zmniejsz format PDF Rozmiar wyjścia

Podczas zapisywania do PDF możesz określić, czy chcesz zoptymalizować wyjście. Aby to zrobić, musisz ustawić OptimizeOutput do true, a następnie zbędne zagnieżdżone płótna i puste płótna zostaną usunięte, sąsiad glyphs z tym samym formatowaniem będzie zwięzłe.

Poniższy przykład kodu pokazuje jak zoptymalizować wyjście:

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

Zobacz również