是否可以在pdf文档中标记字符串?

时间:2014-01-18 16:00:19

标签: java itext

我想知道是否可以用不同的颜色标记pdf中的字符串,或者在循环浏览pdf文档时加下划线?

2 个答案:

答案 0 :(得分:1)

可以创建文档。只需使用不同的块来设置样式。这是一个例子:

Document document = new Document();
PdfWriter.getInstance(document, outputStream);
document.open();
document.add(new Chunk("This word is "));

Chunk underlined = new Chunk("underlined");
underlined.setUnderline(1.0f, -1.0f); //We can customize thickness and position of underline
document.add(underlined);

document.add(new Chunk(". And this phrase has "));

Chunk background = new Chunk("yellow background.");
background.setBackground(BaseColor.YELLOW);
document.add(background);

document.add(Chunk.NEWLINE);
document.close();

但是,编辑现有PDF文档几乎是不可能的。 iText的作者在他的书中写道:

  

在PDF文档中,PDF页面上的每个字符或字形都有   固定位置,无论用于查看的应用程序如何   文献。这是一个优点,但它也有一个缺点。   假设您要将“edit”一词替换为“操纵”一词   在一个句子中,你必须重排文本。你必须重新定位   跟随该单词的所有字符。也许你甚至不得不这样做   将部分文本移动到下一页。如果这不是微不足道的   并非不可能。

     

如果您想“编辑”PDF,建议您更改原件   文件来源并重新制作PDF。

答案 1 :(得分:0)

Aspose.PDF API支持创建新的PDF文档并操作现有的PDF文档,而没有Adobe Acrobat依赖性。您可以搜索并添加“突出显示注释”来标记PDF文本。

使用Aspose.PDF Cloud SDK for Java的REST API解决方案:

// For complete examples and data files, please go to https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-java
String name = "02_pages.pdf";
String folder="Temp";
String remotePath=folder+"/"+name;
// File to upload
File file = new File("C:/Temp/"+name);
// Storage name is default storage
String storage = null;

// Get App Key and App SID from https://dashboard.aspose.cloud/
PdfApi pdfApi = new PdfApi("xxxxxxxxxxxxxxxxxxxx", "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxx");
//Upload file to cloud storage
pdfApi.uploadFile(remotePath,file,storage);
        
//Text Position
Rectangle rect = new Rectangle().LLX(259.27580539703365).LLY(743.4707997894287).URX(332.26148873138425).URY(765.5148007965088);

List<AnnotationFlags> flags = new ArrayList<>();
flags.add(AnnotationFlags.DEFAULT); 

HighlightAnnotation annotation = new HighlightAnnotation();
annotation.setName("Name Updated");
annotation.rect(rect);
annotation.setFlags(flags);
annotation.setHorizontalAlignment(HorizontalAlignment.CENTER);
annotation.setRichText("Rich Text Updated");
annotation.setSubject("Subj Updated");
annotation.setPageIndex(1);
annotation.setZindex(1);
annotation.setTitle("Title Updated");       
annotation.setModified("02/02/2018 00:00:00.000 AM");


List<HighlightAnnotation> annotations = new ArrayList<>();
annotations.add(annotation);
//Add Highlight Annotation to the PDF document
AsposeResponse response = pdfApi.postPageHighlightAnnotations(name,1, annotations, storage, folder);

//Download annotated PDF file from Cloud Storage
File downloadResponse = pdfApi.downloadFile(remotePath, null, null);
File dest = new File("C:/Temp/HighlightAnnotation.pdf");
Files.copy(downloadResponse.toPath(), dest.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
System.out.println("Completed......");

使用Aspose.PDF for Java的本地解决方案:

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Java
// Instantiate Document object
Document document = new Document("C:/Temp/Test.pdf");
// Create TextFragment Absorber instance to search particular text fragment
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Estoque");
// Iterate through pages of PDF document
for (int i = 1; i <= document.getPages().size(); i++) {
    // Get first page of PDF document
    Page page = document.getPages().get_Item(i);
    page.accept(textFragmentAbsorber);
}

// Create a collection of Absorbed text
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

// Iterate on above collection
for (int j = 1; j <= textFragmentCollection.size(); j++) {
    TextFragment textFragment = textFragmentCollection.get_Item(j);

    // Get rectangular dimensions of TextFragment object
    Rectangle rect = new Rectangle((float) textFragment.getPosition().getXIndent(), (float) textFragment.getPosition().getYIndent(), (float) textFragment.getPosition().getXIndent() + (float) textFragment.getRectangle().getWidth(), (float) textFragment.getPosition().getYIndent() + (float) textFragment.getRectangle().getHeight());

    // Instantiate HighLight Annotation instance
    HighlightAnnotation highLight = new HighlightAnnotation(textFragment.getPage(), rect);
    // Set opacity for annotation
    highLight.setOpacity(.80);  
    // Set the color of annotation
    highLight.setColor(Color.getYellow());
    // Add annotation to annotations collection of TextFragment
    textFragment.getPage().getAnnotations().add(highLight);
}
// Save updated document
document.save("C:/Temp/HighLight.pdf");

PS:我是Aspose的支持/宣传人员开发人员。