Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
DimK10 committed Apr 8, 2024
1 parent 3868b4e commit a86529d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import stirling.software.SPDF.model.PdfMetadata;
import stirling.software.SPDF.model.api.PDFWithPageNums;
import stirling.software.SPDF.utils.PdfUtils;
import stirling.software.SPDF.utils.WebResponseUtils;

@RestController
Expand All @@ -49,6 +51,7 @@ public ResponseEntity<byte[]> splitPdf(@ModelAttribute PDFWithPageNums request)
// open the pdf document

PDDocument document = Loader.loadPDF(file.getBytes());
PdfMetadata metadata = PdfUtils.extractMetadataFromPdf(document);
int totalPages = document.getNumberOfPages();
List<Integer> pageNumbers = request.getPageNumbersList(document, false);
System.out.println(
Expand All @@ -75,6 +78,9 @@ public ResponseEntity<byte[]> splitPdf(@ModelAttribute PDFWithPageNums request)
}
previousPageNumber = splitPoint + 1;

// Transfer metadata to split pdf
PdfUtils.setMetadataToPdf(splitDocument, metadata);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
splitDocument.save(baos);

Expand Down
19 changes: 19 additions & 0 deletions src/main/java/stirling/software/SPDF/model/PdfMetadata.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package stirling.software.SPDF.model;

import java.util.Calendar;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class PdfMetadata {
private String author;
private String producer;
private String title;
private String creator;
private String subject;
private String keywords;
private Calendar creationDate;
private Calendar modificationDate;
}
33 changes: 29 additions & 4 deletions src/main/java/stirling/software/SPDF/utils/PdfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
Expand All @@ -39,6 +36,8 @@

import io.github.pixee.security.Filenames;

import stirling.software.SPDF.model.PdfMetadata;

public class PdfUtils {

private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
Expand Down Expand Up @@ -421,4 +420,30 @@ public static byte[] overlayImage(
logger.info("PDF successfully saved to byte array");
return baos.toByteArray();
}

public static PdfMetadata extractMetadataFromPdf(PDDocument pdf) {
return PdfMetadata.builder()
.author(pdf.getDocumentInformation().getAuthor())
.producer(pdf.getDocumentInformation().getProducer())
.title(pdf.getDocumentInformation().getTitle())
.creator(pdf.getDocumentInformation().getCreator())
.subject(pdf.getDocumentInformation().getSubject())
.keywords(pdf.getDocumentInformation().getKeywords())
.creationDate(pdf.getDocumentInformation().getCreationDate())
.modificationDate(pdf.getDocumentInformation().getModificationDate())
.build();
}

public static PDDocument setMetadataToPdf(PDDocument pdf, PdfMetadata pdfMetadata) {
pdf.getDocumentInformation().setAuthor(pdfMetadata.getAuthor());
pdf.getDocumentInformation().setProducer(pdfMetadata.getProducer());
pdf.getDocumentInformation().setTitle(pdfMetadata.getTitle());
pdf.getDocumentInformation().setCreator(pdfMetadata.getCreator());
pdf.getDocumentInformation().setSubject(pdfMetadata.getSubject());
pdf.getDocumentInformation().setKeywords(pdfMetadata.getKeywords());
pdf.getDocumentInformation().setCreationDate(pdfMetadata.getCreationDate());
pdf.getDocumentInformation().setModificationDate(pdfMetadata.getModificationDate());

return pdf;
}
}

0 comments on commit a86529d

Please sign in to comment.