首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可以在pdf文档中标记字符串吗?

可以在pdf文档中标记字符串吗?
EN

Stack Overflow用户
提问于 2014-01-19 00:00:19
回答 2查看 322关注 0票数 1

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

EN

回答 2

Stack Overflow用户

发布于 2014-01-19 01:04:59

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

代码语言:javascript
运行
复制
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的作者在他的书中写道:

文档中,页面上的每个字符或字形都有其固定的位置,与用于查看文档的应用程序无关。这是一个优点,但也有一个缺点。假设你想将句子中的“edit”替换为“manipulate”,那么你必须重新排列文本。您必须重新定位该单词后面的所有字符。也许你甚至不得不将文本的一部分移到下一页。这不是微不足道的,如果不是不可能的话。

如果你想“编辑”PDF,建议你改变文档的原始源并重新生成PDF。

票数 1
EN

Stack Overflow用户

发布于 2020-08-31 15:23:29

PDF支持在不依赖Adobe Acrobat的情况下创建新的Aspose.PDF文档和操作现有的PDF文档。您可以搜索并添加高亮显示注释来标记PDF文本。

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

代码语言:javascript
运行
复制
// 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的内部部署解决方案

代码语言:javascript
运行
复制
// 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");

附言:我在Aspose担任支持/布道者开发人员。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21206416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档