我在使用 PDFBox 以 Java 编写的一些代码时遇到问题。我正在尝试根据从 Excel 电子表格读取的值使用特定表单填充 PDF。下面是我的班级文件。
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.hssf.usermodel.*;
/**
* This is a test file for reading and populating a PDF with specific forms
*/
public class JU_TestFile {
PDPage Stick_Form;
PDPage IKE_Form;
PDPage BO_Form;
/**
* Constructor.
*/
public JU_TestFile() throws IOException
{
this.BO_Form = (PDPage) PDDocument.load(new File("C:\\Users\\saf\\Desktop\\JavaTest\\BO Pole Form.pdf")).getPage(0);
this.IKE_Form = (PDPage) PDDocument.load(new File("C:\\Users\\saf\\Desktop\\JavaTest\\IKE …Run Code Online (Sandbox Code Playgroud) 我正在使用 pdfbox 2.0.5 使用以下代码填写 PDF 文档的表单字段:
doc = PDDocument.load(inputStream);
PDDocumentCatalog catalog = doc.getDocumentCatalog();
PDAcroForm form = catalog.getAcroForm();
for (PDField field : form.getFieldTree()){
field.setValue("??????");
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:U+0434 ('afii10069') 在此字体中不可用 Times-Roman (generic: TimesNewRomanPSMT) encoding: StandardEncoding with Difference
PDF 文档本身包含显示正常的西里尔文文本。我试过使用不同的字体。对于“Arial Unicode MS”,它需要下载一个 50MB 的“Adobe Acrobat Reader DC 字体包”。这是对西里尔字符的要求吗?
我必须在文本字段中指定哪种字体来处理西里尔文(或亚洲)字符?
谢谢,罗波
我正在尝试使用 PDFBox 从 pdf 文件中提取带有坐标的文本。
我混合了在互联网上找到的一些方法/信息(也是stackoverflow),但是我的坐标问题似乎不正确。例如,当我尝试使用坐标在 tex 顶部绘制矩形时,该矩形会在其他地方绘制。
这是我的代码(请不要判断风格,写得很快只是为了测试)
文本行.java
import java.util.List;
import org.apache.pdfbox.text.TextPosition;
/**
*
* @author samue
*/
public class TextLine {
public List<TextPosition> textPositions = null;
public String text = "";
}
Run Code Online (Sandbox Code Playgroud)
myStripper.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the …Run Code Online (Sandbox Code Playgroud) 我刚刚从 PdfBox 1.8 过渡到 2.0.0 并且有相当大的差异。在现有的 pdf 页面上写文本之前,我使用了 drawString。在 2.0.0 中,不推荐使用绘制字符串,但 showText 在块文本中不起作用。
我在 1.8 中的代码:
contentStream.beginText()
contentStream.moveTextPositionByAmount(250, 665)
contentStream.drawString("1 2 3 4 5 6 7 8 9 1 0")
contentStream.endText()
Run Code Online (Sandbox Code Playgroud)
我在 2.0 中的代码
PDDocument newPdf=null
newPdf=PDDocument.load(sourcePdfFile)
PDPage firstPage=newPdf.getPage(0)
PDPageContentStream contentStream = new PDPageContentStream(newPdf, firstPage, PDPageContentStream.AppendMode.APPEND,true,true)
contentStream.setFont(pdfFont, fontSize)
contentStream.beginText()
contentStream.lineTo(200,685)
contentStream.showText("John")
contentStream.endText()
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
任何人都知道我如何像 1.8 一样编写文本
我正在尝试使用 PDFBox 2.0.8 填写一堆 PDF 表单。对于某些文档,在设置 PDTextField 的值时出现以下错误:
java.io.IOException: Could not find font: /ArialMT
Run Code Online (Sandbox Code Playgroud)
显然,该字体未正确嵌入,就像 Microsoft 专有字体经常出现的情况一样。
我如何告诉 PDFBox 用“普通”Arial 或其他字体替换字体?将字段 DA 字符串设置为"/Helv 0 tf 0 g"导致 NullPointerException。
有人可以帮我解决这个例外吗?这是否意味着我必须使用 1.5 的 Java 版本运行 keytool?
我以前有过它,但现在我明白了。
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.itextpdf.text.pdf.security.CertificateInfo.getSubjectFields(CertificateInfo.java:356)
at com.itextpdf.text.pdf.PdfSignatureAppearance.getAppearance(PdfSignatureAppearance.java:884)
at com.itextpdf.text.pdf.PdfSignatureAppearance.preClose(PdfSignatureAppearance.java:1268)
at com.itextpdf.text.pdf.security.MakeSignature.signDetached(MakeSignature.java:140)
at org.deloitte.cms.efi.controller.signature.SignatureAppearance.sign4(SignatureAppearance.java:181)
at org.deloitte.cms.efi.controller.signature.SignatureAppearance.main(SignatureAppearance.java:208)
Run Code Online (Sandbox Code Playgroud)
引起:java.lang.SecurityException:类“org.bouncycastle.asn1.ASN1ObjectIdentifier”的签名者信息与同一包中其他类的签名者信息不匹配
这是我的代码:
public void sign4(String src, String name, String dest,
Certificate[] chain, PrivateKey pk,
String digestAlgorithm, String provider, CryptoStandard subfilter,
String reason, String location)
throws GeneralSecurityException, IOException, DocumentException {
// Creating the reader and the stamper
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
// Creating the appearance …Run Code Online (Sandbox Code Playgroud) 在对 PDDocument 进行数字签名后,我关闭了 Apache PDFBox 中的 PDDocument。我收到警告:You did not close PDF Document当我关闭我的实例时。只有一个地方创建了 PDDocument 并且它被正确关闭。
代码 :
private byte[] buildDocument(File pdfToSign, PDVisibleSigProperties visibleSigProperties) throws Exception
{
FileOutputStream fos = null;
PDDocument doc = null;
try
{
String signedPdfName = pdfToSign.getName().substring(0, pdfToSign.getName().indexOf("."));
File signedFile = File.createTempFile(signedPdfName + "_signed", null);
signedFile.deleteOnExit();
fos = new FileOutputStream(signedFile);
doc = PDDocument.load(pdfToSign);
// create signature dictionary
PDSignature signature = new PDSignature();
signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE);
// subfilter for basic and PAdES Part 2 signatures
signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
signature.setName(visibleSigProperties.getSignerName());
signature.setLocation(visibleSigProperties.getSignerLocation());
signature.setReason(visibleSigProperties.getSignatureReason()); …Run Code Online (Sandbox Code Playgroud) 我有一个关于 Apache PDFBox 的问题。是否可以将 pdf 文件拆分为具有不同页面范围和文件名的不同文件?
例子:
我想使用 PDFBox API 添加文本到 PDF 并将其旋转 45 度并将其放置在页面的中心,文本是动态的,应该始终放置在中心,除了居中部分之外,其他所有功能都可以工作,我将不胜感激任何帮助。我有这个代码:
Point2D.Float pageCenter = getCenter(page);
float stringWidth = getStringWidth(watermarkText, font, fontSize);
float textX = pageCenter.x - stringWidth / 2F + center.x;
System.out.println(textX);
float textY = pageCenter.y + center.y;
//System.out.println("Inside cross"+textX+", "+textY);
fontSize = 110.0f;
cs.transform(Matrix.getRotateInstance(Math.toRadians(45), textX, textY));
cs.moveTo(0, 0);
cs.lineTo(125, 0);
r0.setNonStrokingAlphaConstant(0.20f);
Run Code Online (Sandbox Code Playgroud)
这就是我想要的结果: 输出PDF
我正在遵循在线教程。我正在使用 Eclipse 中的 PDFBox,并且在加载库中的字体时遇到问题。我已将所有 9 个 jar 加载到我的外部库中,不同的字体给了我相同的“无法解析或不是字段”。
import java.io.IOException;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.font.PDFont;
public class PCSRR {
public static void main(String[] args)throws IOException {
PDDocument doc = new PDDocument();
PDPage blankPage = new PDPage();
doc.addPage( blankPage );
PDPage page = doc.getPage(1);
PDPageContentStream contentStream = new PDPageContentStream(doc, page);
//Begin the Content stream
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA , 14);
contentStream.newLineAtOffset(20, 450);
String text = "Hi!!! This is the first sample PDF document.";
contentStream.showText(text);
contentStream.endText();
System.out.println("New Text Content is added in the …Run Code Online (Sandbox Code Playgroud) 我刚开始使用 Tess4J 库。我已将 Tess4j 作为依赖项包含在 Maven 中,如下所示:
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.5.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
到目前为止,它运行得很好,但我无法弄清楚如何禁用日志记录。看来日志来自 PDFBox 和 Fontbox,它们是 Tess4j 的依赖项。以下是此处记录的一些内容:
19:54:46.712 [main] DEBUG org.apache.fontbox.util.autodetect.FontFileFinder - checkFontfile found C:\WINDOWS\FONTS\YuGothR.ttc
19:54:46.757 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-BoldMT from C:\WINDOWS\FONTS\arialbd.ttf
19:54:46.860 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-BoldItalicMT from C:\WINDOWS\FONTS\arialbi.ttf
19:54:46.899 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded ArialMT from C:\WINDOWS\FONTS\arial.ttf
19:54:46.955 [main] DEBUG org.apache.pdfbox.pdmodel.font.FileSystemFontProvider - Loaded Arial-ItalicMT from C:\WINDOWS\FONTS\ariali.ttf
19:54:47.477 [Finalizer] DEBUG org.apache.pdfbox.io.ScratchFileBuffer - ScratchFileBuffer not closed!
Run Code Online (Sandbox Code Playgroud)
我尝试在调用 doOCR 的代码之前设置日志记录级别,但它似乎仍在记录此信息。这是我的代码片段:
19:54:46.712 [main] DEBUG org.apache.fontbox.util.autodetect.FontFileFinder - checkFontfile found …Run Code Online (Sandbox Code Playgroud)