Tag: pdfbox

使用java中的PDFBox在其中心周围旋转PDF

PDDocument document = PDDocument.load(new File(input)); PDPage page = document.getDocumentCatalog().getPages().get(0); PDPageContentStream cs = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.PREPEND, false, false); cs.transform(Matrix.getRotateInstance(Math.toRadians(45), 0, 0)); 我正在使用上面的代码来旋转PDF。 对于上面的图像,我得到以下输出 从该代码开始,页面的内容已经移出框架,并且旋转不会围绕其中心发生。 但我希望得到输出 请给我一些选择。 提前致谢。

在PDFBox中,如何更改PDRectangle对象的原点(0,0)?

情况: 在PDFBox中,PDRectangle对象的默认原点(0,0)似乎是页面的左下角。 例如,下面的代码在页面的左下角为您提供了一个正方形,每边长度为100个单位。 PDRectangle rectangle = new PDRectangle(0, 0, 100, 100); 问题: 是否可以将原点更改为UPPER-LEFT转角,例如,上面的代码会在页面的左上角为您提供相同的方块? 我问的原因是: 我使用PDFTextStripper来获取文本的坐标(通过使用提取的TextPosition对象的getX()和getY()方法)。 从TextPosition对象检索的坐标似乎在UPPER-LEFT CORNER处具有原点(0,0)。 我希望我的PDRectangle对象的坐标与我的TextPosition对象的坐标具有相同的原点。 我试图通过“页面高度减去Y坐标”来调整PDRectangle的Y坐标。 这给了我想要的结果,但它并不优雅。 我想要一个优雅的解决方案 注意 :有人问过类似的问题。 答案就是我尝试过的,这不是最优雅的。 如何从左下角到左上角更改pdf页面中文本的坐标

使用pdfbox从单独的pdf(不同页面大小)添加页面作为图层

如果页面大小不同,如何将外部pdf文档中的页面添加到目标pdf? 这是我想要完成的事情: 我尝试使用LayerUtility(就像在这个例子中PDFBox LayerUtility – 将图层导入到现有PDF中 ),但是一旦我从外部pdf导入页面,该过程就会挂起: PDDocument destinationPdfDoc = PDDocument.load(fileInputStream); PDDocument externalPdf = PDDocument.load(EXTERNAL PDF); List destinationPages = destinationPdfDoc.getDocumentCatalog().getAllPages(); LayerUtility layerUtility = new LayerUtility(destinationPdfDoc); // process hangs here PDXObjectForm firstForm = layerUtility.importPageAsForm(externalPdf, 0); AffineTransform affineTransform = new AffineTransform(); layerUtility.appendFormAsLayer(destinationPages.get(0), firstForm, affineTransform, “external page”); destinationPdfDoc.save(resultTempFile); destinationPdfDoc.close(); externalPdf.close(); 我做错了什么?

如何使用pdfbox在pdf中添加超链接

我想在使用PDFBOX创建的PDF中添加一个超链接,这样我点击一些文本示例“点击此处”将重定向到URL。 我尝试使用PDAnnotationLink和PDActionURI ,但如何在contentstream添加它? PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); PDAnnotationLink txtLink = new PDAnnotationLink(); txtLink.setBorderStyle(borderULine); txtLink.setColour(colourBlue); // add an action PDActionURI action = new PDActionURI(); action.setURI(“www.google.com”); txtLink.setAction(action); contentStream.beginText(); contentStream.moveTextPositionByAmount(400, y-30); contentStream.drawString(txtLink);—-error contentStream.endText();

无法使用PDFBox将图像添加到pdf

我正在编写一个使用pdfbox库从头开始创建pdf的Java应用程序。 我需要将jpg图像放在其中一个页面中。 我正在使用此代码: PDDocument document = new PDDocument(); PDPage page = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); /* … */ /* code to add some text to the page */ /* … */ InputStream in = new FileInputStream(new File(“c:/myimg.jpg”)); PDJpeg img = new PDJpeg(document, in); contentStream.drawImage(img, 100, 700); contentStream.close(); document.save(“c:/mydoc.pdf”); 当我运行代码时,它会成功终止,但如果我使用Acrobat Reader打开生成的pdf文件,页面将完全为白色,并且图像不会放入其中。 […]

Java:Apache PDFbox提取突出显示的文本

我正在使用Apache PDFbox库从PDF文件中提取突出显示的文本(即黄色背景)。 我对这个库完全是新手,并且不知道它用于此目的的哪个类。 到目前为止,我已使用下面的代码从注释中提取文本。 PDDocument pddDocument = PDDocument.load(new File(“test.pdf”)); List allPages = pddDocument.getDocumentCatalog().getAllPages(); for (int i = 0; i < allPages.size(); i++) { int pageNum = i + 1; PDPage page = (PDPage) allPages.get(i); List la = page.getAnnotations(); if (la.size() < 1) { continue; } System.out.println("Total annotations = " + la.size()); System.out.println("\nProcess Page " + […]

将BufferedImage添加到PDFBox文档

在我当前的项目中,我尝试将一个BufferedImage添加到PDFBox文档中。 更具体地说,我使用了JFreeChart的图像。 我的代码如下所示: public void exportToPDF(JFreeChart chart, String filePath){ PDDocument doc = null; PDPage page = null; PDXObjectImage ximage = null; try { doc = new PDDocument(); page = new PDPage(); doc.addPage(page); PDPageContentStream content = new PDPageContentStream(doc, page); BufferedImage image = chart.createBufferedImage(300, 300); ximage = new PDJpeg(doc, image); content.drawImage(ximage, 20, 20); content.close(); } catch(IOException ie) […]

将PDF文件转换为图像

我想将PDF文档转换为图像。 我在使用Ghost4j。 问题: Ghost4J在运行时需要gsdll32.dll文件,我不想使用dll文件。 问题1:有没有办法,在ghost4j中转换图像而不用dll? 问题2:我在PDFBox API中找到了解决方案。 org.apache.pdfbox.pdmodel.PDPagep have method convertToImage()`,它将PDF页面转换为图像格式。 PDDocument doc = PDDocument.load(new File(“/document.pdf”)); Listpages = doc.getDocumentCatalog().getAllPages(); PDPage page = pages.get(0); BufferedImage image =page.convertToImage(); File outputfile = new File(“/image.png”); ImageIO.write(image, “png”, outputfile); doc.close(); 我在PDF文档上只有文字。 我运行此代码时遇到exception: Aug 12, 2013 6:00:24 PM org.apache.pdfbox.util.PDFStreamEngine processOperator INFO: unsupported/disabled operation: BDC Exception in thread “main” java.lang.ExceptionInInitializerError at org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.getawtFont(PDTrueTypeFont.java:481) […]

PDFBox Pdf到图像丢失QR码“ColorSpace Pattern不提供非抚摸颜色”

类似于这个SO PDFBox – PDF到图像丢失条形码 有问题的PDF: https : //drive.google.com/file/d/0B13zTPQR9uxscXRMWjhsZ0doa00/view?usp=sharing 有最小的文本和中等大小的QR码。 我尝试了许多不同的解决方案,使用PDFBox / ImageIO将此PDF页面转换为图像,但到目前为止,结果中始终缺少QR码。 当我使用PDFBox的PDFImageWriter时,我得到这个日志: ColorSpace Pattern doesn’t provide a non-stroking color, using white instead! 我认为这与二维码有关。 这是预期的行为吗? 其他人可以确认PDFBox无法从此PDF中复制QR码吗? 有没有办法使用Java或PDFBox将其转换为图像?

使用pdfbox从pdf中删除不可见的文本

链接到pdf 当我尝试从上面的pdf中提取文本时,我得到了一个在evince查看器中看不见的文本混合文本以及可见的文本。 此外,一些所需的文本缺少观众中没有丢失的字符,例如“FALCONS”中的“S”和许多缺少的“½”字符。 我相信这是由于隐形文本的干扰,因为当在查看器中突出显示pdf时,可以看到不可见文本与可见文本重叠。 有没有办法删除不可见的文字? 还是有其他解决方案吗? 码: import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class App { public static String getPdfText(String pdfPath) throws IOException { File file = new File(pdfPath); PDDocument document = null; PDFTextStripper textStripper = null; String text = null; try { document = PDDocument.load(file); textStripper = new PDFTextStripper(); textStripper.setEndPage(1); text […]