Привет всем, я хочу извлечь текст, используя текстовую стриптизершу, а затем переправить весь текст в новый PDF -документ с увеличением размера шрифта до 18. Я сталкиваюсь с проблемами, в то время как тексты получают переполнение из -за увеличения размера шрифта. Поэтому я хочу управлять этим, создавая новую страницу, когда это необходимо. Не могли бы вы помочь в этом, предоставив соответствующее решение или предложение. Спасибо < /p>
Ниже приведен код для попытки копирования текстов из источника в целевой документ. < /P>
private List textInfo ;
public PDFLargePrintServiceImpl() throws IOException {
super();
textInfo = new ArrayList();
}
@Override
public void testCopyFromTestDocumentSigned(MultipartFile fileContent) throws IOException {
try (
PDDocument source = PDDocument.load(fileContent.getInputStream());
PDDocument target = new PDDocument();
) {
copyText(source, target);
target.save(new File(\output.pdf"));
target.close();
}
}
void copyText(PDDocument source, PDDocument target) throws IOException {
for (int i = 0; i < source.getNumberOfPages(); i++) {
PDPage sourcePage = source.getPage(i);
PDPage targetPage = null;
if (i < target.getNumberOfPages())
targetPage = target.getPage(i);
else
target.addPage(targetPage = new PDPage(sourcePage.getMediaBox()));
copyText(source, i, target, targetPage, textInfo);
}
}
void copyText(PDDocument source, int sourcePageNumber, PDDocument target, PDPage targetPage, List textInfo) throws IOException {
List allTextPositions = new ArrayList();
PDFTextStripper pdfTextStripper = new PDFTextStripper() {
@Override
protected void writeString(String text, List textPositions) throws IOException {
allTextPositions.addAll(textPositions);
super.writeString(text, textPositions);
}
};
pdfTextStripper.setStartPage(sourcePageNumber + 1);
pdfTextStripper.setEndPage(sourcePageNumber + 1);
pdfTextStripper.getText(source);
if (textInfo != null)
textInfo.addAll(textInfo);
PDRectangle targetPageCropBox = targetPage.getCropBox();
float yOffset = targetPageCropBox.getUpperRightY() + targetPageCropBox.getLowerLeftY();
float fontSize = 18f;
try (PDPageContentStream contentStream = new PDPageContentStream(target, targetPage, AppendMode.APPEND, true, true)) {
contentStream.beginText();
float x = 0;
float y = yOffset;
for (TextPosition position: allTextPositions) {
contentStream.setFont(position.getFont(), fontSize);
contentStream.newLineAtOffset(position.getX() - x, - (position.getY() - y));
contentStream.showText(position.getUnicode());
x = position.getX();
y = position.getY();
}
contentStream.endText();
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... nto-target
Как избежать переполнения текста во время копирования текста из исходного документа в целевой новый документ? ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение