Open pdf with pdfbox
LibHunt Java. Trending Popularity Index About. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.
Source Code. Our great sponsors. Scout APM - Less time debugging, more time building. The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives. Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars. Perform the required operations such as adding pages adding text, adding images to the loaded document. After adding all the pages, save the PDF document using the save method of the PDDocument class as shown in the following code block.
Finally close the document using the close method of the PDDocument class as shown below. This example demonstrates how to load an existing PDF Document. Here, we will load the PDF document sample. This example demonstrates how to retrieve the properties of an existing PDF document. Save this code in a file with name RetrivingDocumentAttributes.
Upon execution, the above program retrieves all the attributes of the document and displays them as shown below. In the previous chapter, we discussed how to add pages to a PDF document. In this chapter, we will discuss how to add text to an existing PDF document. You can load an existing document using the load method of the PDDocument class. Therefore, instantiate this class and load the required document as shown below. You can get the required page in a document using the getPage method.
Retrieve the object of the required page by passing its index to this method as shown below. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.
Using the newLineAtOffset method, you can set the position on the content stream in the page. You can set the font of the text to the required style using the setFont method of the PDPageContentStream class as shown below. To this method you need to pass the type and size of the font. This method accepts the required text in the form of string.
After inserting the text, you need to end the text using the endText method of the PDPageContentStream class as shown below. After adding the required content, save the PDF document using the save method of the PDDocument class as shown in the following code block.
This example demonstrates how to add contents to a page in a document. Save this code in a file with name AddingContent. Upon execution, the above program adds the given text to the document and displays the following message.
If you verify the PDF Document new. In the example provided in the previous chapter we discussed how to add text to a page in a PDF but through this program, you can only add the text that would fit in a single line.
If you try to add more content, all the text that exceeds the line space will not be displayed. For example, if you execute the above program in the previous chapter by passing the following string only a part of it will be displayed. Replace the string text of the example in the previous chapter with the above mentioned string and execute it. Upon execution, you will receive the following output.
In order to add multiple lines to a PDF you need to set the leading using the setLeading method and shift to new line using newline method after finishing each line. You can insert various kinds of data elements using the object of the class named PDPageContentStream.
You can set the font of the text to the required style using the setFont method of the PDPageContentStream class as shown below to this method you need to pass the type and size of the font. You can insert multiple strings using the ShowText method of the PDPageContentStream class, by dividing each of them using the newline method as shown below.
Save this program in a file with name AddMultipleLines. In the previous chapter, we have seen how to add text to an existing PDF document. In this chapter, we will discuss how to read text from an existing PDF document. Extracting text is one of the main features of the PDF box library. This class extracts all the text from the given PDF document. To this method you need to pass the document object as a parameter.
This method retrieves the text in a given document and returns it in the form of a String object. This example demonstrates how to read text from the above mentioned PDF document.
Here, we will create a Java program and load a PDF document named new. Save this code in a file with name ReadingText. Upon execution, the above program retrieves the text from the given PDF document and displays it as shown below. In the previous chapter, we have seen how to extract text from an existing PDF document. In this chapter, we will discuss how to insert image to a PDF document. Select a page in the PDF document and retrieve its page object using the getPage method as shown below.
It provides all the required methods to perform operations related to an image, such as, inserting an image, setting its height, setting its width etc. We can create an object of this class using the method createFromFile. To this method, we need to pass the path of the image which we want to add in the form of a string and the document object to which the image needs to be added.
You can insert an image in the PDF document using the drawImage method. To this method, you need to add the image object created in the above step and the required dimensions of the image width and height as shown below. Suppose we have a PDF document named sample. This example demonstrates how to add image to a blank page of the above mentioned PDF document.
Here, we will load the PDF document named sample. Save this code in a file with name InsertingImage. Upon execution, the above program inserts an image into the specified page of the given PDF document displaying the following message.
If you verify the document sample. In the previous chapter, we have seen how to insert an image in a PDF document. In this chapter, we will discuss how to encrypt a PDF document. Using this class, you can restrict users from performing the following operations. The StandardProtectionPolicy class is used to add a password based protection to a document. Instantiate the StandardProtectionPolicy class by passing the owner password, user password, and the AccessPermission object as shown below.
Set the permissions using the setPermissions method of the StandardProtectionPolicy class. This method accepts an AccessPermission object as a parameter. You can protect your document using the protect method of the PDDocument class as shown below.
Pass the StandardProtectionPolicy object as a parameter to this method. After adding the required content save the PDF document using the save method of the PDDocument class as shown in the following code block. Finally, close the document using close method of PDDocument class as shown below.
Suppose, we have a PDF document named sample.
0コメント