Skip to content

Commit

Permalink
Add hidden text encoding latex within pdf document
Browse files Browse the repository at this point in the history
  • Loading branch information
moritzfl committed Mar 3, 2019
1 parent b72db4c commit f877279
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.moritzfl</groupId>
<artifactId>mathematicallatexhelper</artifactId>
<version>2.2.0.0</version>
<version>2.2.1.0</version>
<name>mathematicallatexhelper</name>
<prerequisites>
<maven>3</maven>
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/de/moritzf/latexhelper/Export.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


import java.awt.*;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
Expand All @@ -32,14 +33,17 @@
import javax.swing.filechooser.FileSystemView;


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.*;

import de.moritzf.latexhelper.util.SteganographyUtil;
import gutenberg.itext.Emitter;
import gutenberg.itext.ITextContext;
import gutenberg.itext.PygmentsAdapter;
import gutenberg.itext.Styles;
import gutenberg.itext.emitter.RichTextEmitter;
import gutenberg.itext.emitter.SourceCodeLaTeXExtension;
import gutenberg.itext.model.Markdown;
import gutenberg.itext.model.RichText;
import gutenberg.itext.model.SourceCode;
import gutenberg.pygments.Pygments;
import gutenberg.pygments.styles.DefaultStyle;
Expand Down Expand Up @@ -161,8 +165,17 @@ private static void generatePdf(String expression, File file) throws IOException
SourceCodeLaTeXExtension extension = new SourceCodeLaTeXExtension(pygmentsAdapter);
SourceCode sourceCode = new SourceCode("latex", expression);

com.itextpdf.text.Font font = new com.itextpdf.text.Font();
font.setColor(BaseColor.WHITE);
font.setSize(0.001f);
Chunk chunk = new Chunk("\\##latex##\\" + expression + "\\##latex##\\");
chunk.setFont(font);

document.add(chunk);

extension.emit(sourceCode, iTextContext);
iTextContext.getDocument().addHeader("latex", expression);
document.addHeader("latex", expression);

iTextContext.close();
} catch (DocumentException e) {

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/de/moritzf/latexhelper/LatexImportingTextArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import de.moritzf.latexhelper.util.ImageFileUtil;
import de.moritzf.latexhelper.util.SteganographyUtil;
import mathpix.MathPix;
Expand Down Expand Up @@ -30,6 +31,8 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Text area that can work with images that have content hidden by steganography. If such content is detected, it
Expand Down Expand Up @@ -124,6 +127,16 @@ private String extractFromPdf(File pdfFile) {
try {
reader = new PdfReader(pdfFile.getAbsolutePath());
latex = reader.getInfo().get("latex");
if (latex == null || latex.isEmpty()) {
String text = PdfTextExtractor.getTextFromPage(reader, 1);
LOGGER.log(Level.INFO, text);
Pattern pattern = Pattern.compile("\\\\##latex##\\\\(?<latex>.*)\\\\##latex##\\\\");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
latex = matcher.group("latex");
}

}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not read pdf. Perhaps the file used was not a valid pdf file.");
}
Expand Down

0 comments on commit f877279

Please sign in to comment.