在新发布的
Aspose.Pdf for Java 11.4.0中开发人员可以在PDF文件中添加隐藏文本,可以在调用ProcessPragraphs() 方法后使用PDF文件对象,新版本中还添加了新的旋转方法,可以用于文本注释的旋转,新的版本中还对以往存在的BUG进行了修复,开发人员可以从
控件中国网上下载最新的试用版进行相关功能的测试:
1.在新的11.4版本中如何为PDF文档添加隐藏文本
//Create document with hidden text
// add page to pages collection of PDF file
Page page = doc.getPages().add();
// create TextFragment instance
TextFragment frag1 = new TextFragment("This is common text.");
TextFragment frag2 = new TextFragment("This is invisible text.");
//Set text property - invisible
frag2.getTextState().setInvisible(true);
// add TextFragment to paragraphs collection of page instance
page.getParagraphs().add(frag1);
page.getParagraphs().add(frag2);
// save the PDF document
doc.save("c:/pdftest/HiddenText_output.pdf");
doc.dispose();
//Search text in the document
doc = new com.
Aspose.Pdf.Document("c:/pdftest/HiddenText_output.pdf");
// create TextFragmentAbsorber instance
TextFragmentAbsorber absorber = new TextFragmentAbsorber();
// get text content from first page of PDF file
absorber.visit(doc.getPages().get_Item(1));
// iterate through TextFragment inside TextFragments collection
for(com.
Aspose.Pdf.TextFragment fragment : (Iterable)absorber.getTextFragments())
{
//Display extracted TextFragments and their related properties
System.out.println("Text = " +fragment.getText() + ", on pos = "+ fragment.getPosition().toString() +
" and invisibility = " + fragment.getTextState().isInvisible());
}
// dispose Document object
doc.dispose();
2.如何在调用ProcessPragraphs()方法后使用Page对象
// instantiate Document object
Document document = new Document();
// add page to pages collection of PDF file
Page page1 = document.getPages().add();
// create a loop to add 5 TextFragments
for (int i = 1; i <= 5; i++)
{
// create table object
// set width for Table columns
table1.setColumnWidths("100");
// render table in new page
table1.setInNewPage(true);
// create a row object and add it to rows collection of table instance
// create TextFragment
TextFragment tf = new TextFragment("part"+ i);
tf.setInLineParagraph(false);
tf.setKeptWithNext(false);
// add cell to cells collection of row instance
com.
Aspose.Pdf.Cell cell1 = row1.getCells().add();
// add text fragment to paragraphs collection of table cell
cell1.getParagraphs().add(tf);
// add table to paragraphs collection of page object
page1.getParagraphs().add(table1);
// process paragraph objects inside PDF
document.processParagraphs();
// get page count information from PDF file
System.out.println("Number of pages in PDF = " + document.getPages().size());
}
// save resultant file
document.save("c:/PageCount_out.pdf");
3. 使用新的方法如何对PDF文档内的文本注释进行旋转
// instantiate Document object
Document document = new Document();
// add page to pages collection of PDF file
Page page1 = document.getPages().add();
// create FreeTextAnnotation instance
FreeTextAnnotation annotation = new FreeTextAnnotation(document.getPages().get_Item(1), new com.
Aspose.Pdf.Rectangle(50, 600,
250, 630),
new DefaultAppearance("Helvetica", 16, java.awt.Color.RED));
// set contents for FreeTextAnnotation
annotation.setContents("ABCDEFG");
// set rotating angle for Annotation
annotation.setRotate(Rotation.on90);
// create Rectangle object as per Annotation dimensions
Rectangle rect = annotation.getRect();
// rotate Rectangle object
rect.rotateAngle(90);
annotation.setRect(rect);
// set border color for Annotation as Red
annotation.getCharacteristics().setBorder(java.awt.Color.RED);
// set border information for Annotation instance
annotation.setBorder(new Border(annotation));
// set border width information for annotation instance
annotation.getBorder().setWidth(1);
// add annotation to first page of PDF file
document.getPages().get_Item(1).getAnnotations().add(annotation);
// save resultant file
document.save("c:/pdftest/Annotation_Rotate.pdf");