我正在尝试在PDF中插入图像,但质量使图像不可读.如何提高最终PDF文档的质量?
我已经尝试过其他免费的非GPL许可证库,我认为pdfbox是最好的,所以我希望能够使用pdfbox.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
public class pdfBoxTest {
public static void WritteBufferedImageToPDF(BufferedImage buff)
{
PDDocument doc = null;
PDPage page = null;
PDXObjectImage ximage = null;
try {
doc = new PDDocument();
page = new PDPage();
doc.addPage(page);
ximage = new PDJpeg(doc, buff, 1.0f);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.drawImage(ximage, 0, 0);
content.close();
doc.save("C:/Users/crusader/Desktop/Hello World.pdf");
doc.close();
}
catch …Run Code Online (Sandbox Code Playgroud) 如何使用PDFBox"展平"PDF表单(删除表单字段但保留字段文本)?
快速执行此操作的方法是从acrofrom中删除字段.
为此,您只需要获取文档目录,然后获取acroform,然后从此acroform中删除所有字段.
图形表示与注释链接并保留在文档中.
所以我写了这段代码:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
public class PdfBoxTest {
public void test() throws Exception {
PDDocument pdDoc = PDDocument.load(new File("E:\\Form-Test.pdf"));
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm acroForm = pdCatalog.getAcroForm();
if (acroForm == null) {
System.out.println("No form-field --> stop");
return;
}
@SuppressWarnings("unchecked")
List<PDField> fields = acroForm.getFields();
// set the text in the form-field <-- does work
for (PDField field : fields) {
if (field.getFullyQualifiedName().equals("formfield1")) {
field.setValue("Test-String");
} …Run Code Online (Sandbox Code Playgroud) 如何使用Java确定PDF页面是包含文本还是纯图片?
我搜索了很多论坛和网站,但我还没找到答案.
是否可以从PDF中提取文本,以了解页面是否采用格式图片或文本?
PdfReader reader = new PdfReader(INPUTFILE);
PrintWriter out = new PrintWriter(new FileOutputStream(OUTPUTFILE));
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
// here I want to test the structure of the page !!!! if it's possible
out.println(PdfTextExtractor.getTextFromPage(reader, i));
}
Run Code Online (Sandbox Code Playgroud) PDFBox的字体类中有一种方法,PDFont,名为getFontHeight,听起来很简单.但是我不太了解文档以及参数代表什么.
getFontHeight这将获得角色的字体宽度.参数:
- c - 获取宽度的字符代码.
- offset - 数组中的偏移量.长度
- 数据的长度.
返回: 宽度为1000单位的文本空间,即333或777
这种方法是否适合用于获取PDFBox中字符的高度,如果是这样的话?我可以使用字体高度和字体大小之间的某种关系吗?
我有以下代码
0. templatePage = (PDPage) PDDocument.load(file).getDocumentCatalog().getAllPages().get(0);
1. ...
2. document.importPage(templatePage); //first page
3. ... //draw stuff
4. document.importPage(templatePage); //next page
5. ...
Run Code Online (Sandbox Code Playgroud)
如果在第3行,我只画了一些东西,那么一切正常.但是,如果我画了很多东西,那么我得到:
Index: 12, Size: 0. Stacktrace follows:
java.lang.IndexOutOfBoundsException: Index: 12, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at org.apache.pdfbox.io.RandomAccessBuffer.seek(RandomAccessBuffer.java:84)
at org.apache.pdfbox.io.RandomAccessFileInputStream.read(RandomAccessFileInputStream.java:96)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at org.apache.pdfbox.pdmodel.PDDocument.importPage(PDDocument.java:654)
at xxx.PdfReport.breakPage(PdfReport.java:145)
at xxx.PdfReport.print(PdfReport.java:84)
Run Code Online (Sandbox Code Playgroud)
上面的代码在95%的情况下工作,只有当页面真的满时才会出现问题.
如果在第2行和第4行.我使用
document.addPage(new PDPage());
Run Code Online (Sandbox Code Playgroud)
然后它工作正常.但目标是使用模板pdf.
我正在尝试将尺寸= 2496 x 3512的图像缩放为PDF文档.我正在使用PDFBox生成它,但缩放的图像最终模糊.
以下是一些片段:
PDF页面大小(A4)由page.findMediaBox()返回.createDimension():java.awt.Dimension [width = 612,height = 792]
然后我根据页面大小与图像大小计算缩放维度,返回:java.awt.Dimension [width = 562,height = 792]我使用下面的代码来计算缩放维度:
public static Dimension getScaledDimension(Dimension imgSize, Dimension boundary) {
int original_width = imgSize.width;
int original_height = imgSize.height;
int bound_width = boundary.width;
int bound_height = boundary.height;
int new_width = original_width;
int new_height = original_height;
// first check if we need to scale width
if (original_width > bound_width) {
//scale width to fit
new_width = bound_width;
//scale height to maintain aspect ratio
new_height = (new_width * …Run Code Online (Sandbox Code Playgroud)我需要使用PDFBOX(java)以正确的对齐方式绘制文本.
我目前正在使用ContentStream.drawString将文本绘制到pdf.我不使用等宽字体,因此字符的宽度会有所不同.
有任何想法吗?
Pdfbox Merge Document与1.8.xx一样,就像mergePdf.mergeDocuments()一样工作正常.现在pdfbox版本2.0.0包含一些参数,比如org.apache.pdfbox.multipdf.PDFMergerUtility.mergeDocuments(MemoryUsageSetting arg0)
什么是MemoryUsageSetting如何使用mergeDocuments.I读取就像合并源文档列表一样,保存导致目标文件.请提供一些相当于2.0.0版的代码
public void combine()
{
try
{
PDFMergerUtility mergePdf = new PDFMergerUtility();
String folder ="pdf";
File _folder = new File(folder);
File[] filesInFolder;
filesInFolder = _folder.listFiles();
for (File string : filesInFolder)
{
mergePdf.addSource(string);
}
mergePdf.setDestinationFileName("Combined.pdf");
mergePdf.mergeDocuments();
}
catch(Exception e)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用pdfbox-app-2.0.18.jar或pdfbox-app-2.0.17.jar。
从这里的示例中,我有以下代码:
try (FileOutputStream fos = new FileOutputStream(signedFile);
PDDocument doc = Loader.loadPDF(inputFile)) {
// code
}
Run Code Online (Sandbox Code Playgroud)
执行此代码后,我收到以下错误:
org.apache.pdfbox.Loader is not found
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
我正在使用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)