PDF格式广泛用于生成各种类型的文档,例如发票、财务报告、简历等。在自动化领域,PDF 文档是从 Web 或桌面应用程序中生成和操作的。因此,在某些情况下,可能需要以编程方式向现有 PDF 文件添加文本。据此,本文展示了如何使用 C# 向 PDF 文档添加文本。
- 使用 C# 将文本添加到 PDF
- 使用 TextParagraph 将文本添加到 PDF
- 在 PDF 中插入透明文本
为了动态地向 PDF 文件添加文本,我们将使用Aspose.PDF for .NET。它是一个功能丰富的 API,可从 .NET 应用程序中创建和操作 PDF 文件。下载最新版Aspose.PDF for .NET
使用 C# 将文本添加到 PDF
以下是使用 C# 向 PDF 文件添加文本的步骤。
- 使用Document类加载 PDF 文件。
- 将所需的 PDF 页面放入Page对象中。
- 创建一个TextFragment对象并设置它的文本、位置、字体、背景颜色等。
- 创建一个TextBuilder对象并用Page对象初始化它。
- 使用TextBuilder.AppendText(TextFragment)方法向 PDF 页面添加文本。
- 使用Document.Save(String)方法保存更新的 PDF 文件。
以下代码示例展示了如何向 PDF 文件添加文本。
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Open document
Document pdfDocument = new Document("input.pdf");
// Get particular page
Page pdfPage = (Page)pdfDocument.Pages[1];
// Create text fragment
TextFragment textFragment = new TextFragment("main text");
textFragment.Position = new Position(100, 600);
// Set text properties
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray);
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
// Create TextBuilder object
TextBuilder textBuilder = new TextBuilder(pdfPage);
// Append the text fragment to the PDF page
textBuilder.AppendText(textFragment);
// Save resulting PDF document.
pdfDocument.Save("output.pdf");
使用 TextParagraph 将文本添加到 PDF
以下是使用TextParagraph将文本添加到 PDF 文件的步骤。
- 使用Document类加载 PDF 文件。
- 将所需的 PDF 页面放入一个Page对象或添加一个新页面。
- 创建一个TextBuilder对象并用Page对象初始化它。
- 创建一个TextParagraph类的对象。
- 使用TextParagraph.Rectangle属性指定段落的位置。
- 设置TextParagraph 的格式选项。
- 创建一个TextFragment对象并设置它的文本、位置、字体、背景颜色等。
- 使用TextParagraph.AppendLine(TextFragment)方法将 TextFragment 添加到段落。
- 使用TextBuilder.AppendParagraph(TextParagraph)方法向页面添加段落。
- 使用Document.Save(String)方法保存更新的 PDF 文件。
以下代码示例展示了如何使用 TextParagraph 类向 PDF 添加文本。
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
string fontFile = "font.ttf";
// Load input PDF file
Document doc = new Document("input.pdf");
// Create text builder object for first page of document
TextBuilder textBuilder = new TextBuilder(doc.Pages[1]);
// Create text fragment with sample string
TextFragment textFragment = new TextFragment("Hello world");
if (fontFile != "")
{
// Load the TrueType font into stream object
using (FileStream fontStream = File.OpenRead(fontFile))
{
// Set the font name for text string
textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF);
// Specify the position for Text Fragment
textFragment.Position = new Position(10, 10);
// Add the text to TextBuilder so that it can be placed over the PDF file
textBuilder.AppendText(textFragment);
}
// Save resulting PDF document
doc.Save("output.pdf");
}
在PDF中添加透明文本
Aspose.PDF for .NET 还允许向 PDF 文档添加透明文本,如以下步骤所示。
- 使用Document类加载 PDF 文件。
- 将所需的 PDF 页面放入一个Page对象或添加一个新页面。
- 创建并初始化一个Graph对象。
- 创建一个Rectangle对象并初始化它。
- 设置Rectangle.GraphInfo.FillColor属性。
- 使用Graph.Shapes.Add(Rectangle)方法向Graph添加矩形。
- 使用Page.Paragraphs.Add(Graph)方法将Graph添加到页面的段落集合中。
- 创建一个TextFragment对象并设置其TextState.ForegroundColor属性。
- 使用Page.Paragraphs.Add(TextFragment)方法将TextFragment添加到页面。
- 使用Document.Save(String)方法保存更新的 PDF 文件。
以下代码示例展示了如何向 PDF 文件添加透明文本。
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// Create Document instance
Document doc = new Document("input.pdf");
// Create page to pages collection of PDF file
Aspose.Pdf.Page page = doc.Pages.Add();
// Create Graph object
Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400);
// Create rectangle instance with certain dimensions
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400);
// Create color object from Alpha color channel
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183)));
// Add rectanlge to shapes collection of Graph object
canvas.Shapes.Add(rect);
// Add graph object to paragraphs collection of page object
page.Paragraphs.Add(canvas);
// Set value to not change position for graph object
canvas.IsChangePosition = false;
// Create TextFragment instance with sample value
TextFragment text = new TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text ");
// Create color object from Alpha channel
Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0);
// Set color information for text instance
text.TextState.ForegroundColor = color;
// Add text to paragraphs collection of page instance
page.Paragraphs.Add(text);
// Save the updated PDF file
doc.Save("output.pdf");
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。