PDF处理控件Spire.PDF常见问题解答

简介: 为方便使用者快速掌握和了解Spire.PDF,本文列举了PDF理控件Spire.pdf常见问题及解答欢迎下载最新版体验!

为方便使用者快速掌握和了解Spire.PDF,本文列举了PDF理控件Spire.pdf常见问题及解答欢迎下载最新版体验!

Spire.PDF for .NET最新下载

如何将 HTML 代码转换为 PDF?

A:Spire.PDF 无法加载包含 html 代码的字符串。但是 Spire.Doc 可以加载它并且 Spire.Doc 支持 PDF 格式。所以你可以使用 Spire.Doc 来完成这项工作。完整代码:

string htmlstring = "

Header 1

First paragraph

";

Document doc = new Document();

Section sec = doc.AddSection();

Paragraph para = sec.AddParagraph();


//add html code to document

para.AppendHTML(htmlstring);


//save document as PDF format

doc.SaveToFile("result.pdf", FileFormat.PDF);

如何在表格的单元格中嵌入另一个表格?

A:表格是一个更简单的网格。在网格中,您可以操作每个单元格,为每个单元格设置不同的样式并嵌入另一个网格。所以你可以使用网格来完成这项工作。完整代码:

PdfDocument document = new PdfDocument();

PdfPageBase page = document.Pages.Add(PdfPageSize.A4);


PdfGrid grid = new PdfGrid();

grid.Columns.Add(1);

grid.Columns[0].Width = page.Canvas.ClientSize.Width;

PdfGridRow row0 = grid.Rows.Add();

row0.Cells[0].Value = "This is the first row.";

row0.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

PdfGridRow row1 = grid.Rows.Add();

PdfLayoutResult result=grid.Draw(page, new PointF(0, 20));


PdfGrid grid2 = new PdfGrid();

grid2.Columns.Add(2);

PdfGridRow newrow = grid2.Rows.Add();

grid2.Columns[0].Width = grid.Columns[0].Width / 2;

grid2.Columns[1].Width = grid.Columns[0].Width / 2;

newrow.Cells[0].Value = "This is row two column one.";

newrow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

newrow.Cells[1].Value = "This is row two column two.";

newrow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);


//assign grid2 to row1

row1.Cells[0].Value = grid2;


//drwa grid2

result = grid2.Draw(page, new PointF(0, result.Bounds.Location.Y + result.Bounds.Height));

document.SaveToFile("result.pdf");

如何合并网格中的单元格?

A:Spire.PDF 为您提供名为 RowSpan 和 ColumnSpan 的属性来合并单元格。完整代码:

PdfDocument doc = new PdfDocument();

PdfPageBase page = doc.Pages.Add();


PdfGrid grid = new PdfGrid();

grid.Columns.Add(5);

float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);

for (int i = 0; i < grid.Columns.Count; i++)

{

grid.Columns[i].Width = width * 0.20f;

}

PdfGridRow row0 = grid.Rows.Add();

PdfGridRow row1 = grid.Rows.Add();


row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);

row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true);


row0.Cells[0].Value = "Corporation";


//merge with the downside cell

row0.Cells[0].RowSpan = 2;


row0.Cells[1].Value = "B&K Undersea Photo";

row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);


//merge with the right cell

row0.Cells[1].ColumnSpan = 3;


row0.Cells[4].Value = "World";

row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);

row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen;


row1.Cells[1].Value = "Diving International Unlimited";

row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);

row1.Cells[1].ColumnSpan = 4;


grid.Draw(page, new PointF(0, 0));


doc.SaveToFile("result.pdf");

如何在 PDF 文件中添加签名?

A:首先,使用类 PdfCertificate 创建一个证书实例。证书实例将用于创建 PdfSignature 实例。然后使用类 PdfSignature 创建一个签名实例。并且您需要设置签名实例的属性。完整代码:

PdfDocument doc = new PdfDocument();

doc.LoadFromFile("sample.pdf");

PdfCertificate cert = new PdfCertificate("Demo.pfx", "e-iceblue");


//add signature to every page of PDF file

foreach (PdfPageBase page in doc.Pages)

{

PdfSignature signature = new PdfSignature(page.Document, page, cert, "demo");

signature.ContactInfo = "Harry";

signature.Certificated = true;

signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill;

}


doc.SaveToFile("result.pdf");

如何重新排列 PDF 文件的页面?

A : 请调用 ReArrange 方法。该方法将一个 int 数组作为参数。int 数组表示页面的新顺序。完整代码:

PdfDocument document = new PdfDocument();


//sample.pdf has four pages

document.LoadFromFile("sample.pdf");


//rearrange the pages of the PDF file

int[] range = new int[] { 0, 2, 1, 3 };

document.Pages.ReArrange(range);

document.SaveToFile("result.pdf");

如何添加图片水印?

A : Spire.PDF 不直接支持图片水印。但是您可以将图像设置为 BackgroundImage。如果图像足够大,BackgroundImage 就像图像水印一样。完整代码:

PdfDocument document = new PdfDocument();

PdfPageBase page = document.Pages.Add(PdfPageSize.A4);

page.Canvas.DrawString("This is a demo about image watermark!",

new PdfFont(PdfFontFamily.Helvetica, 25f),

new PdfSolidBrush(Color.Green),

10, 40);


//set image as BackgroundImage to make image watermark

Image img = Image.FromFile("scene.bmp");

page.BackgroundImage = img;

document.SaveToFile("result.pdf");

如何在所有页面上重复标题?

A : PdfDocumentTemplate 中的内容将适用于 PDF 文件的每一页。您所要做的就是创建一个将标题添加到 PdfDocumentTemplate 的方法。然后标题将添加到每个页面。完整代码:

static void Main(string[] args)

{

PdfDocument doc = new PdfDocument();

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

PdfMargins margin = new PdfMargins();

margin.Top = unitCvtr.ConvertUnits(3.0f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

margin.Bottom = margin.Top;

margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);

margin.Right = margin.Left;



// create three page

PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);

page = doc.Pages.Add(PdfPageSize.A4, margin);

page = doc.Pages.Add(PdfPageSize.A4, margin);


//apply template

SetDocumentTemplate(doc, PdfPageSize.A4, margin);

doc.SaveToFile("result.pdf");


}

//method to add header to every page

private static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)

{

PdfPageTemplateElement leftSpace

= new PdfPageTemplateElement(margin.Left, pageSize.Height);

doc.Template.Left = leftSpace;


PdfPageTemplateElement topSpace

= new PdfPageTemplateElement(pageSize.Width, margin.Top);

topSpace.Foreground = true;

doc.Template.Top = topSpace;


//draw header label

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic));

PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

String label = "Demo about Header Repeating";


//set the header style

SizeF size = font.MeasureString(label, format);

float y = topSpace.Height - font.Height - 40;

PdfPen pen = new PdfPen(Color.Black, 0.75f);

topSpace.Graphics.SetTransparency(0.5f);

topSpace.Graphics.DrawLine(pen, margin.Left - 30, y, pageSize.Width - margin.Right + 30, y);

y = y - 1 - size.Height;

topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format);


PdfPageTemplateElement rightSpace

= new PdfPageTemplateElement(margin.Right, pageSize.Height);

doc.Template.Right = rightSpace;


PdfPageTemplateElement bottomSpace

= new PdfPageTemplateElement(pageSize.Width, margin.Bottom);

bottomSpace.Foreground = true;

doc.Template.Bottom = bottomSpace;

}

相关文章
vscode 使用markdown 转PDF的常见问题 之插件安装
vscode 使用markdown 转PDF的常见问题 之插件安装
472 0
|
Web App开发 JavaScript 前端开发
web在线编辑word,excel,pdf插件-----WebOffice 文档控件API
目    录       一、工作原理...5 1.1         开发流程...5 1.2         WEB页面调用控件:.6 二、接口说明...7 2.1         接口...7 2.1.1     AboutBox.7 2.1.2     AcceptAllRevisions.7 2.1.3     CloseDoc.7 2.1.4  
9826 75
|
2月前
|
C#
【PDF提取内容改名】批量提取PDF指定区域内容重命名PDF文件,PDF自动提取内容命名的方案和详细步骤
本工具可批量提取PDF中的合同编号、日期、发票号等关键信息,支持PDF自定义区域提取并自动重命名文件,适用于合同管理、发票处理、文档归档和数据录入场景。基于iTextSharp库实现,提供完整代码示例与百度、腾讯网盘下载链接,助力高效处理PDF文档。
412 40
|
5月前
|
存储 JSON API
如何将 Swagger 文档导出为 PDF 文件
你会发现自己可能需要将 Swagger 文档导出为 PDF 或文件,以便于共享和存档。在这篇博文中,我们将指导你完成将 Swagger 文档导出为 PDF 格式的过程。
|
2月前
|
编译器 Python
如何利用Python批量重命名PDF文件
本文介绍了如何使用Python提取PDF内容并用于文件重命名。通过安装Python环境、PyCharm编译器及Jupyter Notebook,结合tabula库实现PDF数据读取与处理,并提供代码示例与参考文献。
|
4月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
162 10
|
3月前
|
数据采集 存储 API
Python爬虫结合API接口批量获取PDF文件
Python爬虫结合API接口批量获取PDF文件
|
7月前
|
人工智能 编解码 文字识别
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具
OCRmyPDF 是一款开源命令行工具,专为将扫描的 PDF 文件转换为可搜索、可复制的文档。支持多语言、图像优化和多核处理。
878 17
OCRmyPDF:16.5K Star!快速将 PDF 文件转换为可搜索、可复制的文档的命令行工具
|
7月前
|
文字识别 Serverless 开发工具
【全自动改PDF名】批量OCR识别提取PDF自定义指定区域内容保存到 Excel 以及根据PDF文件内容的标题来批量重命名
学校和教育机构常需处理成绩单、报名表等PDF文件。通过OCR技术,可自动提取学生信息并录入Excel,便于统计分析和存档管理。本文介绍使用阿里云服务实现批量OCR识别、内容提取、重命名及导出表格的完整步骤,包括开通相关服务、编写代码、部署函数计算和设置自动化触发器等。提供Python示例代码和详细操作指南,帮助用户高效处理PDF文件。 链接: - 百度网盘:[链接](https://pan.baidu.com/s/1mWsg7mDZq2pZ8xdKzdn5Hg?pwd=8866) - 腾讯网盘:[链接](https://share.weiyun.com/a77jklXK)
816 5
|
8月前
|
机器学习/深度学习 人工智能 文字识别
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式
Zerox 是一款开源的本地化高精度OCR工具,基于GPT-4o-mini模型,支持PDF、DOCX、图片等多种格式文件,能够零样本识别复杂布局文档,输出Markdown格式结果。
696 4
Zerox:AI驱动的万能OCR工具,精准识别复杂布局并输出Markdown格式,支持PDF、DOCX、图片等多种文件格式