Merge branch 'eugenp:master' into danielmcnally285_string_to_long
This commit is contained in:
commit
c9c82e9e33
|
@ -0,0 +1,49 @@
|
|||
package com.baeldung.aboutlength;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StringLengthAndByteArrayLengthUnitTest {
|
||||
|
||||
@Test
|
||||
void whenStrWithAllAsciiChar_thenStrLengthAndBytesLengthAreEqual() {
|
||||
String s = "beautiful";
|
||||
assertEquals(9, s.length());
|
||||
assertEquals(9, s.getBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenStrWithUnicodeChar_thenStrLengthAndBytesLengthAreNotEqual() {
|
||||
assertEquals("f6", Integer.toHexString('ö'));
|
||||
assertEquals("7f8e", Integer.toHexString('美'));
|
||||
assertEquals("4e3d", Integer.toHexString('丽'));
|
||||
|
||||
String de = "schöne";
|
||||
assertEquals(6, de.length());
|
||||
assertEquals(7, de.getBytes().length);
|
||||
|
||||
String cn = "美丽";
|
||||
assertEquals(2, cn.length());
|
||||
assertEquals(6, cn.getBytes().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingUTF_32_thenBytesLengthIs4TimesStrLength() {
|
||||
Charset UTF_32 = Charset.forName("UTF_32");
|
||||
|
||||
String en = "beautiful";
|
||||
assertEquals(9, en.length());
|
||||
assertEquals(9 * 4, en.getBytes(UTF_32).length);
|
||||
|
||||
String de = "schöne";
|
||||
assertEquals(6, de.length());
|
||||
assertEquals(6 * 4, de.getBytes(UTF_32).length);
|
||||
|
||||
String cn = "美丽";
|
||||
assertEquals(2, cn.length());
|
||||
assertEquals(2 * 4, cn.getBytes(UTF_32).length);
|
||||
}
|
||||
}
|
|
@ -9,9 +9,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -59,6 +59,8 @@
|
|||
|
||||
<properties>
|
||||
<faunadb.version>4.2.0</faunadb.version>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<start-class>com.baeldung.faunablog.FaunaBlogApplication</start-class>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -5,15 +5,17 @@ import com.faunadb.client.FaunaClient;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableGlobalMethodSecurity(prePostEnabled = true)
|
||||
@EnableMethodSecurity
|
||||
public class WebSecurityConfiguration {
|
||||
|
||||
@Autowired
|
||||
|
@ -21,13 +23,11 @@ public class WebSecurityConfiguration {
|
|||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.csrf()
|
||||
.disable();
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/**")
|
||||
.permitAll()
|
||||
.and()
|
||||
.httpBasic();
|
||||
http.csrf(CsrfConfigurer::disable)
|
||||
.authorizeHttpRequests(requests -> requests
|
||||
.requestMatchers("/**")
|
||||
.permitAll())
|
||||
.httpBasic(Customizer.withDefaults());
|
||||
return http.build();
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
|
|
@ -6,7 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
|
|
@ -6,6 +6,8 @@ import org.springframework.security.core.userdetails.User;
|
|||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
|
@ -30,8 +32,9 @@ public class FaunaUserDetailsService implements UserDetailsService {
|
|||
if (userData == null) {
|
||||
throw new UsernameNotFoundException("User not found");
|
||||
}
|
||||
PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
||||
|
||||
return User.withDefaultPasswordEncoder()
|
||||
return User.builder().passwordEncoder(encoder::encode)
|
||||
.username(userData.at("data", "username").to(String.class).orNull())
|
||||
.password(userData.at("data", "password").to(String.class).orNull())
|
||||
.roles("USER")
|
||||
|
|
|
@ -11,6 +11,7 @@ import org.hibernate.service.ServiceRegistry;
|
|||
import com.baeldung.hibernate.exception.persistentobject.entity.Article;
|
||||
import com.baeldung.hibernate.exception.persistentobject.entity.Author;
|
||||
import com.baeldung.hibernate.exception.persistentobject.entity.Book;
|
||||
import com.baeldung.hibernate.namedparameternotbound.Person;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static SessionFactory sessionFactory;
|
||||
|
@ -34,6 +35,7 @@ public class HibernateUtil {
|
|||
configuration.addAnnotatedClass(Book.class);
|
||||
configuration.addAnnotatedClass(Author.class);
|
||||
configuration.addAnnotatedClass(Article.class);
|
||||
configuration.addAnnotatedClass(Person.class);
|
||||
|
||||
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
|
||||
.applySettings(configuration.getProperties()).build();
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.hibernate.namedparameternotbound;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.hibernate.namedparameternotbound;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.query.Query;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.baeldung.hibernate.exception.persistentobject.HibernateUtil;
|
||||
|
||||
class NamedParameterNotBoundExceptionUnitTest {
|
||||
|
||||
private static Session session;
|
||||
|
||||
@BeforeAll
|
||||
static void init() {
|
||||
session = HibernateUtil.getSessionFactory()
|
||||
.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void clear() {
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenSettingValueToNamedParameter_thenDoNotThrowQueryException() {
|
||||
Query<Person> query = session.createQuery("FROM Person p WHERE p.firstName = :firstName", Person.class);
|
||||
query.setParameter("firstName", "Azhrioun");
|
||||
|
||||
assertNotNull(query.list());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenNotSettingValueToNamedParameter_thenThrowQueryException() {
|
||||
Exception exception = assertThrows(QueryException.class, () -> {
|
||||
Query<Person> query = session.createQuery("FROM Person p WHERE p.firstName = :firstName", Person.class);
|
||||
query.list();
|
||||
});
|
||||
|
||||
String expectedMessage = "Named parameter not bound";
|
||||
String actualMessage = exception.getMessage();
|
||||
|
||||
assertTrue(actualMessage.contains(expectedMessage));
|
||||
}
|
||||
|
||||
}
|
12
pom.xml
12
pom.xml
|
@ -686,7 +686,6 @@
|
|||
<modules>
|
||||
<module>akka-modules</module>
|
||||
<module>algorithms-modules</module>
|
||||
<module>antlr</module>
|
||||
<module>apache-cxf-modules</module>
|
||||
<module>apache-httpclient-2</module>
|
||||
<module>apache-httpclient4</module>
|
||||
|
@ -700,9 +699,7 @@
|
|||
<module>apache-poi-3</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>asciidoctor</module>
|
||||
<module>atomix</module>
|
||||
<module>aws-modules</module>
|
||||
<module>azure</module>
|
||||
|
@ -813,8 +810,6 @@
|
|||
<module>osgi</module>
|
||||
<module>parent-boot-3</module>
|
||||
<module>patterns-modules</module>
|
||||
<module>pdf-2</module>
|
||||
<module>pdf</module>
|
||||
<module>performance-tests</module>
|
||||
<module>persistence-modules</module>
|
||||
<!--<module>persistence-modules/java-harperdb</module>--> <!-- This module requires a library to download manually -->
|
||||
|
@ -896,6 +891,7 @@
|
|||
<module>tensorflow-java</module>
|
||||
<module>testing-modules</module>
|
||||
<module>testing-modules/mockito-simple</module>
|
||||
<module>text-processing-libraries-modules</module>
|
||||
<module>timefold-solver</module>
|
||||
<module>vaadin</module>
|
||||
<module>vavr-modules</module>
|
||||
|
@ -940,7 +936,6 @@
|
|||
<modules>
|
||||
<module>akka-modules</module>
|
||||
<module>algorithms-modules</module>
|
||||
<module>antlr</module>
|
||||
<module>apache-cxf-modules</module>
|
||||
<module>apache-httpclient-2</module>
|
||||
<module>apache-httpclient4</module>
|
||||
|
@ -954,9 +949,7 @@
|
|||
<module>apache-poi-3</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-thrift</module>
|
||||
<module>apache-tika</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>asciidoctor</module>
|
||||
<module>atomix</module>
|
||||
<module>aws-modules</module>
|
||||
<module>azure</module>
|
||||
|
@ -1067,8 +1060,6 @@
|
|||
<module>osgi</module>
|
||||
<module>parent-boot-3</module>
|
||||
<module>patterns-modules</module>
|
||||
<module>pdf-2</module>
|
||||
<module>pdf</module>
|
||||
<module>performance-tests</module>
|
||||
<module>persistence-modules</module>
|
||||
<module>persistence-modules/spring-data-neo4j</module>
|
||||
|
@ -1148,6 +1139,7 @@
|
|||
<module>tensorflow-java</module>
|
||||
<module>testing-modules</module>
|
||||
<module>testing-modules/mockito-simple</module>
|
||||
<module>text-processing-libraries-modules</module>
|
||||
<module>timefold-solver</module>
|
||||
<module>vaadin</module>
|
||||
<module>vavr-modules</module>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
## Text Processing Libraries
|
||||
|
||||
This module contains modules about Text Processing Libraries.
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
|
@ -1,43 +1,43 @@
|
|||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.pdfcleanup.CleanUpProperties;
|
||||
import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
|
||||
import com.itextpdf.pdfcleanup.PdfCleanUpTool;
|
||||
import com.itextpdf.pdfcleanup.PdfCleaner;
|
||||
import com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy;
|
||||
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;
|
||||
|
||||
public class PdfContentRemover {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung-modified.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-cleaned.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
removeContentFromDocument(pdfDocument);
|
||||
pdfDocument.close();
|
||||
}
|
||||
|
||||
private static void removeContentFromDocument(PdfDocument pdfDocument) throws IOException {
|
||||
// 5.1. remove text
|
||||
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
|
||||
strategy.add(new RegexBasedCleanupStrategy("Baeldung"));
|
||||
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);
|
||||
|
||||
// 5.2. remove other areas
|
||||
List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(10, 50, 90, 70)), new PdfCleanUpLocation(2, new Rectangle(35, 400, 100, 35)));
|
||||
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations, new CleanUpProperties());
|
||||
cleaner.cleanUp();
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.pdfcleanup.CleanUpProperties;
|
||||
import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
|
||||
import com.itextpdf.pdfcleanup.PdfCleanUpTool;
|
||||
import com.itextpdf.pdfcleanup.PdfCleaner;
|
||||
import com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy;
|
||||
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;
|
||||
|
||||
public class PdfContentRemover {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung-modified.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-cleaned.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
removeContentFromDocument(pdfDocument);
|
||||
pdfDocument.close();
|
||||
}
|
||||
|
||||
private static void removeContentFromDocument(PdfDocument pdfDocument) throws IOException {
|
||||
// 5.1. remove text
|
||||
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
|
||||
strategy.add(new RegexBasedCleanupStrategy("Baeldung"));
|
||||
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);
|
||||
|
||||
// 5.2. remove other areas
|
||||
List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(10, 50, 90, 70)), new PdfCleanUpLocation(2, new Rectangle(35, 400, 100, 35)));
|
||||
PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDocument, cleanUpLocations, new CleanUpProperties());
|
||||
cleaner.cleanUp();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,86 +1,86 @@
|
|||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import com.itextpdf.forms.PdfAcroForm;
|
||||
import com.itextpdf.forms.fields.PdfFormField;
|
||||
import com.itextpdf.forms.fields.PdfTextFormField;
|
||||
import com.itextpdf.io.image.ImageData;
|
||||
import com.itextpdf.io.image.ImageDataFactory;
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfString;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
|
||||
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
|
||||
import com.itextpdf.layout.Document;
|
||||
import com.itextpdf.layout.element.Image;
|
||||
import com.itextpdf.layout.element.Paragraph;
|
||||
import com.itextpdf.layout.element.Table;
|
||||
import com.itextpdf.layout.element.Text;
|
||||
import com.itextpdf.layout.properties.UnitValue;
|
||||
|
||||
public class PdfEditor {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-modified.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
addContentToDocument(pdfDocument);
|
||||
}
|
||||
|
||||
private static void addContentToDocument(PdfDocument pdfDocument) throws MalformedURLException {
|
||||
// 4.1. add form
|
||||
PdfFormField personal = PdfFormField.createEmptyField(pdfDocument);
|
||||
personal.setFieldName("information");
|
||||
PdfTextFormField name = PdfFormField.createText(pdfDocument, new Rectangle(35, 400, 100, 30), "name", "");
|
||||
personal.addKid(name);
|
||||
PdfAcroForm.getAcroForm(pdfDocument, true)
|
||||
.addField(personal, pdfDocument.getFirstPage());
|
||||
|
||||
// 4.2. add new page
|
||||
pdfDocument.addNewPage(1);
|
||||
|
||||
// 4.3. add annotation
|
||||
PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(40, 435, 0, 0)).setTitle(new PdfString("name"))
|
||||
.setContents("Your name");
|
||||
pdfDocument.getPage(2)
|
||||
.addAnnotation(ann);
|
||||
|
||||
// create document form pdf document
|
||||
Document document = new Document(pdfDocument);
|
||||
|
||||
// 4.4. add an image
|
||||
ImageData imageData = ImageDataFactory.create("src/main/resources/baeldung.png");
|
||||
Image image = new Image(imageData).scaleAbsolute(550, 100)
|
||||
.setFixedPosition(1, 10, 50);
|
||||
document.add(image);
|
||||
|
||||
// 4.5. add a paragraph
|
||||
Text title = new Text("This is a demo").setFontSize(16);
|
||||
Text author = new Text("Baeldung tutorials.");
|
||||
Paragraph p = new Paragraph().setFontSize(8)
|
||||
.add(title)
|
||||
.add(" from ")
|
||||
.add(author);
|
||||
document.add(p);
|
||||
|
||||
// 4.6. add a table
|
||||
Table table = new Table(UnitValue.createPercentArray(2));
|
||||
table.addHeaderCell("#");
|
||||
table.addHeaderCell("company");
|
||||
table.addCell("name");
|
||||
table.addCell("baeldung");
|
||||
document.add(table);
|
||||
|
||||
// close the document
|
||||
// this automatically closes the pdfDocument, which then closes automatically the pdfReader and pdfWriter
|
||||
document.close();
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import com.itextpdf.forms.PdfAcroForm;
|
||||
import com.itextpdf.forms.fields.PdfFormField;
|
||||
import com.itextpdf.forms.fields.PdfTextFormField;
|
||||
import com.itextpdf.io.image.ImageData;
|
||||
import com.itextpdf.io.image.ImageDataFactory;
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfString;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.annot.PdfAnnotation;
|
||||
import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation;
|
||||
import com.itextpdf.layout.Document;
|
||||
import com.itextpdf.layout.element.Image;
|
||||
import com.itextpdf.layout.element.Paragraph;
|
||||
import com.itextpdf.layout.element.Table;
|
||||
import com.itextpdf.layout.element.Text;
|
||||
import com.itextpdf.layout.properties.UnitValue;
|
||||
|
||||
public class PdfEditor {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-modified.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
addContentToDocument(pdfDocument);
|
||||
}
|
||||
|
||||
private static void addContentToDocument(PdfDocument pdfDocument) throws MalformedURLException {
|
||||
// 4.1. add form
|
||||
PdfFormField personal = PdfFormField.createEmptyField(pdfDocument);
|
||||
personal.setFieldName("information");
|
||||
PdfTextFormField name = PdfFormField.createText(pdfDocument, new Rectangle(35, 400, 100, 30), "name", "");
|
||||
personal.addKid(name);
|
||||
PdfAcroForm.getAcroForm(pdfDocument, true)
|
||||
.addField(personal, pdfDocument.getFirstPage());
|
||||
|
||||
// 4.2. add new page
|
||||
pdfDocument.addNewPage(1);
|
||||
|
||||
// 4.3. add annotation
|
||||
PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(40, 435, 0, 0)).setTitle(new PdfString("name"))
|
||||
.setContents("Your name");
|
||||
pdfDocument.getPage(2)
|
||||
.addAnnotation(ann);
|
||||
|
||||
// create document form pdf document
|
||||
Document document = new Document(pdfDocument);
|
||||
|
||||
// 4.4. add an image
|
||||
ImageData imageData = ImageDataFactory.create("src/main/resources/baeldung.png");
|
||||
Image image = new Image(imageData).scaleAbsolute(550, 100)
|
||||
.setFixedPosition(1, 10, 50);
|
||||
document.add(image);
|
||||
|
||||
// 4.5. add a paragraph
|
||||
Text title = new Text("This is a demo").setFontSize(16);
|
||||
Text author = new Text("Baeldung tutorials.");
|
||||
Paragraph p = new Paragraph().setFontSize(8)
|
||||
.add(title)
|
||||
.add(" from ")
|
||||
.add(author);
|
||||
document.add(p);
|
||||
|
||||
// 4.6. add a table
|
||||
Table table = new Table(UnitValue.createPercentArray(2));
|
||||
table.addHeaderCell("#");
|
||||
table.addHeaderCell("company");
|
||||
table.addCell("name");
|
||||
table.addCell("baeldung");
|
||||
document.add(table);
|
||||
|
||||
// close the document
|
||||
// this automatically closes the pdfDocument, which then closes automatically the pdfReader and pdfWriter
|
||||
document.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,45 +1,45 @@
|
|||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.itextpdf.kernel.colors.ColorConstants;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfPage;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||
import com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation;
|
||||
import com.itextpdf.layout.Canvas;
|
||||
import com.itextpdf.layout.element.Paragraph;
|
||||
import com.itextpdf.pdfcleanup.PdfCleaner;
|
||||
import com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy;
|
||||
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;
|
||||
|
||||
public class PdfTextReplacement {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung-modified.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-fixed.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
replaceTextContentFromDocument(pdfDocument);
|
||||
pdfDocument.close();
|
||||
}
|
||||
|
||||
private static void replaceTextContentFromDocument(PdfDocument pdfDocument) throws IOException {
|
||||
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
|
||||
strategy.add(new RegexBasedCleanupStrategy("Baeldung tutorials").setRedactionColor(ColorConstants.WHITE));
|
||||
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);
|
||||
|
||||
for (IPdfTextLocation location : strategy.getResultantLocations()) {
|
||||
PdfPage page = pdfDocument.getPage(location.getPageNumber() + 1);
|
||||
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), page.getDocument());
|
||||
Canvas canvas = new Canvas(pdfCanvas, location.getRectangle());
|
||||
canvas.add(new Paragraph("HIDDEN").setFontSize(8)
|
||||
.setMarginTop(0f));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.pdfedition;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.itextpdf.kernel.colors.ColorConstants;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfPage;
|
||||
import com.itextpdf.kernel.pdf.PdfReader;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||
import com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation;
|
||||
import com.itextpdf.layout.Canvas;
|
||||
import com.itextpdf.layout.element.Paragraph;
|
||||
import com.itextpdf.pdfcleanup.PdfCleaner;
|
||||
import com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy;
|
||||
import com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy;
|
||||
|
||||
public class PdfTextReplacement {
|
||||
|
||||
private static final String SOURCE = "src/main/resources/baeldung-modified.pdf";
|
||||
private static final String DESTINATION = "src/main/resources/baeldung-fixed.pdf";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
PdfReader reader = new PdfReader(SOURCE);
|
||||
PdfWriter writer = new PdfWriter(DESTINATION);
|
||||
PdfDocument pdfDocument = new PdfDocument(reader, writer);
|
||||
replaceTextContentFromDocument(pdfDocument);
|
||||
pdfDocument.close();
|
||||
}
|
||||
|
||||
private static void replaceTextContentFromDocument(PdfDocument pdfDocument) throws IOException {
|
||||
CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
|
||||
strategy.add(new RegexBasedCleanupStrategy("Baeldung tutorials").setRedactionColor(ColorConstants.WHITE));
|
||||
PdfCleaner.autoSweepCleanUp(pdfDocument, strategy);
|
||||
|
||||
for (IPdfTextLocation location : strategy.getResultantLocations()) {
|
||||
PdfPage page = pdfDocument.getPage(location.getPageNumber() + 1);
|
||||
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamAfter(), page.getResources(), page.getDocument());
|
||||
Canvas canvas = new Canvas(pdfCanvas, location.getRectangle());
|
||||
canvas.add(new Paragraph("HIDDEN").setFontSize(8)
|
||||
.setMarginTop(0f));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 4.5 KiB |
|
@ -9,7 +9,7 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
|
@ -0,0 +1,24 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>text-processing-libraries-modules</artifactId>
|
||||
<name>text-processing-libraries-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>antlr</module>
|
||||
<module>apache-tika</module>
|
||||
<module>asciidoctor</module>
|
||||
<module>pdf</module>
|
||||
<module>pdf-2</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
Loading…
Reference in New Issue