我有这个java代码:
try {
PDFTextStripper pdfs = new PDFTextStripper();
String textOfPDF = pdfs.getText(PDDocument.load("doc"));
doc.add(new Field(campo.getDestino(),
textOfPDF,
Field.Store.NO,
Field.Index.ANALYZED));
} catch (Exception exep) {
System.out.println(exep);
System.out.println("PDF fail");
}
Run Code Online (Sandbox Code Playgroud)
抛出这个:
11:45:07,017 WARN [COSDocument] Warning: You did not close a PDF Document
Run Code Online (Sandbox Code Playgroud)
而且我不知道为什么要扔掉这个1,2,3或更多.
我发现COSDocument是一个类并且有close()方法,但是我没有使用这个类.
我有这个进口:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
Run Code Online (Sandbox Code Playgroud)
谢谢 :)
我必须用表制作PDF.到目前为止它工作正常,但现在我想添加一个包装功能.所以我需要插入换行符.
contentStream.beginText();
contentStream.moveTextPositionByAmount(x, y);
contentStream.drawString("Some text to insert into a table.");
contentStream.endText();
Run Code Online (Sandbox Code Playgroud)
我想\n在"插入"之前添加一个" ".我尝试了" \u000A"这是换行的十六进制值,但Eclipse向我显示错误.
是否可以使用drawString添加换行符?
我正在使用PDFBox从现有的PDF模板构建文档,因此它会打开文件,向其中添加文本并保存.除了尝试使用外部TTF字体外,它运行良好.我尝试了不同的东西,并搜索了2天的解决方案,但在PDFBox上没有太多.
这是一些代码,使用字体"Tardy Kid",因为它不能被误认为是其他任何东西,并且不太可能是任何标准库的一部分.
代码执行正常,从println显示"TardyKid"(显示字体已加载且名称可获取),并显示文本 - 但它在Helvetica中.getStringWidth()用于计算宽度的代码的更复杂部分似乎也表示宽度表的成功加载.它只是没有正确显示.
代码在较大程序的上下文中运行,该程序打开现有PDF文档(模板)并向其添加文本.这一切似乎都很好,除了
public void setText ( PDDocument document, String text ) throws IOException {
int lastPage = document.getNumberOfPages() - 1;
PDPage page = (PDPage) document.getDocumentCatalog().getAllPages().get(lastPage);
PDPageContentStream contentStream = null;
try {
contentStream = new PDPageContentStream(document,page,true,true,false);
File fontFile = new File(m_fontDir, "Tardy_Kid.ttf");
PDFont font = PDTrueTypeFont.loadTTF(document, fontFile);
Color color = new Color(196, 18, 47);
float x = 100f, y = 700f;
System.out.println(font.getBaseFont());
contentStream.setFont(font, 32);
contentStream.setNonStrokingColor(color);
contentStream.beginText();
contentStream.moveTextPositionByAmount(x,y);
contentStream.drawString(text);
contentStream.endText();
} finally { …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache pdfbox来提取文本.我可以从pdf中提取文本,但我不知道如何知道这个词是否是粗体??? (代码建议会很好!!!)这是从pdf中提取纯文本的代码.
PDDocument document = PDDocument
.load("/home/lipu/workspace/MRCPTester/test.pdf");
document.getClass();
if (document.isEncrypted()) {
try {
document.decrypt("");
} catch (InvalidPasswordException e) {
System.err.println("Error: Document is encrypted with a password.");
System.exit(1);
}
}
// PDFTextStripperByArea stripper = new PDFTextStripperByArea();
// stripper.setSortByPosition(true);
PDFTextStripper stripper = new PDFTextStripper();
stripper.setStartPage(1);
stripper.setEndPage(2);
stripper.setSortByPosition(true);
String st = stripper.getText(document);
Run Code Online (Sandbox Code Playgroud) 情况:
在PDFBox中,PDRectangle对象的默认原点(0,0)似乎是页面的左下角.
例如,以下代码在页面的左下角为您提供了一个正方形,每边长度为100个单位.
PDRectangle rectangle = new PDRectangle(0, 0, 100, 100);
Run Code Online (Sandbox Code Playgroud)
问题:
是否可以将原点更改为UPPER-LEFT转角,例如,上面的代码会在页面的左上角为您提供相同的方块?
我问的原因
是:我使用PDFTextStripper来获取文本的坐标(通过使用提取的TextPosition对象的getX()和getY()方法).从TextPosition对象检索的坐标似乎在UPPER-LEFT CORNER处具有原点(0,0).我希望我的PDRectangle对象的坐标与我的TextPosition对象的坐标具有相同的原点.
我试图通过"页面高度减去Y坐标"来调整PDRectangle的Y坐标.这给了我想要的结果,但它并不优雅.我想要一个优雅的解决方案
注意:有人问过类似的问题.答案就是我尝试过的,这不是最优雅的. 如何从左下角到左上角更改pdf页面中文本的坐标
我有一个pdf表单,我正在尝试使用pdfBox填写表单并打印文档.我让它适用于1页打印作业,但我不得不尝试修改多个页面.基本上它是一个基本信息顶部和内容列表的表单.好吧,如果内容大于表单有空间,我必须使它成为多页文档.我最终获得了一个带有漂亮页面的文档,然后所有剩余的页面都是空白模板.我究竟做错了什么?
PDDocument finalDoc = new PDDocument();
File template = new File("path/to/template.pdf");
//Declare basic info to be put on every page
String name = "John Smith";
String phoneNum = "555-555-5555";
//Get list of contents for each page
List<List<Map<String, String>>> pageContents = methodThatReturnsMyInfo();
for (List<Map<String, String>> content : pageContents) {
PDDocument doc = new PDDocument().load(template);
PDDocumentCatlog docCatalog = doc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
acroForm.getField("name").setValue(name);
acroForm.getField("phoneNum").setValue(phoneNum);
for (int i=0; i<content.size(); i++) {
acroForm.getField("qty"+i).setValue(content.get(i).get("qty"));
acroForm.getField("desc"+i).setValue(content.get(i).get("desc"));
}
List<PDPage> pages = docCatalog.getAllPages();
finalDoc.addPage(pages.get(0));
}
//Then prints/saves …Run Code Online (Sandbox Code Playgroud) 我想在PDFBOX中创建一个按钮,即验证或重置按钮,该按钮将调用PDF中嵌入式javascript的某些功能.
如何在PDFBOX中创建这样的按钮?
我尝试使用PDPushButton片段跟随代码,但它现在正常工作.在这里,当我点击按钮区域时,会显示勾选标记符号并在每次点击时切换.边框也没有显示出来.相反,我想显示标签和边框周围的普通按钮.
我使用的是pdfbox版本1.8.10.
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
doc.addPage(page);
PDAcroForm acroForm = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm);
PDActionJavaScript javascript = new PDActionJavaScript("function validate(index){ app.alert(index); }");
doc.getDocumentCatalog().setOpenAction( javascript );
COSDictionary cosDict = new COSDictionary();
COSArray rect = new COSArray();
rect.add(new COSFloat(100));
rect.add(new COSFloat(10));
rect.add(new COSFloat(200));
rect.add(new COSFloat(60));
cosDict.setItem(COSName.RECT, rect);
cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
cosDict.setItem(COSName.TYPE, COSName.ANNOT);
cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
cosDict.setItem(COSName.T, new COSString("My Btn"));
cosDict.setItem(COSName.V, new COSString("Validate"));
cosDict.setItem(COSName.DA, new COSString("/Helv 7 Tf 0 g"));
PDPushButton button = new PDPushButton(acroForm, …Run Code Online (Sandbox Code Playgroud) 目前我有一个客户端 - 服务器应用程序,给定一个 PDF 文件,对其进行签名(使用服务器证书),将签名附加到原始文件并将输出返回给客户端(所有这些都是通过 PDFBox 实现的)。
我有一个签名处理程序,这是我的外部签名支持(其中内容是 PDF 文件)
public byte[] sign(InputStream content) throws IOException {
try {
System.out.println("Generating CMS signed data");
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
ContentSigner sha1Signer = new JcaContentSignerBuilder("Sha1WithRSA").build(privateKey);
generator.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build())
.build(sha1Signer, new X509CertificateHolder(certificate.getEncoded())));
CMSTypedData cmsData = new CMSProcessableByteArray(IOUtils.toByteArray(content));
CMSSignedData signedData = generator.generate(cmsData, false);
return signedData.getEncoded();
} catch (GeneralSecurityException e) {
throw new IOException(e);
} catch (CMSException e) {
throw new IOException(e);
} catch (OperatorCreationException e) {
throw new IOException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我在想 - …
我终于成功地让 PDFBox 打印了我的 unicodes。但是现在,我想了解我提出的解决方案。下面的代码有效并将 a 打印?到页面。
两件事不起作用:
更改
PDType0Font.load(documentMock, systemResourceAsStream, true);
为
PDType0Font.load(documentMock, systemResourceAsStream, false);
更改
final PDFont robotoLight = loadFontAlternative("Roboto-Light.ttf");
为
final PDFont robotoLight = loadFont("Roboto-Light.ttf");
第一个更改打印两个点而不是字符。 embedSubset 有什么作用,因为它在设置为 false 时不起作用? 文档太少,我无法理解。
第二个更改提供了以下异常Exception in thread "main" java.lang.IllegalArgumentException: U+2265 is not available in this font's encoding: WinAnsiEncoding
此问题已在许多其他问题中涵盖,这些问题早于 PDFBox 2.0,其中在处理 unicode 时存在错误。所以,他们不直接回答这个问题。除此之外,问题很明显:我不应该将编码设置为 WinAnsiEncoding 而是不同的东西。
但是编码应该是什么?而为什么没有UTF-8编码或类似的可用?
COSName 中没有关于许多选项的文档。
public class SimpleReportUnicode {
public static void main(String[] args) throws IOException {
PDDocument report = createReport();
final String …Run Code Online (Sandbox Code Playgroud) 我想在 C# 中使用 Apache 的 PDFBox 库,我检查了 NuGet 但遗憾的是,那里没有 PDFBox 2.0。
我在互联网上搜索了在 .Net 上使用 Java 库的方法,发现一些旧文章(2011-2014)说我应该使用 IKVM,并发现一些较新的文章说 IKVM 不再更新并且它不起作用不再。
有没有什么可行的方法可以使 PDFBox 2.0 库与 C# 一起使用?
如果我的问题听起来很愚蠢,我深表歉意,但 2 小时前我不知道我们可以将 Java 库转换为 .NET,哈哈。