PDF Document Manipulation ב-Java

Aspose.PDF FOSS for Java provides a rich set of APIs for manipulating existing PDF מסמכים – חלוקת, קיצוץ, פריצת עמודים, כניסת שורות של עמוד, ו תצוגה של תוכן בתוך הדפים.היכולות הללו ממוקדות ב PdfFileEditor ושל הקבוצות החברתיות שלה ContentsResizeParameters ו ContentsResizeValue.

PDFFileEditor: מבצעי דף הליבה

The PdfFileEditor שיעור הוא המקום המרכזי עבור פעולות קובץ PDF ברמה גבוהה. הוא מספק שיטות להדביק קבצים PDF מרובים, לחלץ תת-חתיכות דף, לחלק מסמך לדפים בודדים, ולשלוח דפים מתוך מסכם אחד. במקום אחר במצב מסוים.

PdfFileEditor editor = new PdfFileEditor();
// Concatenate two documents into one
editor.concatenate("first.pdf", "second.pdf", "combined.pdf");
// Extract pages 2 through 5 to a new file
editor.extract("source.pdf", 2, 5, "extracted.pdf");

שיטות מרכזיות כוללות:

  • concatenate(inputFiles, outputFile) • שילוב של מספר מסמכים על מנת
  • extract(inputFile, startPage, endPage, outputFile) - ייצוא מגוון נוגדנים
  • extract(inputFile, pageNumbers, outputFile) - ייצוא של קבוצה שאינה קבועה
  • splitToPages(inputFile) מחולקים לתוך אחד ByteArrayOutputStream לפי דף
  • insert(inputFile, insertPosition, portFile, startPage, endPage, outputFile) להוסיף רצועה
  • delete(inputFile, pageNumbers, outputFile) להסיר דפים ספציפיים
  • append(inputFile, portFile, startPage, endPage, outputFile) - להוסיף מגוון דף

עבור קבצים גדולים, setUseDiskBuffer(true) החלפת אחסון פנימי מהחבל אל הדיסק.

תוכן מחדש עם ContentResizeParameters

PdfFileEditor.resizeContents() מתאים את התוכן בתוך גבולות הדף הקיימים. כל מגרש – שמאל, ימין, העליון, התחתון – והרחבה והגובה של התוכן הם: מתוארים כ ContentsResizeValue שיטות, שנוצרו באמצעות שיטת המפעל ContentsResizeValue.percents(value) או ContentsResizeValue.units(value).

try (Document doc = new Document("input.pdf")) {
    PdfFileEditor editor = new PdfFileEditor();
    ContentsResizeParameters params = new ContentsResizeParameters(
        ContentsResizeValue.percents(10),   // left margin
        ContentsResizeValue.percents(80),   // content width
        ContentsResizeValue.percents(10),   // right margin
        ContentsResizeValue.percents(10),   // top margin
        ContentsResizeValue.percents(80),   // content height
        ContentsResizeValue.percents(10)    // bottom margin
    );
    editor.resizeContents(doc, params);
    doc.save("resized.pdf");
}

6 תוצאות ב-The Constructor for ContentsResizeParameters מקבלים ערכים ב- הזמנה: leftMargin, contentsWidth, rightMargin, topM argin , contentHeight, bottomMargent. כל שישה חייבים לסכום עד 100% בעת שימוש בערכים מבוססי אחוז, או להתאים את הדף. שימוש במדדים של ערכים נקודות מוחלטות.

חידוש דפים ספציפיים

כאשר רק תת-סדרה של דפים דורשת חידוש, העומס המקבל סדרה מלאה של מספרים בדף ממוקד את הדפים האלה תוך שמירה על השאר ללא שינוי:

PdfFileEditor editor = new PdfFileEditor();
ContentsResizeParameters params = new ContentsResizeParameters(
    ContentsResizeValue.units(28),   // left margin in points
    ContentsResizeValue.units(540),  // content width
    ContentsResizeValue.units(28),   // right margin
    ContentsResizeValue.units(28),   // top margin
    ContentsResizeValue.units(786),  // content height
    ContentsResizeValue.units(28)    // bottom margin
);
// Only resize pages 1 and 3
editor.resizeContents(doc, new int[]{1, 3}, params);

ContentsResizeValue.getValue() ו isPercent() אפשרות לבדוק את המאגר הערך וההחלטה אם הוא מייצג אחוז או מספר נקודות מוחלט.

תגיות: N-Up Layout

PdfFileEditor כולל makeBooklet() על מנת להטיל דפים בסדר ספרייה משני צדדים ו makeNUp() כדי לצייר מספר עמודים על לוח.שני השיטות מקבלות מסלולי קובץ או זרם :

PdfFileEditor editor = new PdfFileEditor();
// Print as a folded booklet
editor.makeBooklet("input.pdf", "booklet.pdf");
// Tile 2 columns × 3 rows per sheet
editor.makeNUp("input.pdf", "nup.pdf", 2, 3);

PdfPageEditor: Per-Pay Zoom Control – בקרת זום לפי דף

PdfPageEditor מטרות תכונות דף בודדות כגון רמת זום. setZoom() מקבלת מתרגל זרימה אשר שולט בהגדילה ראשונית כאשר המסמך פותחים את זה ב-View:

PdfPageEditor pageEditor = new PdfPageEditor();
pageEditor.bindPdf("input.pdf");
pageEditor.setZoom(1.5f);
pageEditor.save("zoomed.pdf");

ערכים של 0.0 או שהרעיון שלילי נדחה - getZoom() חזרה 1.0 במקרים אלה. זה עושה PdfPageEditor מתאים להכנת מסמכים אשר חייבים להיות פתוחים ב- הגדלת מקרים של גישה או שימוש במראה קיוסק.

להתחיל

הוסף את ההסתמכות של Maven ולהיבוא את חבילת הפסד:

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-pdf</artifactId>
  <version>0.1.0-alpha</version>
</dependency>
import org.aspose.pdf.*;
import org.aspose.pdf.facades.*;

Aspose.PDF FOSS for Java is MIT-licensed and available at https://github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Java.

משאבים קשורים