From 494bc2c09f494122ea00de582e3c95f9935b0b5b Mon Sep 17 00:00:00 2001 From: Hashim <85452526+Hashim0510@users.noreply.github.com> Date: Fri, 4 Oct 2024 21:23:00 +0530 Subject: [PATCH] =?UTF-8?q?commit=20for=20feature=20developing=20invert-re?= =?UTF-8?q?place=20color=20of=20a=20pdf=20for=20stirl=E2=80=A6=20(#1982)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit commit for feature developing invert-replace color of a pdf for stirling PDF --- .../ReplaceAndInvertColorFactory.java | 39 +++++ .../misc/ReplaceAndInvertColorController.java | 55 ++++++ .../controller/web/OtherWebController.java | 7 + .../misc/HighContrastColorCombination.java | 8 + .../SPDF/model/api/misc/ReplaceAndInvert.java | 7 + .../misc/ReplaceAndInvertColorRequest.java | 40 +++++ .../misc/ReplaceAndInvertColorService.java | 42 +++++ .../misc/CustomColorReplaceStrategy.java | 163 ++++++++++++++++++ .../misc/HighContrastColorReplaceDecider.java | 30 ++++ .../utils/misc/InvertFullColorStrategy.java | 104 +++++++++++ .../utils/misc/PdfTextStripperCustom.java | 36 ++++ .../misc/ReplaceAndInvertColorStrategy.java | 24 +++ src/main/resources/messages_en_GB.properties | 20 +++ src/main/resources/messages_en_US.properties | 24 +++ .../resources/templates/fragments/navbar.html | 3 + src/main/resources/templates/home.html | 8 +- .../templates/misc/replace-color.html | 89 ++++++++++ 17 files changed, 697 insertions(+), 2 deletions(-) create mode 100644 src/main/java/stirling/software/SPDF/Factories/ReplaceAndInvertColorFactory.java create mode 100644 src/main/java/stirling/software/SPDF/controller/api/misc/ReplaceAndInvertColorController.java create mode 100644 src/main/java/stirling/software/SPDF/model/api/misc/HighContrastColorCombination.java create mode 100644 src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvert.java create mode 100644 src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvertColorRequest.java create mode 100644 src/main/java/stirling/software/SPDF/service/misc/ReplaceAndInvertColorService.java create mode 100644 src/main/java/stirling/software/SPDF/utils/misc/CustomColorReplaceStrategy.java create mode 100644 src/main/java/stirling/software/SPDF/utils/misc/HighContrastColorReplaceDecider.java create mode 100644 src/main/java/stirling/software/SPDF/utils/misc/InvertFullColorStrategy.java create mode 100644 src/main/java/stirling/software/SPDF/utils/misc/PdfTextStripperCustom.java create mode 100644 src/main/java/stirling/software/SPDF/utils/misc/ReplaceAndInvertColorStrategy.java create mode 100644 src/main/resources/templates/misc/replace-color.html diff --git a/src/main/java/stirling/software/SPDF/Factories/ReplaceAndInvertColorFactory.java b/src/main/java/stirling/software/SPDF/Factories/ReplaceAndInvertColorFactory.java new file mode 100644 index 00000000..6aeb4b2d --- /dev/null +++ b/src/main/java/stirling/software/SPDF/Factories/ReplaceAndInvertColorFactory.java @@ -0,0 +1,39 @@ +package stirling.software.SPDF.Factories; + +import org.springframework.stereotype.Component; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.model.api.misc.HighContrastColorCombination; +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; +import stirling.software.SPDF.utils.misc.CustomColorReplaceStrategy; +import stirling.software.SPDF.utils.misc.InvertFullColorStrategy; +import stirling.software.SPDF.utils.misc.ReplaceAndInvertColorStrategy; + +@Component +public class ReplaceAndInvertColorFactory { + + public ReplaceAndInvertColorStrategy replaceAndInvert( + MultipartFile file, + ReplaceAndInvert replaceAndInvertOption, + HighContrastColorCombination highContrastColorCombination, + String backGroundColor, + String textColor) { + + if (replaceAndInvertOption == ReplaceAndInvert.CUSTOM_COLOR + || replaceAndInvertOption == ReplaceAndInvert.HIGH_CONTRAST_COLOR) { + + return new CustomColorReplaceStrategy( + file, + replaceAndInvertOption, + textColor, + backGroundColor, + highContrastColorCombination); + + } else if (replaceAndInvertOption == ReplaceAndInvert.FULL_INVERSION) { + + return new InvertFullColorStrategy(file, replaceAndInvertOption); + } + + return null; + } +} diff --git a/src/main/java/stirling/software/SPDF/controller/api/misc/ReplaceAndInvertColorController.java b/src/main/java/stirling/software/SPDF/controller/api/misc/ReplaceAndInvertColorController.java new file mode 100644 index 00000000..c7296a1b --- /dev/null +++ b/src/main/java/stirling/software/SPDF/controller/api/misc/ReplaceAndInvertColorController.java @@ -0,0 +1,55 @@ +package stirling.software.SPDF.controller.api.misc; + +import java.io.IOException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import io.swagger.v3.oas.annotations.Operation; + +import stirling.software.SPDF.model.api.misc.ReplaceAndInvertColorRequest; +import stirling.software.SPDF.service.misc.ReplaceAndInvertColorService; + +@RestController +@RequestMapping("/api/v1/misc") +public class ReplaceAndInvertColorController { + + private ReplaceAndInvertColorService replaceAndInvertColorService; + + @Autowired + public ReplaceAndInvertColorController( + ReplaceAndInvertColorService replaceAndInvertColorService) { + this.replaceAndInvertColorService = replaceAndInvertColorService; + } + + @PostMapping(consumes = "multipart/form-data", value = "/replace-invert-pdf") + @Operation( + summary = "Replace-Invert Color PDF", + description = + "This endpoint accepts a PDF file and option of invert all colors or replace text and background colors. Input:PDF Output:PDF Type:SISO") + public ResponseEntity replaceAndInvertColor( + @ModelAttribute ReplaceAndInvertColorRequest replaceAndInvertColorRequest) + throws IOException { + + InputStreamResource resource = + replaceAndInvertColorService.replaceAndInvertColor( + replaceAndInvertColorRequest.getFileInput(), + replaceAndInvertColorRequest.getReplaceAndInvertOption(), + replaceAndInvertColorRequest.getHighContrastColorCombination(), + replaceAndInvertColorRequest.getBackGroundColor(), + replaceAndInvertColorRequest.getTextColor()); + + // Return the modified PDF as a downloadable file + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=inverted.pdf") + .contentType(MediaType.APPLICATION_PDF) + .body(resource); + } +} diff --git a/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java b/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java index 8b6178bd..7f87d4f2 100644 --- a/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java +++ b/src/main/java/stirling/software/SPDF/controller/web/OtherWebController.java @@ -31,6 +31,13 @@ public class OtherWebController { return "misc/compress-pdf"; } + @GetMapping("/replace-and-invert-color-pdf") + @Hidden + public String replaceAndInvertColorPdfForm(Model model) { + model.addAttribute("currentPage", "replace-invert-color-pdf"); + return "misc/replace-color"; + } + @GetMapping("/extract-image-scans") @Hidden public ModelAndView extractImageScansForm() { diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/HighContrastColorCombination.java b/src/main/java/stirling/software/SPDF/model/api/misc/HighContrastColorCombination.java new file mode 100644 index 00000000..27c6290b --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/misc/HighContrastColorCombination.java @@ -0,0 +1,8 @@ +package stirling.software.SPDF.model.api.misc; + +public enum HighContrastColorCombination { + WHITE_TEXT_ON_BLACK, + BLACK_TEXT_ON_WHITE, + YELLOW_TEXT_ON_BLACK, + GREEN_TEXT_ON_BLACK, +} diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvert.java b/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvert.java new file mode 100644 index 00000000..c3acdc56 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvert.java @@ -0,0 +1,7 @@ +package stirling.software.SPDF.model.api.misc; + +public enum ReplaceAndInvert { + HIGH_CONTRAST_COLOR, + CUSTOM_COLOR, + FULL_INVERSION, +} diff --git a/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvertColorRequest.java b/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvertColorRequest.java new file mode 100644 index 00000000..1d96d35b --- /dev/null +++ b/src/main/java/stirling/software/SPDF/model/api/misc/ReplaceAndInvertColorRequest.java @@ -0,0 +1,40 @@ +package stirling.software.SPDF.model.api.misc; + +import io.swagger.v3.oas.annotations.media.Schema; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import stirling.software.SPDF.model.api.PDFFile; + +@Data +@EqualsAndHashCode(callSuper = true) +public class ReplaceAndInvertColorRequest extends PDFFile { + + @Schema( + description = "Replace and Invert color options of a pdf.", + allowableValues = {"HIGH_CONTRAST_COLOR", "CUSTOM_COLOR", "FULL_INVERSION"}) + private ReplaceAndInvert replaceAndInvertOption; + + @Schema( + description = + "If HIGH_CONTRAST_COLOR option selected, then pick the default color option for text and background.", + allowableValues = { + "WHITE_TEXT_ON_BLACK", + "BLACK_TEXT_ON_WHITE", + "YELLOW_TEXT_ON_BLACK", + "GREEN_TEXT_ON_BLACK" + }) + private HighContrastColorCombination highContrastColorCombination; + + @Schema( + description = + "If CUSTOM_COLOR option selected, then pick the custom color for background. " + + "Expected color value should be 24bit decimal value of a color") + private String backGroundColor; + + @Schema( + description = + "If CUSTOM_COLOR option selected, then pick the custom color for text. " + + "Expected color value should be 24bit decimal value of a color") + private String textColor; +} diff --git a/src/main/java/stirling/software/SPDF/service/misc/ReplaceAndInvertColorService.java b/src/main/java/stirling/software/SPDF/service/misc/ReplaceAndInvertColorService.java new file mode 100644 index 00000000..29df742a --- /dev/null +++ b/src/main/java/stirling/software/SPDF/service/misc/ReplaceAndInvertColorService.java @@ -0,0 +1,42 @@ +package stirling.software.SPDF.service.misc; + +import java.io.IOException; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.Factories.ReplaceAndInvertColorFactory; +import stirling.software.SPDF.model.api.misc.HighContrastColorCombination; +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; +import stirling.software.SPDF.utils.misc.ReplaceAndInvertColorStrategy; + +@Service +public class ReplaceAndInvertColorService { + private ReplaceAndInvertColorFactory replaceAndInvertColorFactory; + + @Autowired + public ReplaceAndInvertColorService(ReplaceAndInvertColorFactory replaceAndInvertColorFactory) { + this.replaceAndInvertColorFactory = replaceAndInvertColorFactory; + } + + public InputStreamResource replaceAndInvertColor( + MultipartFile file, + ReplaceAndInvert replaceAndInvertOption, + HighContrastColorCombination highContrastColorCombination, + String backGroundColor, + String textColor) + throws IOException { + + ReplaceAndInvertColorStrategy replaceColorStrategy = + replaceAndInvertColorFactory.replaceAndInvert( + file, + replaceAndInvertOption, + highContrastColorCombination, + backGroundColor, + textColor); + + return replaceColorStrategy.replace(); + } +} diff --git a/src/main/java/stirling/software/SPDF/utils/misc/CustomColorReplaceStrategy.java b/src/main/java/stirling/software/SPDF/utils/misc/CustomColorReplaceStrategy.java new file mode 100644 index 00000000..3c5753f0 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/utils/misc/CustomColorReplaceStrategy.java @@ -0,0 +1,163 @@ +package stirling.software.SPDF.utils.misc; + +import java.awt.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Set; + +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.PDPageTree; +import org.apache.pdfbox.pdmodel.font.*; +import org.apache.pdfbox.text.TextPosition; +import org.springframework.core.io.InputStreamResource; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.model.api.misc.HighContrastColorCombination; +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; + +public class CustomColorReplaceStrategy extends ReplaceAndInvertColorStrategy { + + private String textColor; + private String backgroundColor; + private HighContrastColorCombination highContrastColorCombination; + + public CustomColorReplaceStrategy( + MultipartFile file, + ReplaceAndInvert replaceAndInvert, + String textColor, + String backgroundColor, + HighContrastColorCombination highContrastColorCombination) { + super(file, replaceAndInvert); + this.textColor = textColor; + this.backgroundColor = backgroundColor; + this.highContrastColorCombination = highContrastColorCombination; + } + + @Override + public InputStreamResource replace() throws IOException { + + // If ReplaceAndInvert is HighContrastColor option, then get the colors of text and + // background from static + if (replaceAndInvert == ReplaceAndInvert.HIGH_CONTRAST_COLOR) { + String[] colors = + HighContrastColorReplaceDecider.getColors( + replaceAndInvert, highContrastColorCombination); + this.textColor = colors[0]; + this.backgroundColor = colors[1]; + } + + // Create a temporary file, with the original filename from the multipart file + File file = File.createTempFile("temp", getFileInput().getOriginalFilename()); + + // Transfer the content of the multipart file to the file + getFileInput().transferTo(file); + + try (PDDocument document = Loader.loadPDF(file)) { + + PDPageTree pages = document.getPages(); + + for (PDPage page : pages) { + + PdfTextStripperCustom pdfTextStripperCustom = new PdfTextStripperCustom(); + // Get text positions + List> charactersByArticle = + pdfTextStripperCustom.processPageCustom(page); + + // Begin a new content stream + PDPageContentStream contentStream = + new PDPageContentStream( + document, page, PDPageContentStream.AppendMode.APPEND, true, true); + + // Set the new text color + contentStream.setNonStrokingColor(Color.decode(this.textColor)); + + // Draw the text with the new color + for (List textPositions : charactersByArticle) { + for (TextPosition text : textPositions) { + // Move to the text position + contentStream.beginText(); + contentStream.newLineAtOffset( + text.getX(), page.getMediaBox().getHeight() - text.getY()); + PDFont font = null; + String unicodeText = text.getUnicode(); + try { + font = PDFontFactory.createFont(text.getFont().getCOSObject()); + } catch (IOException io) { + System.out.println("Primary font not found, using fallback font."); + font = new PDType1Font(Standard14Fonts.FontName.HELVETICA); + } + // if a character is not supported by font, then look for supported font + try { + byte[] bytes = font.encode(unicodeText); + } catch (IOException io) { + System.out.println("text could not be encoded "); + font = checkSupportedFontForCharacter(unicodeText); + } catch (IllegalArgumentException ie) { + System.out.println("text not supported by font "); + font = checkSupportedFontForCharacter(unicodeText); + } finally { + // if any other font is not supported, then replace default character * + if (font == null) { + font = new PDType1Font(Standard14Fonts.FontName.HELVETICA); + unicodeText = "*"; + } + } + contentStream.setFont(font, text.getFontSize()); + contentStream.showText(unicodeText); + contentStream.endText(); + } + } + // Close the content stream + contentStream.close(); + // Use a content stream to overlay the background color + try (PDPageContentStream contentStreamBg = + new PDPageContentStream( + document, + page, + PDPageContentStream.AppendMode.PREPEND, + true, + true)) { + // Set background color (e.g., light yellow) + contentStreamBg.setNonStrokingColor(Color.decode(this.backgroundColor)); + contentStreamBg.addRect( + 0, 0, page.getMediaBox().getWidth(), page.getMediaBox().getHeight()); + contentStreamBg.fill(); + } + } + // Save the modified PDF to a ByteArrayOutputStream + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + document.save(byteArrayOutputStream); + document.close(); + + // Prepare the modified PDF for download + ByteArrayInputStream inputStream = + new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + InputStreamResource resource = new InputStreamResource(inputStream); + return resource; + } + } + + private PDFont checkSupportedFontForCharacter(String unicodeText) { + + Set fonts = Standard14Fonts.getNames(); + for (String font : fonts) { + Standard14Fonts.FontName fontName = Standard14Fonts.getMappedFontName(font); + PDFont currentFont = new PDType1Font(fontName); + try { + byte[] bytes = currentFont.encode(unicodeText); + return currentFont; + } catch (IOException io) { + System.out.println("text could not be encoded "); + } catch (IllegalArgumentException ie) { + System.out.println("text not supported by font "); + } + } + return null; + } +} diff --git a/src/main/java/stirling/software/SPDF/utils/misc/HighContrastColorReplaceDecider.java b/src/main/java/stirling/software/SPDF/utils/misc/HighContrastColorReplaceDecider.java new file mode 100644 index 00000000..70d226b5 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/utils/misc/HighContrastColorReplaceDecider.java @@ -0,0 +1,30 @@ +package stirling.software.SPDF.utils.misc; + +import stirling.software.SPDF.model.api.misc.HighContrastColorCombination; +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; + +public class HighContrastColorReplaceDecider { + + // To decide the text and background colors for High contrast color option for replace-invert + // color feature + public static String[] getColors( + ReplaceAndInvert replaceAndInvert, + HighContrastColorCombination highContrastColorCombination) { + + if (highContrastColorCombination == HighContrastColorCombination.BLACK_TEXT_ON_WHITE) { + return new String[] {"0", "16777215"}; + } else if (highContrastColorCombination + == HighContrastColorCombination.GREEN_TEXT_ON_BLACK) { + return new String[] {"65280", "0"}; + } else if (highContrastColorCombination + == HighContrastColorCombination.WHITE_TEXT_ON_BLACK) { + return new String[] {"16777215", "0"}; + } else if (highContrastColorCombination + == HighContrastColorCombination.YELLOW_TEXT_ON_BLACK) { + + return new String[] {"16776960", "0"}; + } + + return null; + } +} diff --git a/src/main/java/stirling/software/SPDF/utils/misc/InvertFullColorStrategy.java b/src/main/java/stirling/software/SPDF/utils/misc/InvertFullColorStrategy.java new file mode 100644 index 00000000..12cd5204 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/utils/misc/InvertFullColorStrategy.java @@ -0,0 +1,104 @@ +package stirling.software.SPDF.utils.misc; + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; + +import javax.imageio.ImageIO; + +import org.apache.pdfbox.Loader; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; +import org.apache.pdfbox.rendering.PDFRenderer; +import org.springframework.core.io.InputStreamResource; +import org.springframework.web.multipart.MultipartFile; + +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; + +public class InvertFullColorStrategy extends ReplaceAndInvertColorStrategy { + + public InvertFullColorStrategy(MultipartFile file, ReplaceAndInvert replaceAndInvert) { + super(file, replaceAndInvert); + } + + @Override + public InputStreamResource replace() throws IOException { + + // Create a temporary file, with the original filename from the multipart file + File file = File.createTempFile("temp", getFileInput().getOriginalFilename()); + + // Transfer the content of the multipart file to the file + getFileInput().transferTo(file); + + // Load the uploaded PDF + PDDocument document = Loader.loadPDF(file); + + // Render each page and invert colors + PDFRenderer pdfRenderer = new PDFRenderer(document); + for (int page = 0; page < document.getNumberOfPages(); page++) { + BufferedImage image = + pdfRenderer.renderImageWithDPI(page, 300); // Render page at 300 DPI + + // Invert the colors + invertImageColors(image); + + // Create a new PDPage from the inverted image + PDPage pdPage = document.getPage(page); + PDImageXObject pdImage = + PDImageXObject.createFromFileByContent( + convertToBufferedImageTpFile(image), document); + + PDPageContentStream contentStream = + new PDPageContentStream( + document, pdPage, PDPageContentStream.AppendMode.OVERWRITE, true); + contentStream.drawImage( + pdImage, + 0, + 0, + pdPage.getMediaBox().getWidth(), + pdPage.getMediaBox().getHeight()); + contentStream.close(); + } + + // Save the modified PDF to a ByteArrayOutputStream + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + document.save(byteArrayOutputStream); + document.close(); + + // Prepare the modified PDF for download + ByteArrayInputStream inputStream = + new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + InputStreamResource resource = new InputStreamResource(inputStream); + return resource; + } + + // Method to invert image colors + private void invertImageColors(BufferedImage image) { + int width = image.getWidth(); + int height = image.getHeight(); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + int rgba = image.getRGB(x, y); + Color color = new Color(rgba, true); + Color invertedColor = + new Color( + 255 - color.getRed(), + 255 - color.getGreen(), + 255 - color.getBlue()); + image.setRGB(x, y, invertedColor.getRGB()); + } + } + } + + // Helper method to convert BufferedImage to InputStream + private File convertToBufferedImageTpFile(BufferedImage image) throws IOException { + File file = new File("image.png"); + ImageIO.write(image, "png", file); + return file; + } +} diff --git a/src/main/java/stirling/software/SPDF/utils/misc/PdfTextStripperCustom.java b/src/main/java/stirling/software/SPDF/utils/misc/PdfTextStripperCustom.java new file mode 100644 index 00000000..291acf5e --- /dev/null +++ b/src/main/java/stirling/software/SPDF/utils/misc/PdfTextStripperCustom.java @@ -0,0 +1,36 @@ +package stirling.software.SPDF.utils.misc; + +import java.awt.geom.Rectangle2D; +import java.io.IOException; +import java.util.List; + +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.text.PDFTextStripperByArea; +import org.apache.pdfbox.text.TextPosition; + +public class PdfTextStripperCustom extends PDFTextStripperByArea { + + /** + * Constructor. + * + * @throws IOException If there is an error loading properties. + */ + public PdfTextStripperCustom() throws IOException {} + + // To process the page text using stripper and returns the TextPosition and its values + public List> processPageCustom(PDPage page) throws IOException { + + addRegion( + "wholePage", + new Rectangle2D.Float( + page.getMediaBox().getLowerLeftX(), + page.getMediaBox().getLowerLeftY(), + page.getMediaBox().getWidth(), + page.getMediaBox().getHeight())); + extractRegions(page); + + List> textPositions = getCharactersByArticle(); + + return textPositions; + } +} diff --git a/src/main/java/stirling/software/SPDF/utils/misc/ReplaceAndInvertColorStrategy.java b/src/main/java/stirling/software/SPDF/utils/misc/ReplaceAndInvertColorStrategy.java new file mode 100644 index 00000000..87590731 --- /dev/null +++ b/src/main/java/stirling/software/SPDF/utils/misc/ReplaceAndInvertColorStrategy.java @@ -0,0 +1,24 @@ +package stirling.software.SPDF.utils.misc; + +import java.io.IOException; + +import org.springframework.core.io.InputStreamResource; +import org.springframework.web.multipart.MultipartFile; + +import lombok.Data; +import stirling.software.SPDF.model.api.PDFFile; +import stirling.software.SPDF.model.api.misc.ReplaceAndInvert; + +@Data +// @EqualsAndHashCode(callSuper = true) +public abstract class ReplaceAndInvertColorStrategy extends PDFFile { + + protected ReplaceAndInvert replaceAndInvert; + + public ReplaceAndInvertColorStrategy(MultipartFile file, ReplaceAndInvert replaceAndInvert) { + setFileInput(file); + setReplaceAndInvert(replaceAndInvert); + } + + public abstract InputStreamResource replace() throws IOException; +} diff --git a/src/main/resources/messages_en_GB.properties b/src/main/resources/messages_en_GB.properties index ecfec820..761a8568 100644 --- a/src/main/resources/messages_en_GB.properties +++ b/src/main/resources/messages_en_GB.properties @@ -480,6 +480,26 @@ home.removeImagePdf.title=Remove image home.removeImagePdf.desc=Remove image from PDF to reduce file size removeImagePdf.tags=Remove Image,Page operations,Back end,server side +#replace-invert-color +replace-color.title=Replace-Invert-Color +replace-color.header=Replace-Invert Color PDF +home.replaceColorPdf.title=Replace and Invert Color +home.replaceColorPdf.desc=Replace color for text and background in PDF and invert full color of pdf to reduce file size +replaceColorPdf.tags=Replace Color,Page operations,Back end,server side +replace-color.selectText.1=Replace or Invert color Options +replace-color.selectText.2=Default(Default high contrast colors) +replace-color.selectText.3=Custom(Customized colors) +replace-color.selectText.4=Full-Invert(Invert all colors) +replace-color.selectText.5=High contrast color options +replace-color.selectText.6=white text on black background +replace-color.selectText.7=Black text on white background +replace-color.selectText.8=Yellow text on black background +replace-color.selectText.9=Green text on black background +replace-color.selectText.10=Choose text Color +replace-color.selectText.11=Choose background Color +replace-color.submit=Replace + + ########################### # # diff --git a/src/main/resources/messages_en_US.properties b/src/main/resources/messages_en_US.properties index 027530b2..17a08ad7 100644 --- a/src/main/resources/messages_en_US.properties +++ b/src/main/resources/messages_en_US.properties @@ -480,6 +480,30 @@ home.removeImagePdf.title=Remove image home.removeImagePdf.desc=Remove image from PDF to reduce file size removeImagePdf.tags=Remove Image,Page operations,Back end,server side +#replace-invert-color +replace-color.title=Replace-Invert-Color +replace-color.header=Replace-Invert Color PDF +home.replaceColorPdf.title=Replace and Invert Color +home.replaceColorPdf.desc=Replace color for text and background in PDF and invert full color of pdf to reduce file size +replaceColorPdf.tags=Replace Color,Page operations,Back end,server side +replace-color.selectText.1=Replace or Invert color Options +replace-color.selectText.2=Default(Default high contrast colors) +replace-color.selectText.3=Custom(Customized colors) +replace-color.selectText.4=Full-Invert(Invert all colors) +replace-color.selectText.5=High contrast color options +replace-color.selectText.6=white text on black background +replace-color.selectText.7=Black text on white background +replace-color.selectText.8=Yellow text on black background +replace-color.selectText.9=Green text on black background +replace-color.selectText.10=Choose text Color +replace-color.selectText.11=Choose background Color +replace-color.submit=Replace + + + + + + ########################### # # diff --git a/src/main/resources/templates/fragments/navbar.html b/src/main/resources/templates/fragments/navbar.html index b852569d..fe0c8842 100644 --- a/src/main/resources/templates/fragments/navbar.html +++ b/src/main/resources/templates/fragments/navbar.html @@ -198,6 +198,9 @@
+
+
diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html index f8f85440..7f448f7a 100644 --- a/src/main/resources/templates/home.html +++ b/src/main/resources/templates/home.html @@ -248,11 +248,15 @@
+
+
+
+
+
-
diff --git a/src/main/resources/templates/misc/replace-color.html b/src/main/resources/templates/misc/replace-color.html new file mode 100644 index 00000000..4defcff7 --- /dev/null +++ b/src/main/resources/templates/misc/replace-color.html @@ -0,0 +1,89 @@ + + + + + + + + + +
+
+ +

+
+
+
+
+ zoom_in_map + +
+
+
+
+
+
+

+ +
+
+ + + + + +
+
+
+
+
+ +
+ + + + \ No newline at end of file