diff --git a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java index e415aa9d..51efa644 100644 --- a/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/api/SplitPDFController.java @@ -27,7 +27,9 @@ import io.github.pixee.security.Filenames; 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 @@ -49,6 +51,7 @@ public class SplitPDFController { // open the pdf document PDDocument document = Loader.loadPDF(file.getBytes()); + PdfMetadata metadata = PdfUtils.extractMetadataFromPdf(document); int totalPages = document.getNumberOfPages(); List pageNumbers = request.getPageNumbersList(document, false); System.out.println( @@ -75,6 +78,9 @@ public class SplitPDFController { } previousPageNumber = splitPoint + 1; + // Transfer metadata to split pdf + PdfUtils.setMetadataToPdf(splitDocument, metadata); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); splitDocument.save(baos); diff --git a/src/main/java/stirling/software/SPDF/model/PdfMetadata.java b/src/main/java/stirling/software/SPDF/model/PdfMetadata.java new file mode 100644 index 00000000..35054bc8 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/PdfMetadata.java @@ -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; +} diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 3d16f131..f51eff2a 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -19,11 +19,8 @@ import javax.imageio.stream.ImageOutputStream; 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; @@ -39,6 +36,8 @@ import org.springframework.web.multipart.MultipartFile; import io.github.pixee.security.Filenames; +import stirling.software.SPDF.model.PdfMetadata; + public class PdfUtils { private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class); @@ -421,4 +420,30 @@ public class PdfUtils { 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; + } }