pdfbox

PDF find out if text is underlined or a table cell

ぐ巨炮叔叔 提交于 2019-11-29 11:06:31
问题 I have been playing around with PdfBox and PDFTextStripperByArea method. I was able to extract information if the text is bold or italic , but I'm unable to get the underline information. As far as I understand it in PDF, underline is done by drawing lines. So in theory I should be able to get some sort of information about lines somewhere around the text. Giving this information I could then find out if either text is underlined or in a table. Here is my code so far: List<TextPosition>

How to Create a Radio Button Group with PDFBox 2.0

痴心易碎 提交于 2019-11-29 11:03:30
I want to create a Radio Button group using PDFBox 2.0, I am able to create 3 Radio Buttons, but I can't figure out how to group them (PDFBox 1.8, used PDRadioCollection , but 2.0 doesn't.). How do you create a Radio Button Group with PDFBox 2.0? Here is my current code: PDDocument document = new PDDocument(); PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); acroForm.setNeedAppearances(true); document.getDocumentCatalog().setAcroForm(acroForm); PDResources res = new PDResources(); COSName fontName = res.add(PDTrueTypeFont.load

Using PdfBox, how do I retrieve contents of PDDocument as a byte array?

ぐ巨炮叔叔 提交于 2019-11-29 09:18:07
I am currently using PdfBox as the driver for a pdf-file editor application. I need the contents of the PdfBox representation of a pdf file (PDDocument) as a byte array. Does anyone know how to do this? I hope it's not too late... ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); document.save(byteArrayOutputStream); document.close(); InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); And voila! You've got both input streams! 来源: https://stackoverflow.com/questions/11593116/using-pdfbox-how-do-i-retrieve-contents-of-pddocument-as-a

how to know if a field is on a particular page?

空扰寡人 提交于 2019-11-29 07:57:26
The PDFbox content stream is done per page, but the fields come from the form which comes from the catalog, which comes from the pdf doc itself. So I'm not sure which fields are on which pages, and its causing to write text out to incorrect locations/pages. ie. I'm processing fields per page, but not sure which fields are on which pages. Is there a way to tell which field is on which page? Or, is there a way to get just the fields on the current page? Thank you! Mark code snippet: PDDocument pdfDoc = PDDocument.load(file); PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog(); PDAcroForm

Can't add an image to a pdf using PDFBox

六眼飞鱼酱① 提交于 2019-11-29 05:26:54
I'm writing a java app that creates a pdf from scratch using the pdfbox library. I need to place a jpg image in one of the page. I'm using this code: 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"); When

Add page as layer from separate pdf(different page size) using pdfbox

拜拜、爱过 提交于 2019-11-29 05:08:55
How can I add a page from external pdf doc to destination pdf if pages have different sizes? Here is what I'd like to accomplish: I tried to use LayerUtility (like in this example PDFBox LayerUtility - Importing layers into existing PDF ), but once I import page from external pdf the process hangs: PDDocument destinationPdfDoc = PDDocument.load(fileInputStream); PDDocument externalPdf = PDDocument.load(EXTERNAL PDF); List<PDPage> destinationPages = destinationPdfDoc.getDocumentCatalog().getAllPages(); LayerUtility layerUtility = new LayerUtility(destinationPdfDoc); // process hangs here

How to sign pdf in Java using pdfbox

妖精的绣舞 提交于 2019-11-29 03:55:15
I am trying to sign pdf using pdfbox libraries. I have stuck now and realy need a help. This is my code: private static void signPdf(PDDocument document) throws Exception { PDSignature sig = new PDSignature(); sig.setFilter(COSName.ADOBE_PPKLITE); sig.setSubFilter(COSName.ADBE_PKCS7_DETACHED); sig.setByteRange(new int[] {'a','a','a','a'}); sig.setContents(new byte[]{(byte) 23, (byte) 23, (byte) 23, (byte) 23}); SignatureOptions options = new SignatureOptions(); document.addSignature(sig, new SignatureInterface() { public byte[] sign(InputStream content) throws SignatureException, IOException {

Add BufferedImage to PDFBox document

妖精的绣舞 提交于 2019-11-28 23:57:37
In my current project, I try to add a BufferedImage to a PDFBox document. More specificly, I use an image from a JFreeChart . My code looks like this: 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) { } doc.save

How to center a text using PDFBox

隐身守侯 提交于 2019-11-28 20:52:21
问题 My question is very simple: how can I center a text on a PDF, using PDFBox ? I don't know the string in advance, I can't find the middle by trial. The string doesn't always have the same width. I need either: A method that can center the text, something like addCenteredString(myString) A method that can give me the width of the string in pixels. I can then calculate the center, for I know the dimensions of the PDF. Any help is welcome! 回答1: Ok, I found the answer myself. Here is how to center

PDFBox: How to print pdf with specified printer?

淺唱寂寞╮ 提交于 2019-11-28 20:43:51
I want to use PDFBox for printing PDF files created by iText. I have tried this successfully with PDDocument class and its method print(). You can find documentation here: http://pdfbox.apache.org/apidocs/ . (I am using this code:) public static void printPDF(String fileName) throws IOException, PrinterException { PDDocument doc = PDDocument.load(fileName); doc.print(); } The method print() works great, but there is one problem: When I need to print multiple files, the method asks me to select printer for each one of documents.. Is there any way how to set printer only once? For printer