我尝试使用库PDFBox编辑文本而不是没有。我不知道如何获取单个文本对象的流,因此我可以编辑文本和/或颜色。
任何想法,例如? 感谢
答案 0 :(得分:1)
我发现pdf中的编辑文本不可靠,因此请尝试使用矩形(白色/背景颜色填充)清除文本,并将新文本写入清除位置。这是一个示例代码。
//to add a link in footer
//to replace a text
//to replace a link/url/href
public static void editTextorUrl(String inputFile, String outputFile)
throws IOException, COSVisitorException {
// the document
PDDocument doc = null;
try {
System.out.println(inputFile);
doc = PDDocument.load(inputFile);
List pages = doc.getDocumentCatalog().getAllPages();
for (int i = 0; i < pages.size(); i++) {
float inch = 72;
PDGamma colourRed = new PDGamma();
colourRed.setR(1);
PDGamma colourBlue = new PDGamma();
colourBlue.setB(1);
PDGamma white = new PDGamma();
white.setR(1);
white.setB(1);
white.setG(1);
PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary();
borderThick.setWidth(inch / 12); // 12th inch
PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary();
borderThin.setWidth(inch / 72); // 1 point
PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary();
borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE);
borderULine.setWidth(inch / 72); // 1 point
PDPage page = (PDPage) pages.get(i);
PDFont font = PDType1Font.HELVETICA;
PDPageContentStream contentStream = new PDPageContentStream(
doc, page, true, false);
contentStream.setNonStrokingColor(Color.WHITE);
contentStream.fillRect(55, 27, 144, 17);
contentStream.setNonStrokingColor(Color.BLUE);
contentStream.beginText();
contentStream.setFont(font, 11);
contentStream.moveTextPositionByAmount(55, 37);
contentStream.drawString("www.loasoftwares.com"); //text to be replaced
contentStream.endText();
contentStream.setLineWidth(inch / 300);
contentStream.setStrokingColor(Color.BLUE);
contentStream.drawLine(55, 34, 188, 34);
contentStream.close();
PDAnnotationLink txtLink = new PDAnnotationLink();
PDRectangle position = new PDRectangle();
position.setLowerLeftX(55);
position.setLowerLeftY(27);
position.setUpperRightX(188);
position.setUpperRightY(50);
txtLink.setRectangle(position);
// add an action
PDActionURI action = new PDActionURI();
action.setURI("www.loasoftwares.com");
txtLink.setBorderStyle(borderULine);
txtLink.setAction(action);
txtLink.setColour(white);
page.getAnnotations().add(txtLink);
}
doc.save(outputFile);
} finally {
if (doc != null) {
doc.close();
}
}
}
答案 1 :(得分:0)
备注:在特定情况下有效(ASCII字体字体加上相当长的字符串参数)
/ ** *此示例将用新的PDF替换字符串。 * *该示例摘自pdf文件格式规范。 * * @作者本·利奇菲尔德 * @版本$修订:1.3 $ * /
ffmpeg -i source.mp4 -vf drawtext=\"text_shaping=1:fontfile=font.ttf:
text='یه نوشته فارسی!': fontcolor=black: fontsize=$font_size: box=1: boxcolor=black@0:boxborderw=0: x=(w-text_w)/2: y=(h-text_h)/2 :enable='between(t,5,10)'\" -c:a copy -force_key_frames 0:05:00,0:6:00 end.mp4