บันทึกงานนำเสนอบน Android

ภาพรวม

หลังจากที่คุณสร้างงานนำเสนอหรือเปิดงานนำเสนอที่มีอยู่แล้ว, ใช้เมธอดPresentation.saveเพื่อเขียนผลลัพธ์ Aspose.Slides for Android via Java สามารถบันทึกงานนำเสนอเป็นไฟล์หรือสตรีมในรูปแบบ PowerPoint, OpenDocument, PDF และรูปแบบอื่นๆ ส่วนต่อไปนี้ครอบคลุมการดำเนินการบันทึกมาตรฐานและตัวเลือกที่มีสำหรับผลลัพธ์ PPTX

บันทึกงานนำเสนอเป็นไฟล์

เพื่อบันทึกงานนำเสนอเป็นไฟล์, ให้ส่งพาธเอาต์พุตและค่าSaveFormatไปยังเมธอดPresentation.save ค่าฟอร์แมตกำหนดประเภทของไฟล์ที่ Aspose.Slides สร้าง

ตัวอย่างต่อไปนี้สร้างงานนำเสนอและบันทึกเป็นไฟล์ PPTX:

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation();
try {
    // เพิ่มหรือแก้ไขเนื้อหางานนำเสนอที่นี่.

    presentation.save("Output.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอในรูปแบบต้นฉบับของมัน

สำหรับตัวอย่างการตรวจจับไฟล์และสตรีม, พฤติกรรมของงานนำเสนอที่สร้างใหม่, และความแตกต่างระหว่างรูปแบบต้นทางและเอาต์พุต, ดูDetermine the Original Presentation Format

ในแอปพลิเคชันประมวลผลเป็นชุด, รูปแบบอินพุตอาจไม่ทราบล่วงหน้า หลังจากโหลดไฟล์ให้อ่านรูปแบบต้นฉบับจากเมธอดIPresentation.getSourceFormat ส่งค่าที่ได้ของSourceFormatไปยังSlideUtil.toSaveFormatเพื่อรับค่าSaveFormatที่สอดคล้องกัน แล้วใช้Presentation.saveเพื่อเขียนงานนำเสนอที่แก้ไขแล้ว

ตัวอย่างเต็มต่อไปนี้ประมวลผลไฟล์ทุกไฟล์ในไดเรกทอรีอินพุต, อัปเดตชื่อเรื่องของมัน, และบันทึกไปยังไดเรกทอรีเอาต์พุตในรูปแบบที่โหลดมา:

import com.aspose.slides.Presentation;
import com.aspose.slides.SlideUtil;
import java.io.File;

File inputDirectory = new File("Input");
File outputDirectory = new File("Output");

if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
    System.err.println("Cannot create the output directory.");
}

File[] inputFiles = inputDirectory.listFiles(File::isFile);
if (inputFiles != null && outputDirectory.isDirectory()) {
    for (File inputFile : inputFiles) {
        try {
            Presentation presentation = new Presentation(inputFile.getPath());
            try {
                int saveFormat = SlideUtil.toSaveFormat(presentation.getSourceFormat());
                presentation.getDocumentProperties().setTitle("Processed by the batch application");

                File outputFile = new File(outputDirectory, inputFile.getName());
                presentation.save(outputFile.getPath(), saveFormat);
            } finally {
                presentation.dispose();
            }
        } catch (IllegalArgumentException exception) {
            System.err.println("Cannot map the source format of '" + inputFile.getPath() + "': " + exception.getMessage());
        } catch (Exception exception) {
            System.err.println("Cannot process '" + inputFile.getPath() + "': " + exception.getMessage());
        }
    }
}

SlideUtil.toSaveFormat แมป PPT, PPTX, ODP, PPTM, PPSX, PPSM, POTX, POTM, PPS, POT, OTP, FODP และ PowerPoint XML ไปยังรูปแบบบันทึกงานนำเสนอที่สอดคล้องกัน มันแมปเฉพาะรูปแบบต้นทางของงานนำเสนอ; ไม่ได้ตั้งใจให้เลือกรูปแบบส่งออกเช่น PDF, HTML, TIFF หรือรูปภาพ การส่งค่าที่ไม่รองรับหรือไม่ถูกต้องของSourceFormatจะทำให้เกิดIllegalArgumentException

ไฟล์ PPT, PPS, และ POT รุ่นเก่าใช้คอนเทนเนอร์ไบนารีเดียวกัน เมื่อโหลดงานนำเสนอจากสตรีมที่ไม่มีส่วนขยายไฟล์ ไฟล์ PPS หรือ POT อาจถูกระบุเป็น PPT หากต้องการรักษาช่วงย่อยรุ่นเก่าเหล่านี้ไว้ ให้เก็บชื่อไฟล์หรือเมตาดาต้ารูปแบบต้นฉบับแยกต่างหากและใช้เมื่อเลือกชื่อไฟล์และรูปแบบเอาต์พุต

บันทึกงานนำเสนอเป็นสตรีม

เพื่อเขียนงานนำเสนอโดยไม่ต้องอ้างอิงพาธไฟล์สุดท้าย, ให้ส่งสตรีมที่เขียนได้และค่าSaveFormatไปยังเมธอดPresentation.save วิธีนี้มีประโยชน์เมื่อเอาต์พุตต้องคืนค่าจากเว็บเซอร์วิส, เก็บในฐานข้อมูล, หรือประมวลผลในหน่วยความจำ

ตัวอย่างต่อไปนี้บันทึกงานนำเสนอใหม่ไปยังสตรีมไฟล์:

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import java.io.FileOutputStream;
import java.io.OutputStream;

Presentation presentation = new Presentation();
try {
    OutputStream outputStream = new FileOutputStream("Output.pptx");
    try {
        presentation.save(outputStream, SaveFormat.Pptx);
    } finally {
        outputStream.close();
    }
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอด้วยประเภทมุมมองที่กำหนดล่วงหน้า

คุณสามารถระบุมุมมองที่ PowerPoint เปิดงานนำเสนอที่บันทึกไว้ครั้งแรกได้ ใช้เมธอดViewProperties.setLastViewร่วมกับค่าViewTypeก่อนบันทึก

ตัวอย่างต่อไปนี้กำหนดให้มุมมอง Slide Master เป็นมุมมองเริ่มต้น:

import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.ViewType;

Presentation presentation = new Presentation();
try {
    presentation.getViewProperties().setLastView(ViewType.SlideMasterView);
    presentation.save("SlideMasterView.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอในรูปแบบ Strict Office Open XML

เพื่อสร้างไฟล์ PPTX ที่สอดคล้องกับโปรไฟล์ Strict ของ Office Open XML, สร้างอินสแตนซ์PptxOptionsและใช้เมธอดsetConformanceร่วมกับConformance.Iso29500_2008_Strict จากนั้นส่งอ็อปชันไปยังเมธอดPresentation.save

import com.aspose.slides.Conformance;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

PptxOptions options = new PptxOptions();
options.setConformance(Conformance.Iso29500_2008_Strict);

Presentation presentation = new Presentation();
try {
    presentation.save("StrictOfficeOpenXml.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอในรูปแบบ Office Open XML ในโหมด Zip64

ไฟล์ ZIP มาตรฐานจำกัดขนาดข้อมูลบีบอัดและไม่บีบอัดของแต่ละรายการ, ขนาดทั้งหมดของไฟล์ ZIP, และจำนวนรายการ เนื่องจากไฟล์ PPTX เป็นไฟล์ ZIP, งานนำเสนอที่มีขนาดใหญ่มากอาจเกินขีดจำกัดเหล่านั้น ส่วนขยาย ZIP64 จึงเพิ่มขีดจำกัดขนาดและจำนวนรายการที่ใช้ได้

ใช้เมธอดPptxOptions.setZip64Modeเพื่อควบคุมว่าการเขียนส่วนขยาย ZIP64 จะทำหรือไม่:

  • IfNecessary ใช้ ZIP64 เฉพาะเมื่องานนำเสนอเกินขีดจำกัด ZIP มาตรฐาน นี่เป็นโหมดเริ่มต้น
  • Never ปิดการใช้ส่วนขยาย ZIP64
  • Always เขียนส่วนขยาย ZIP64 เสมอสำหรับเอาต์พุต

ตัวอย่างต่อไปนี้เปิดใช้งานส่วนขยาย ZIP64 เสมอสำหรับงานนำเสนอเอาต์พุต:

import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.Zip64Mode;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setZip64Mode(Zip64Mode.Always);

    presentation.save("OutputZip64.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอในรูปแบบ Office Open XML พร้อมระดับการบีบอัด

สำหรับผลลัพธ์ PPTX, คุณสามารถปรับสมดุลระหว่างความเร็วในการบันทึกและขนาดไฟล์โดยใช้เมธอดPptxOptions.setCompressionLevel คลาสCompressionLevel มีค่าต่อไปนี้:

  • None จัดเก็บข้อมูลโดยไม่บีบอัด
  • Level1 ให้การบีบอัดที่เร็วที่สุดและผลลัพธ์ที่บีบอัดขนาดใหญ่ที่สุด
  • Level2 ถึง Level5 ค่อย ๆ ให้ความสำคัญกับผลลัพธ์ที่เล็กลงมากกว่าความเร็วในการบันทึก
  • Level6 สมดุลระหว่างความเร็วในการบันทึกและขนาดไฟล์ นี่เป็นระดับค่าเริ่มต้น
  • Level7 และ Level8 ให้ความสำคัญกับผลลัพธ์ที่เล็กลงมากกว่าความเร็วในการบันทึกเพิ่มเติม
  • Level9 ให้การบีบอัดที่เข้มที่สุดและต้องการเวลาประมวลผลมากที่สุด

ตัวอย่างต่อไปนี้บันทึกงานนำเสนอโดยไม่มีการบีบอัด:

import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setCompressionLevel(CompressionLevel.None);

    presentation.save("OutputNoCompression.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

ตัวอย่างต่อไปนี้ใช้ระดับการบีบอัดสูงสุด:

import com.aspose.slides.CompressionLevel;
import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setCompressionLevel(CompressionLevel.Level9);

    presentation.save("OutputMaximumCompression.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

บันทึกงานนำเสนอโดยไม่รีเฟรชภาพย่อ

เมื่อบันทึกงานนำเสนอเป็น PPTX, เมธอดPptxOptions.setRefreshThumbnail ควบคุมภาพย่อของเอกสาร:

  • true สร้างภาพย่อใหม่ในระหว่างการบันทึก นี่เป็นค่าเริ่มต้น
  • false รักษาภาพย่อที่มีอยู่ หากงานนำเสนอไม่มีภาพย่อ Aspose.Slides จะไม่สร้างขึ้น

ตัวอย่างต่อไปนี้บันทึกงานนำเสนอโดยไม่รีเฟรชภาพย่อของมัน:

import com.aspose.slides.PptxOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

Presentation presentation = new Presentation("Sample.pptx");
try {
    PptxOptions options = new PptxOptions();
    options.setRefreshThumbnail(false);

    presentation.save("Output.pptx", SaveFormat.Pptx, options);
} finally {
    presentation.dispose();
}

บันทึกการอัปเดตความคืบหน้าเป็นเปอร์เซ็นต์

เพื่อเฝ้าติดตามการบันทึก, ให้ทำการติดตั้งอินเทอร์เฟซIProgressCallbackและส่งอิมพลิเมนต์ไปยังเมธอดISaveOptions.setProgressCallback Aspose.Slides จะเรียกเมธอดIProgressCallback.reporting พร้อมค่าความคืบหน้าในระหว่างการส่งออก

ตัวอย่างต่อไปนี้รายงานความคืบหน้าในการส่งออก PDF ไปยังคอนโซล:

import com.aspose.slides.IProgressCallback;
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

class ExportProgressHandler implements IProgressCallback {
    public void reporting(double progressValue) {
        int progress = (int) progressValue;
        System.out.println(progress + "% of the file has been converted.");
    }
}

PdfOptions options = new PdfOptions();
options.setProgressCallback(new ExportProgressHandler());

Presentation presentation = new Presentation("Sample.pptx");
try {
    presentation.save("Output.pdf", SaveFormat.Pdf, options);
} finally {
    presentation.dispose();
}

คำถามที่พบบ่อย

Aspose.Slides รองรับการบันทึกแบบเพิ่มส่วนหรือ “fast save” หรือไม่?

ไม่ ระบบบันทึกแต่ละครั้งจะเขียนไฟล์เอาต์พุตที่สมบูรณ์ ไม่ได้อัปเดตเฉพาะส่วนที่เปลี่ยนแปลงเท่านั้น

หลายเธรดสามารถบันทึกอินสแตนซ์ Presentation เดียวกันได้หรือไม่?

ไม่ อินสแตนซ์Presentation ไม่ได้เป็น thread‑safe[/slides/th/androidjava/multithreading/] ให้เข้าถึงและบันทึกแต่ละอินสแตนซ์จากเธรดเดียวเท่านั้น

ลิงก์และไฟล์ที่เชื่อมโยงภายนอกจะเกิดอะไรขึ้นเมื่อบันทึกงานนำเสนอ?

Hyperlinks จะคงอยู่ในงานนำเสนอ Aspose.Slides ไม่ทำการคัดลอกไฟล์ที่เชื่อมโยงภายนอก ดังนั้นงานนำเสนอที่บันทึกไว้ต้องยังคงเข้าถึงตำแหน่งนั้นได้

ฉันสามารถบันทึกเมตาดาต้าเอกสาร เช่น ผู้เขียน, ชื่อเรื่อง, บริษัทและวันสร้างได้หรือไม่?

ได้ ตั้งคุณสมบัติdocument propertiesที่ต้องการก่อนบันทึก แล้ว Aspose.Slides จะเขียนลงในไฟล์เอาต์พุต