แปลงเอกสารเป็นPDF
ความสามารถในการแปลงเอกสารจากรูปแบบหนึ่งไปยังอีกรูปแบบหนึ่งได้อย่างง่ายดายและเชื่อถือได้คือคุณลักษณะสำคัญของAspose.Words หนึ่งในรูปแบบที่นิยมมากที่สุดสำหรับการแปลงคือPDF–รูปแบบคงที่เค้าโครงซึ่งจะรักษาลักษณะเดิมของเอกสารในระหว่างการแสดงผลบนแพลตฟอร์มต่างๆ คำว่า"การแสดงผล"ถูกใช้ในAspose.Wordsเพื่ออธิบายกระบวนการของการแปลงเอกสารเป็นรูปแบบแฟ้.
แปลงเอกสารคำเป็นPDF
แปลงจากคำเป็นPDFเป็นกระบวนการที่ค่อนข้างซับซ้อนที่ต้องใช้หลายขั้นตอนของการคำนวณ Aspose.Wordsเครื่องยนต์เค้าโครงเลียนแบบวิธีการทำงานของเครื่องยนต์เค้าโครงหน้าMicrosoft Wordทำให้เอกสารที่ส่งออกPDFดูใกล้เคียงที่สุดกับสิ่งที่คุณเห็นในMicrosoft Word.
ด้วยAspose.WordsคุณสามารถแปลงเอกสารจากDOCหรือDOCXรูปแบบเป็นPDFโดยไม่ต้องใช้สำนักงานMicrosoft บทความนี้อธิบายวิธีการดำเนินการแปลงนี้.
กำลังแปลงDOCXหรือDOCเป็นPDF
การแปลงจากDOCหรือDOCXรูปแบบเอกสารลงในรูปแบบPDFในAspose.Wordsเป็นเรื่องง่ายมากและสามารถทำได้ด้วยรหัสสองบรรทัดที่:
- ป้อนเอกสารของคุณลงในวัตถุDocumentโดยใช้หนึ่งในตัวสร้างโดยการระบุชื่อเอกสารที่มีนามสกุล.
- เรียกหนึ่งในวิธีการ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-Java.git. | |
Document doc = new Document(getMyDir() + "Document.docx"); | |
doc.save(getArtifactsDir() + "BaseConversions.DocxToPdf.pdf"); |
คุณสามารถดาวน์โหลดไฟล์แม่แบบของตัวอย่างนี้ได้จาก Aspose.Words GitHub.
บางครั้งจำเป็นต้องระบุตัวเลือกเพิ่มเติมซึ่งอาจส่งผลต่อผลลัพธ์ของการบันทึกเอกสารเป็นPDF อ็อพชันนี้สามารถระบุได้โดยใช้คลาสของPdfSaveOptionsซึ่งประกอบด้วยคุณสมบัติที่กำหนดวิธีแสดงผลลัพธ์PDF.
โปรดทราบว่าด้วยเทคนิคเดียวกันคุณสามารถแปลงเอกสารรูปแบบโฟลวเค้าโครงเป็นPDFรูปแ.
แปลงเป็นมาตรฐานPDFที่แตกต่างกัน
Aspose.Wordsให้การแจงนับPdfCompliaceเพื่อสนับสนุนการแปลงDOCหรือDOCXเป็นมาตรฐานรูปแบบต่างๆPDF(เช่นPDF 1.7, PDF 1.5, ฯลฯ).
ตัวอย่างรหัสต่อไปนี้แสดงให้เห็นถึงวิธีการแปลงเอกสารเป็นPDF1.7โดยใช้PdfSaveOptionsกับการปฏิบัติตาม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); |
แปลงภาพเป็น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-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); | |
} |
เมื่อต้องการให้รหัสนี้ทำงานคุณต้องเพิ่มการอ้างอิงไปยังAspose.Words,Javaโอ้วภาพและจาวาแพคเกจระดับภาพไปยังโครงการของคุณ.
ลดขนาดเอาท์พุทPDF
เมื่อบันทึกเป็นPDFคุณสามารถระบุว่าคุณต้องการเพิ่มประสิทธิภาพผลลัพธ์ เมื่อต้องการทำเช่นนี้คุณจะต้องตั้งค่าธงOptimizeOutputเป็นจริงแล้วผืนผ้าใบที่ซ้อนกันซ้ำซ้อนและผืนผ้าใบที่ว่างเปล่าจะถูกลบออกสัญลักษณ์เพื่อนบ้านที่มีการจัดรูปแบบเดียวกันจะถูกเชื่อมต่อ.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการเพิ่มประสิทธิภาพผลลัพธ์:
// 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); |
ดูเพิ่มเติม
- บทความ การแสดงผล สำหรับข้อมูลเพิ่มเติมเกี่ยวกับหน้าคงที่และรูปแบบเค้าโครงโฟลว์
- บทความ แปลงเป็นรูปแบบหน้าถาวร สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเค้าโครงหน้า
- บทความ ระบุตัวเลือกการแสดงผลเมื่อแปลงเป็นPDF สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการใช้
PdfSaveOptions
คลาส