-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
484 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ririv/quickoutline/pdfProcess/PageLabel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ririv.quickoutline.pdfProcess; | ||
|
||
|
||
//https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=383 | ||
//根据PDF标准 | ||
//与iText的PageLabel对应 | ||
public record PageLabel(int pageNum, PageLabelNumberingStyle numberingStyle, String labelPrefix, Integer firstPage){ | ||
|
||
public enum PageLabelNumberingStyle{ | ||
/** | ||
* 1, 2, 3, 4... | ||
*/ | ||
DECIMAL_ARABIC_NUMERALS, | ||
/** | ||
* I, II, III, IV... | ||
*/ | ||
UPPERCASE_ROMAN_NUMERALS, | ||
/** | ||
* i, ii, iii, iv... | ||
*/ | ||
LOWERCASE_ROMAN_NUMERALS, | ||
/** | ||
* A, B, C, D... | ||
*/ | ||
UPPERCASE_LETTERS, | ||
/** | ||
* a, b, c, d... | ||
*/ | ||
LOWERCASE_LETTERS | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ririv/quickoutline/pdfProcess/PageLabelSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ririv.quickoutline.pdfProcess; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public interface PageLabelSetter<T> { | ||
|
||
void setPageLabels(String srcFilePath, String destFilePath, List<PageLabel> labelList) throws IOException; | ||
|
||
T mapPageLabelNumberingStyle(PageLabel.PageLabelNumberingStyle numberingStyle); | ||
} |
2 changes: 1 addition & 1 deletion
2
...koutline/pdfProcess/PdfViewScaleType.java → ...uickoutline/pdfProcess/ViewScaleType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 53 additions & 85 deletions
138
src/main/java/com/ririv/quickoutline/pdfProcess/itextImpl/ItextPageLabelSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,74 @@ | ||
package com.ririv.quickoutline.pdfProcess.itextImpl; | ||
|
||
import com.itextpdf.io.font.constants.StandardFonts; | ||
import com.itextpdf.kernel.font.PdfFont; | ||
import com.itextpdf.kernel.font.PdfFontFactory; | ||
import com.itextpdf.kernel.geom.PageSize; | ||
import com.itextpdf.kernel.geom.Rectangle; | ||
import com.itextpdf.kernel.pdf.*; | ||
import com.itextpdf.kernel.pdf.action.PdfAction; | ||
import com.itextpdf.kernel.pdf.annot.PdfAnnotation; | ||
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation; | ||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas; | ||
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; | ||
import com.itextpdf.layout.Document; | ||
import com.itextpdf.layout.element.AreaBreak; | ||
import com.itextpdf.layout.element.Link; | ||
import com.itextpdf.layout.element.Paragraph; | ||
import com.itextpdf.kernel.pdf.PageLabelNumberingStyle; | ||
import com.itextpdf.kernel.pdf.PdfDocument; | ||
import com.itextpdf.kernel.pdf.PdfReader; | ||
import com.itextpdf.kernel.pdf.PdfWriter; | ||
import com.ririv.quickoutline.pdfProcess.PageLabel; | ||
import com.ririv.quickoutline.pdfProcess.PageLabelSetter; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ItextPageLabelSetter { | ||
|
||
|
||
public static final String DEST = "./target/sandbox/objects/page_labels.pdf"; | ||
// https://kb.itextpdf.com/itext/page-labels | ||
public class ItextPageLabelSetter implements PageLabelSetter<PageLabelNumberingStyle> { | ||
|
||
public static void main(String[] args) throws IOException { | ||
final String SRC = ""; | ||
final String DEST = "./target/sandbox/objects/page_labels.pdf"; | ||
|
||
List<PageLabel> labelList = new ArrayList<>(); | ||
labelList.add(new PageLabel(1, PageLabel.PageLabelNumberingStyle.UPPERCASE_LETTERS,null,null)); | ||
File file = new File(DEST); | ||
file.getParentFile().mkdirs(); | ||
new ItextPageLabelSetter().setPdfLabels(DEST); | ||
new ItextPageLabelSetter().setPageLabels(SRC, DEST, labelList); | ||
} | ||
|
||
protected void setPdfLabels(String dest) throws IOException { | ||
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); | ||
Document doc = new Document(pdfDoc); | ||
|
||
PdfViewerPreferences viewerPreferences = new PdfViewerPreferences(); | ||
viewerPreferences.setPrintScaling(PdfViewerPreferences.PdfViewerPreferencesConstants.NONE); | ||
pdfDoc.getCatalog().setPageMode(PdfName.UseThumbs); | ||
pdfDoc.getCatalog().setPageLayout(PdfName.TwoPageLeft); | ||
pdfDoc.getCatalog().setViewerPreferences(viewerPreferences); | ||
|
||
doc.add(new Paragraph("Hello World")); | ||
doc.add(new Paragraph("Hello People")); | ||
|
||
doc.add(new AreaBreak()); | ||
PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA); | ||
public void setPageLabels(String srcFilePath, String destFilePath, List<PageLabel> labelList) throws IOException { | ||
PdfDocument pdfDoc = new PdfDocument(new PdfReader(srcFilePath), new PdfWriter(destFilePath)); | ||
|
||
// Add the text to the direct content, but not in the right order | ||
PdfCanvas canvas = new PdfCanvas(pdfDoc.getPage(2)); | ||
canvas.beginText(); | ||
canvas.setFontAndSize(font, 12); | ||
canvas.moveText(88.66f, 788); | ||
canvas.showText("ld"); | ||
canvas.moveText(-22f, 0); | ||
canvas.showText("Wor"); | ||
canvas.moveText(-15.33f, 0); | ||
canvas.showText("llo"); | ||
canvas.moveText(-15.33f, 0); | ||
canvas.showText("He"); | ||
canvas.endText(); | ||
PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(250, 25)); | ||
new PdfCanvas(formXObject, pdfDoc).beginText() | ||
.setFontAndSize(font, 12) | ||
.moveText(0, 7) | ||
.showText("Hello People") | ||
.endText(); | ||
canvas.addXObjectAt(formXObject, 36, 763); | ||
for(PageLabel label: labelList){ | ||
int pageNum = label.pageNum(); | ||
PageLabelNumberingStyle numberingStyle = mapPageLabelNumberingStyle(label.numberingStyle()); | ||
|
||
pdfDoc.setDefaultPageSize(new PageSize(PageSize.A4).rotate()); | ||
doc.add(new AreaBreak()); | ||
doc.add(new Paragraph("Hello World")); | ||
String labelPrefix = label.labelPrefix(); | ||
Integer firstPage = label.firstPage(); | ||
|
||
pdfDoc.setDefaultPageSize(new PageSize(842, 595)); | ||
doc.add(new AreaBreak()); | ||
doc.add(new Paragraph("Hello World")); | ||
if (label.firstPage() ==null){ | ||
pdfDoc.getPage(pageNum).setPageLabel(numberingStyle, labelPrefix); | ||
} else { | ||
pdfDoc.getPage(pageNum).setPageLabel(numberingStyle, labelPrefix, firstPage); | ||
} | ||
} | ||
|
||
pdfDoc.setDefaultPageSize(PageSize.A4); | ||
doc.add(new AreaBreak()); | ||
pdfDoc.getLastPage().setCropBox(new Rectangle(10, 70, 525, 755)); | ||
doc.add(new Paragraph("Hello World")); | ||
|
||
doc.add(new AreaBreak()); | ||
pdfDoc.getLastPage().getPdfObject().put(PdfName.UserUnit, new PdfNumber(5)); | ||
doc.add(new Paragraph("Hello World")); | ||
|
||
doc.add(new AreaBreak()); | ||
pdfDoc.getLastPage().setArtBox(new Rectangle(36, 36, 523, 770)); | ||
Paragraph p = new Paragraph("Hello ") | ||
.add(new Link("World", PdfAction.createURI("http://maps.google.com"))); | ||
doc.add(p); | ||
PdfAnnotation a = new PdfTextAnnotation( | ||
new Rectangle(36, 755, 30, 30)) | ||
.setTitle(new PdfString("Example")) | ||
.setContents("This is a post-it annotation"); | ||
pdfDoc.getLastPage().addAnnotation(a); | ||
|
||
|
||
pdfDoc.getPage(1).setPageLabel(PageLabelNumberingStyle.UPPERCASE_LETTERS, null); | ||
pdfDoc.getPage(3).setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, null); | ||
pdfDoc.getPage(4).setPageLabel(PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS, "Custom-", 2); | ||
} | ||
|
||
doc.close(); | ||
@Override | ||
public PageLabelNumberingStyle mapPageLabelNumberingStyle(PageLabel.PageLabelNumberingStyle numberingStyle) { | ||
|
||
switch (numberingStyle) { | ||
case PageLabel.PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS -> { | ||
return PageLabelNumberingStyle.DECIMAL_ARABIC_NUMERALS; | ||
} | ||
case PageLabel.PageLabelNumberingStyle.LOWERCASE_ROMAN_NUMERALS -> { | ||
return PageLabelNumberingStyle.LOWERCASE_ROMAN_NUMERALS; | ||
} | ||
case PageLabel.PageLabelNumberingStyle.UPPERCASE_ROMAN_NUMERALS-> { | ||
return PageLabelNumberingStyle.UPPERCASE_ROMAN_NUMERALS; | ||
} | ||
case PageLabel.PageLabelNumberingStyle.LOWERCASE_LETTERS-> { | ||
return PageLabelNumberingStyle.LOWERCASE_LETTERS; | ||
} | ||
case PageLabel.PageLabelNumberingStyle.UPPERCASE_LETTERS-> { | ||
return PageLabelNumberingStyle.UPPERCASE_LETTERS; | ||
} | ||
case null -> { | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/ririv/quickoutline/service/PdfLabelService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ririv.quickoutline.service; | ||
|
||
import com.itextpdf.kernel.pdf.PageLabelNumberingStyle; | ||
import com.ririv.quickoutline.pdfProcess.PageLabel; | ||
import com.ririv.quickoutline.pdfProcess.PageLabelSetter; | ||
import com.ririv.quickoutline.pdfProcess.itextImpl.ItextPageLabelSetter; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class PdfLabelService { | ||
private final PageLabelSetter<PageLabelNumberingStyle> pageLabelSetter = new ItextPageLabelSetter(); | ||
|
||
public void setPageLabels(String srcFilePath, String destFilePath, List<PageLabel> labelList) throws IOException{ | ||
pageLabelSetter.setPageLabels(srcFilePath, destFilePath, labelList); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.