להפוך מסמך ל- PDF
היכולת להמיר בקלות וביעילות מסמכים מתבנית אחת לאחרת היא תכונה מרכזית של Aspose.Words. אחד הפורמטים הפופולריים ביותר להמיר הוא PDF - פורמט קבוע עיכובים, אשר משמר את המראה המקורי של מסמך במהלך ביצועו בפלטפורמות שונות. המונח “מחדש” משמש Aspose.Words כדי לתאר את התהליך של המרת מסמך לתוך תבנית קובץ שדמיינו או יש מושג של דפים.
להפוך מסמך Word ל- PDF
הסחה מ- Word ל- PDF היא תהליך מורכב למדי הדורש מספר שלבים של חישוב. Aspose.Words מנוע הפריסה מחק את הדרך Microsoft Wordמנוע פריסת העמוד עובד, מה שהופך מסמכי תפוקה של PDF נראים קרובים ככל האפשר למה שאתה יכול לראות. Microsoft Word.
עם Aspose.Words באפשרותך להמיר מסמך מתבנית DOC או DOCX ל- PDF ללא שימוש Microsoft משרד. מאמר זה מסביר כיצד לבצע המרה זו.
המרת DOCX או DOC ל- PDF
המרת פורמט מסמך DOC או DOCX לתוך פורמט PDF בפורמט PDF בפורמט PDF Aspose.Words זה מאוד קל וניתן להשיג באמצעות שני שורות קוד:
1.1 1. לטעון את המסמך שלך לתוך Document להתנגד באמצעות אחד מבניו על ידי ציון שם המסמך עם הרחבה פורמט שלה. 1.1 1. Invoke אחד Document.Save שיטות על Document אובייקט ומציין את פורמט הפלט הרצוי כ- PDF על ידי כניסה לקובץ עם הרחבה “PDF”.
הדוגמה הבאה של הקוד מראה כיצד להמיר מסמך מ- DOCX ל- PDF באמצעות שימוש ב- 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 (כגון PDF1.7, PDF וכו ‘).
הדוגמה הבאה של הקוד מראה כיצד להמיר מסמך ל- PDF1.7 באמצעות שימוש ב- PDF 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תמונה ו-Jvax חבילות כיתה לפרוייקט שלך.
הקטנת PDF גודל בחוץ
בעת שמירת PDF, באפשרותך לציין אם ברצונך לייעל את התפוקה. כדי לעשות זאת, עליך להגדיר את OptimizeOutput דגל true, ולאחר מכן יוסרו הבדים הנטושים והבדים הריקים, השכנים glyphs עם אותו פורמט יהיה מתואם.
הדוגמה הבאה של הקוד מראה כיצד לייעל את הפלט:
// 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); |
ראה גם
- המאמר Rendering לקבלת מידע נוסף על פורמטים קבועים של דף וזרימה
- המאמר המרת פורמט קבוע-עמוד למידע נוסף על פריסת הדף
- המאמר תגית: PDF למידע נוסף על השימוש
PdfSaveOptions
הכיתה