parent
3868b4eca2
commit
471865e4a3
3 changed files with 54 additions and 4 deletions
|
@ -27,7 +27,9 @@ import io.github.pixee.security.Filenames;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
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.model.api.PDFWithPageNums;
|
||||||
|
import stirling.software.SPDF.utils.PdfUtils;
|
||||||
import stirling.software.SPDF.utils.WebResponseUtils;
|
import stirling.software.SPDF.utils.WebResponseUtils;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
@ -49,6 +51,7 @@ public class SplitPDFController {
|
||||||
// open the pdf document
|
// open the pdf document
|
||||||
|
|
||||||
PDDocument document = Loader.loadPDF(file.getBytes());
|
PDDocument document = Loader.loadPDF(file.getBytes());
|
||||||
|
PdfMetadata metadata = PdfUtils.extractMetadataFromPdf(document);
|
||||||
int totalPages = document.getNumberOfPages();
|
int totalPages = document.getNumberOfPages();
|
||||||
List<Integer> pageNumbers = request.getPageNumbersList(document, false);
|
List<Integer> pageNumbers = request.getPageNumbersList(document, false);
|
||||||
System.out.println(
|
System.out.println(
|
||||||
|
@ -75,6 +78,9 @@ public class SplitPDFController {
|
||||||
}
|
}
|
||||||
previousPageNumber = splitPoint + 1;
|
previousPageNumber = splitPoint + 1;
|
||||||
|
|
||||||
|
// Transfer metadata to split pdf
|
||||||
|
PdfUtils.setMetadataToPdf(splitDocument, metadata);
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
splitDocument.save(baos);
|
splitDocument.save(baos);
|
||||||
|
|
||||||
|
|
19
src/main/java/stirling/software/SPDF/model/PdfMetadata.java
Normal file
19
src/main/java/stirling/software/SPDF/model/PdfMetadata.java
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -19,11 +19,8 @@ import javax.imageio.stream.ImageOutputStream;
|
||||||
|
|
||||||
import org.apache.pdfbox.Loader;
|
import org.apache.pdfbox.Loader;
|
||||||
import org.apache.pdfbox.cos.COSName;
|
import org.apache.pdfbox.cos.COSName;
|
||||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
import org.apache.pdfbox.pdmodel.*;
|
||||||
import org.apache.pdfbox.pdmodel.PDPage;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
||||||
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
|
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.common.PDRectangle;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
import org.apache.pdfbox.pdmodel.graphics.PDXObject;
|
||||||
import org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject;
|
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 io.github.pixee.security.Filenames;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.model.PdfMetadata;
|
||||||
|
|
||||||
public class PdfUtils {
|
public class PdfUtils {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
|
private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);
|
||||||
|
@ -421,4 +420,30 @@ public class PdfUtils {
|
||||||
logger.info("PDF successfully saved to byte array");
|
logger.info("PDF successfully saved to byte array");
|
||||||
return baos.toByteArray();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue