高级功能

将 Pdf 发送到浏览器以下载

有时在开发 ASP.NET 应用程序时,您需要将 PDF 文件发送到网页浏览器以供下载,而无需物理保存它们。为了实现这一点,您可以在生成 PDF 文档后将其保存到 MemoryStream 对象中,并将该 MemoryStream 中的字节传递给 Response 对象。这样做将使浏览器下载生成的 PDF 文档。

以下代码片段演示了上述功能:

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-.NET
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World"));
MemoryStream ms = new MemoryStream();
doc.Save(ms);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = "UTF-8";
Response.AddHeader("content-length", ms.Length.ToString());
Response.AddHeader("content-disposition", String.Format("attachment;filename=TestDocument.pdf", "FileName"));
Response.ContentType = "application/pdf"; Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();

从 PDF 文件中提取嵌入文件

在处理 PDF 格式文件的高级功能方面,Aspose.PDF 表现突出。它提取嵌入文件的方式比其他提供此功能的工具要好得多。

使用 Aspose.PDF for .NET,您可以有效地提取任何嵌入文件,无论是嵌入字体、图像、视频还是音频。 使用 Aspose.PDF for .NET,您可以高效地提取任何嵌入式文件,包括嵌入式字体、图像、视频或音频。

以下代码片段提取 PDF 文件中的所有嵌入式文件:

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
// Load source PDF file
Document doc = new Document(dataDir + "input.pdf");
// Save output in XML format
doc.Save(dataDir + "PDFToXML_out.xml", SaveFormat.MobiXml);

使用 Latex 脚本添加数学表达式

使用 Aspose.PDF,您可以使用 latex 脚本在 PDF 文档中添加数学表达式/公式。以下示例展示了如何以两种不同方式使用此功能,在表格单元格中添加数学公式:

无前导代码和文档环境

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create a new Document Object
Document doc = new Document();
// Add Page in Pages Collection
Page page = doc.Pages.Add();
// Create a Table
Table table = new Table();
// Add a row into Table
Row row = table.Rows.Add();
// Add Cell with Latex Script to add methematical expressions/formulae
string latexText1 = "$123456789+\\sqrt{1}+\\int_a^b f(x)dx$";
Cell cell = row.Cells.Add();
cell.Margin = new MarginInfo { Left = 20, Right = 20, Top = 20, Bottom = 20 };
// Second LatexFragment constructor bool parameter provides LaTeX paragraph indents elimination.
LatexFragment ltext1 = new LatexFragment(latexText1, true);
cell.Paragraphs.Add(ltext1);
// Add table inside page
page.Paragraphs.Add(table);
// Save the document
doc.Save(dataDir + "LatextScriptInPdf_out.pdf");

带前导代码和文档环境

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Text();
// Create a new Document Object
Document doc = new Document();
// Add Page in Pages Collection
Page page = doc.Pages.Add();
// Create a Table
Table table = new Table();
// Add a row into Table
Row row = table.Rows.Add();
// Add Cell with Latex Script to add methematical expressions/formulae
string latexText2 = @"\documentclass{article}
\begin{document}
Latex and the document class will normally take care of page layout issues for you. For submission to an academic publication, this entire topic will be out
\end{document}";
Cell cell = row.Cells.Add();
cell.Margin = new MarginInfo { Left = 20, Right = 20, Top = 20, Bottom = 20 };
HtmlFragment text2 = new HtmlFragment(latexText2);
cell.Paragraphs.Add(text2);
// Add table inside page
page.Paragraphs.Add(table);
// Save the document
doc.Save(dataDir + "LatextScriptInPdf2_out.pdf");

支持 Latex 标签

支持 Latex 标签

align 环境在 amsmath 包中定义,proof 环境在 amsthm 包中定义。因此,您需要在文档前言中使用 \usepackage 命令指定这些包。这意味着您必须将 LaTeX 文本包含在文档环境中,如下面的代码示例所示。

// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
var dataDir = RunExamples.GetDataDir_AsposePdf_Text();
var s = @"
\usepackage{amsmath,amsthm}
\begin{document}
\begin{proof} The proof is a follows:
\begin{align}
(x+y)^3&=(x+y)(x+y)^2
(x+y)(x^2+2xy+y^2)\\
&=x^3+3x^2y+3xy^3+x^3.\qedhere
\end{align}
\end{proof}
\end{document}";
var doc = new Document();
var page = doc.Pages.Add();
var latex = new LatexFragment(s);
page.Paragraphs.Add(latex);
doc.Save(dataDir + "Script_out.pdf");