我正在使用PDFBox和iText从各种语言创建一个简单(只是段落)的pdf文档.就像是 :
pdfBox:
private static void createPdfBoxDocument(File from, File to) {
PDDocument document = null;
try {
document = new TextToPDF().createPDFFromText(new FileReader(from));
document.save(new FileOutputStream(to));
} finally {
if (document != null)
document.close();
}
}
private void createPdfBoxDoc() throws IOException, FileNotFoundException, COSVisitorException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
PDType1Font font = PDType1Font.TIMES_ROMAN;
contentStream.setFont(font, 12);
contentStream.beginText();
contentStream.moveTextPositionByAmount(100, 400);
contentStream.drawString("š");
contentStream.endText();
contentStream.close();
document.save("test.pdf");
document.close();
}
Run Code Online (Sandbox Code Playgroud)
itext …
我正在阅读PDF并输出PDF,其中包含原始PDF的多个副本.我通过对PDFBox和iText做同样的事情进行测试.如果我单独复制每个页面,iText会创建一个小得多的输出.
问题:在PDFBox中是否有另一种方法可以实现更小的输出PDF.
对于一个示例输入文件,使用这两个工具为输出生成两个副本:
PDFBox的Java代码(很遗憾对您造成错误处理).注意它如何反复读取输入并将其作为一个整体复制:
PDFMergerUtility merger = new PDFMergerUtility();
PDDocument workplace = null;
try {
for (int cnt = 0; cnt < COPIES; ++cnt) {
PDDocument document = null;
InputStream stream = null;
try {
stream = new FileInputStream(new File(sourceFileName));
document = PDDocument.load(stream);
if (workplace == null) {
workplace = document;
} else {
merger.appendDocument(workplace, document);
}
} finally {
if (document != null && document != workplace) {
document.close(); …Run Code Online (Sandbox Code Playgroud) 
当我看到PDF结构时,我会找到/ DA标签。在pdf规范中,我读:DA-(必需)用于格式化文本的默认外观字符串。
我使用PDF BOX获取PDF格式的文本颜色信息.我可以使用以下代码获取输出.但我怀疑StrokingColor代表什么,非抚摸颜色代表什么.基于此,我将如何决定哪个文本具有哪种颜色.有人建议我吗?我的cuurent输出是这样的:DeviceRGB DeviceCMYK java.awt.Color [r = 63,g = 240,b = 0] java.awt.Color [r = 35,g = 31,b = 32] 34.934998 31.11 31.875
PDDocument doc = null;
try {
doc = PDDocument.load(strFilepath);
PDFStreamEngine engine = new PDFStreamEngine(ResourceLoader.loadProperties("org/apache/pdfbox/resources/PageDrawer.properties"));
PDPage page = (PDPage)doc.getDocumentCatalog().getAllPages().get(1);
engine.processStream(page, page.findResources(), page.getContents().getStream());
PDGraphicsState graphicState = engine.getGraphicsState();
System.out.println(graphicState.getStrokingColor().getColorSpace().getName());
System.out.println(graphicState.getNonStrokingColor().getColorSpace().getName());
System.out.println(graphicState.getNonStrokingColor().getJavaColor());
System.out.println(graphicState.getStrokingColor().getJavaColor());
float colorSpaceValues[] = graphicState.getStrokingColor().getColorSpaceValue();
for (float c : colorSpaceValues) {
System.out.println(c * 255);
}
}
finally {
if (doc != null) {
doc.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要以任意角度旋转PDF页面的内容,并且PDPage.setRotation(int)命令被限制为90度的倍数.页面的内容是矢量和文本,我需要能够稍后放大内容,这意味着由于分辨率的降低,我无法将页面转换为图像.
我想用PDFBox在我的PDF中写一些内容.一旦页面高度小于我需要创建另一页面的边距.我想保留游标信息.我有一种方法可以获取光标信息,例如光标所在的位置,这样我就可以从光标位置减去边距并为其添加另一个页面.现在我做了类似的事
PDRectangle rect = page.getMediaBox();
float positionY = rect.getWidth();
positionY = positionY - pdfWriter.defaultBottomMargin;
if(positionY < positionX) {
positionY = rect.getWidth();
PDPage page2 = page;
rect = page2.getMediaBox();
document.addPage(page2);
PDPageContentStream contentStream = new PDPageContentStream(document, page2);
contentStream.appendRawCommands("T*\n");
contentStream.beginText();
// contentStream.setFont(font, 12);
contentStream.moveTextPositionByAmount(positionX, positionY);
contentStream.drawString(tmpText[k]);
contentStream.endText();
contentStream.close();
}
Run Code Online (Sandbox Code Playgroud) - 您可以通过以下链接查看示例:http: //radixcode.com/pdfbox-example-code-how-to-extract-text-from-pdf-file-with-java/
import java.io.IOException;
public class JavaPDFTest {
public static void main(String[] args) throws IOException {
PDFManager pdfManager = new PDFManager();
pdfManger.setFilePath("E:\test.pdf");
System.out.println(pdfManager.ToText());
}
}
Run Code Online (Sandbox Code Playgroud)
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class PDFManager {
private PDFParser parser;
private PDFTextStripper pdfStripper;
private PDDocument pdDoc ;
private COSDocument cosDoc ;
private String Text ;
private String filePath;
private File file;
public PDFManager() {
}
public String ToText() throws IOException
{ …Run Code Online (Sandbox Code Playgroud) 使用PDFBox提取PDF时,是否可以保留文本格式?
我有一个解析PDF文档以获取信息的程序。当发布新版本的PDF时,作者使用粗体或斜体文本表示新的信息,并使用删除线或下划线删除所指示的省略文本。在PDFbox中使用基本的Stripper类会返回所有文本,但是格式会被删除,因此我无法判断文本是新的还是省略的。我目前正在使用以下项目示例代码:
Dim doc As PDDocument = Nothing
Try
doc = PDDocument.load(RFPFilePath)
Dim stripper As New PDFTextStripper()
stripper.setAddMoreFormatting(True)
stripper.setSortByPosition(True)
rtxt_DocumentViewer.Text = stripper.getText(doc)
Finally
If doc IsNot Nothing Then
doc.close()
End If
End Try
Run Code Online (Sandbox Code Playgroud)
如果我简单地将PDF文本复制并粘贴到保留格式的richtextbox中,我的解析代码就可以正常工作。我当时想通过打开PDF,全选,复制,关闭文档,然后将其粘贴到我的richtextbox中,以编程方式进行此操作,但这似乎很笨拙。
我正在尝试获取一个PDDocument对象并将其传递给其他模块,InputStream而不将文档保存到文件系统.
现在,我读到PDStream并且有点了解这个的目的.因此,我尝试做这样的事情:
PDStream stream = new PDStream(document);
InputStream is = stream.createInputStream();
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将输入流加载到a中时PDDocument,我收到此错误:
Exception in thread "main" java.io.IOException: Error: End-of-File, expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1111)
at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:1885)
at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:1868)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:245)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1098)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:995)
at app.DGDCreator.main(DGDCreator.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:143)
Run Code Online (Sandbox Code Playgroud)
后来我发现结果文件大小为0kb ...
我正在尝试使用PDFbox填充重复的表单。我正在使用TreeMap并使用单个记录填充表单。pdf格式的格式是在第一页上列出了六个记录,在第二页上插入了一个静态页。(对于大于六个记录的TreeMap,将重复此过程)。我得到的错误特定于TreeMap的大小。这就是我的问题。我不知道为什么当我用超过35个条目填充TreeMap时收到此警告:
2018年4月23日2:36:25 org.apache.pdfbox.cos.COSDocument最终定稿警告:警告:您尚未关闭PDF文档
public class test {
public static void main(String[] args) throws IOException, IOException {
// TODO Auto-generated method stub
File dataFile = new File("dataFile.csv");
File fi = new File("form.pdf");
Scanner fileScanner = new Scanner(dataFile);
fileScanner.nextLine();
TreeMap<String, String[]> assetTable = new TreeMap<String, String[]>();
int x = 0;
while (x <= 36) {
String lineIn = fileScanner.nextLine();
String[] elements = lineIn.split(",");
elements[0] = elements[0].toUpperCase().replaceAll(" ", "");
String key = elements[0];
key = key.replaceAll(" ", "");
assetTable.put(key, elements);
x++;
}
PDDocument …Run Code Online (Sandbox Code Playgroud) pdfbox ×10
java ×6
itext ×2
pdf ×2
acrobat ×1
adobe-reader ×1
fonts ×1
inputstream ×1
treemap ×1
vb.net ×1