Aspose.Pdf
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.Visit(page);
PageMarkup markup = absorber.PageMarkups[0];
支持得到段落,怎样才能替换这个段落里的所有的文本?
用什么方法能够替换每个段落?
我想要实现把每段文字替换成我想输入的文字
谢谢支持
我们需要调查您的这一要求,为此,我们的问题跟踪系统中已将调查单记录为 PDFNET-50276。我们将进一步调查其详细信息,并在解决后立即通知您。请耐心等待,给我们一些时间。
我们对造成的不便很抱歉。
获取现有段落信息(ParagraphAbsorber)和添加新段落(TextBuilder 或 page.Paragraphs 集合)的功能在 Aspose.PDF 中是分开的。我们假设添加新的段落操作与浏览文档和对文本进行细微更改有本质上的不同。用户应该明确初始化添加新段落。
请考虑几个事实:
- Paragraphs Page集合的变化/page) 仅对添加的 BaseParagraph 对象有效。它对现有段落/文本没有影响;
- MarkupSection的段落集合的任何更改pdf/net/aspose.pdf.text/markupsection) 或 PageMarkup 对文档没有影响;
- 您可以在 MarkupParagraph 中更改一个或多个 TextFragments 的文本.aspose.com/pdf/net/aspose.pdf.text/markupparagraph)进行细微更改。但是大的改动很可能会导致一段看起来很糟糕的结果;
Aspose.PDF现在没有段落替换功能。即使添加了该功能,我们也对该功能的可用性有疑问。
我们推荐以下操作路线图来替换该段:
1)加载PDF文档
2)为PDF文档的页面创建TextBuilder;
3) 使用 ParagraphAbsorber 探索页面上现有的段落结构;
4) peek MarkupParagraph 替换为Text /aspose.pdf.text/markupparagraph/properties/text) 或坐标 Points;
5) 保存原始[MarkupParagraph]的位置/矩形信息(https://reference.aspose.com/pdf/net/aspose.pdf.text/markupparagraph)
6) 遍历段落 TextFragments 并将所有文本替换为空字符串;
7) 使用 Rectangle 创建新的 TextParagraph .pdf.text/textparagraph/properties/rectangle) 原始段落和新文本;
(如需替换多段,重复步骤4-7)
8) 使用 TextBuilder.AppendParagraph 方法在页面上添加新段落;
(如需处理多页,重复2-8步)
9)保存更新的PDF文档。
请考虑以下代码:
//Load document
Document doc = new Document(dataDir + "TextWithParagraphs.pdf");
//Get page for processing
Page page = doc.Pages[1];
//Create text builder for further adding of text
TextBuilder textBuilder = new TextBuilder(page);
//Create paragraph absorber and apply it to the page
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.Visit(page);
//Get page markup
PageMarkup markup = absorber.PageMarkups[0];
//Iterate through paragraphs on the markup in searching of paragraph to replace
foreach (MarkupSection section in markup.Sections)
{
foreach (MarkupParagraph paragraph in section.Paragraphs)
{
//Search for paragraph containing specified text
if (paragraph.Text.Contains("Quisque semper congue nisl"))
{
//Store rectangle of original paragraph
Rectangle paragraphRectangle = GetRectangleFromPoints(paragraph.Points);
//Store text state of paragraph text
TextState originalTextState = paragraph.Fragments[0].TextState;
//Remove text of original paragraph
foreach (TextFragment fragment in paragraph.Fragments)
{
fragment.Text = String.Empty;
}
//Create new text paragraph for replacement
TextParagraph replacementParagraph = new TextParagraph();
//Apply original position and size to added paragraph
replacementParagraph.Rectangle = paragraphRectangle;
//Create text fragment with replacement text
TextFragment replacementFragment = new TextFragment("The quick brown fox jumps over the lazy dog. Jackdaws love my big sphinx of quartz. The rain in Spain stays mainly in the plain.");
//Apply original text properties to added paragraph
replacementFragment.TextState.Font = originalTextState.Font;
replacementFragment.TextState.FontSize = originalTextState.FontSize;
//LineSpacing requires manual fitting now. We plan to fix it in future.
replacementFragment.TextState.LineSpacing = originalTextState.FontSize * 0.20f;
//HorizontalAlignment requires manual setting now. We plan to fix it in future.
replacementParagraph.HorizontalAlignment = HorizontalAlignment.Justify;
//Fill new paragraph with text
replacementParagraph.AppendLine(replacementFragment);
//Set properties for paragraph placement
replacementParagraph.VerticalAlignment = VerticalAlignment.Top;
replacementParagraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
//Put replacement paragraph on the page
textBuilder.AppendParagraph(replacementParagraph);
}
}
}
//Save updated document
doc.Save(dataDir + "TextWithParagraphs_replaced.pdf");
private static Rectangle GetRectangleFromPoints(Point[] points)
{
Rectangle rectangle = new Rectangle(points[0].X, points[0].Y, points[0].X, points[0].Y);
for (int i = 1; i < points.Length; i++)
{
rectangle.LLX = Math.Min(rectangle.LLX, points[i].X);
rectangle.LLY = Math.Min(rectangle.LLY, points[i].Y);
rectangle.URX = Math.Max(rectangle.URX, points[i].X);
rectangle.URY = Math.Max(rectangle.URY, points[i].Y);
}
return rectangle;
}
输入:TextWithParagraphs.pdf (13.9 KB)
输出:TextWithParagraphs_replaced.pdf (45.4 KB)
该代码是一个简单的实现示例。我们不能保证它会为任何输入文件产生良好的结果。