原创|使用教程|编辑:龚雪|2014-07-23 09:46:21.000|阅读 562 次
概述:Aspose.Pdf for .NET迎来了一次重大更新,重点是文件格式转换的优化。此外还引入了一个全新功能---单独提取字体。具体功能以及实现方法请看详细内容。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
在最新版本的 Aspose.Pdf for .NET中,我们的重点是文件格式转换的优化。这其中包括 PDF to HTML, SVG to PDF, PDF to Excel, PDF to DOC, Image to PDF 以及 PDF to Image。
Aspose.Pdf for .NET 9.4.0 还引入了从HTML, SVG和其他来源单独提取字体的功能。下面这些代码片段就展示了如何实现这个功能:
public void PDFNEWNET_36524_cacheFonts_Sample() { Helper.SetLicense(); string inFile = TestSettings.GetInputFile("36524.pdf"); HTMLMultithreadingTester_FontCache_Sample tester = new HTMLMultithreadingTester_FontCache_Sample(inFile); tester.Run(); } class HTMLMultithreadingTester_FontCache_Sample { public HTMLMultithreadingTester_FontCache_Sample(string inputFile) { inFile = inputFile; fileNameOnly = Path.GetFileNameWithoutExtension(inFile); testOut = Path.Combine(TestSettings.TestOutput, fileNameOnly); // Delete previous output directories if (Directory.Exists(testOut)) { string[] files = Directory.GetFiles(testOut, "*.*", SearchOption.AllDirectories); foreach (string file in files) { File.Delete(file); } string[] directories = Directory.GetDirectories(testOut, "*.*", SearchOption.AllDirectories); foreach (string dir in directories) { Directory.Delete(dir); } } Directory.CreateDirectory(testOut); } string inFile; string testOut; string fileNameOnly; public Dictionary outFileNames = new Dictionary(); public void Run() { Helper.SetLicense(); // Folder that contains pre-generated cached fonts // All the fonts of the document will be placed to this folder and will be passed to each page conversion procedure string fontCacheFolder = Path.Combine(testOut, fileNameOnly + "_fonts_preSaved\\"); string cacheFontFileTemplate = Path.Combine(fontCacheFolder, "font{0}.ttf"); // Folder that will contain fonts as a result of the conversion procedure string fontOutFolder = Path.GetFullPath(Path.Combine(testOut, fileNameOnly + "_fonts\\")); // Create our folders Directory.CreateDirectory(fontCacheFolder); Directory.CreateDirectory(fontOutFolder); System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); Aspose.Pdf.Document document = new Aspose.Pdf.Document(inFile); int pageCount = document.Pages.Count; // Find all the fonts of the document FontAbsorber fa = new FontAbsorber(); fa.Visit(document); FontCollection fc = fa.Fonts; List fontFiles = new List(); // Save all the fonts in the cache folder int fontNum = 0; foreach (Pdf.Text.Font font in fc) { string cacheFontFile = string.Format(cacheFontFileTemplate, fontNum++); using (Stream fileStream = File.OpenWrite(cacheFontFile)) { font.Save(fileStream); } fontFiles.Add(cacheFontFile); } int pageNumber = 0; // Split the document to separate pages to convert them in parallel foreach (Page pdfPage in document.Pages) { using (Document newDocument = new Document()) { newDocument.Pages.Add(pdfPage); newDocument.Save(Path.Combine(testOut, String.Format(fileNameOnly + "_page{0}.pdf", pageNumber))); } pageNumber++; } document.Dispose(); // Run conversion threads Thread[] threads = new Thread[pageCount]; ThreadParam[] threadParams = new ThreadParam[pageCount]; for (int i = 0; i < pageCount; i++) { threads[i] = new Thread(new ParameterizedThreadStart(Worker)); threadParams[i] = new ThreadParam(i); threadParams[i].fontFiles = fontFiles; threads[i].Start(threadParams[i]); } // Wait threads to finish for (int i = 0; i < pageCount; i++) { threads[i].Join(); } sw.Stop(); Console.WriteLine(sw.Elapsed.TotalSeconds); } private void Worker(Object param) { ThreadParam threadParam = (ThreadParam)param; Console.Out.WriteLine("started: " + threadParam.PageNum); try { using (Aspose.Pdf.Document pdfPageDocument = new Aspose.Pdf.Document(Path.Combine(testOut, String.Format(fileNameOnly + "_page{0}.pdf", threadParam.PageNum)))) { Aspose.Pdf.HtmlSaveOptions htmlOptions = new Aspose.Pdf.HtmlSaveOptions(); htmlOptions.SplitIntoPages = false; htmlOptions.FixedLayout = true; htmlOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.AlwaysSaveAsTTF; htmlOptions.CompressSvgGraphicsIfAny = false; htmlOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(CacheFontsStrategy); // addtthe cached fonts as a font sources foreach (string fontFile in threadParam.fontFiles) { htmlOptions.FontSources.Add(new FileFontSource(fontFile)); } htmlOptions.RasterImagesSavingMode = Aspose.Pdf.HtmlSaveOptions.RasterImagesSavingModes.AsExternalPngFilesReferencedViaSvg; string outputFileName = Path.GetFullPath(Path.Combine(testOut, String.Format(fileNameOnly + "_page{0}.html", threadParam.PageNum))); pdfPageDocument.Save(outputFileName, htmlOptions); outFileNames[threadParam.PageNum] = outputFileName; } } catch (Exception ex) { threadParam.isSuccess = false; Console.Out.WriteLine(ex.ToString()); } } class ThreadParam { public ThreadParam(int pageNum) { this.PageNum = pageNum; this.isSuccess = true; } public int PageNum; public List fontFiles; public bool isSuccess; } static object resourceSavingSync = new object(); /// /// Resource saving callback that saves fonts into output folder and builds css links to the fonts /// private string CacheFontsStrategy(SaveOptions.ResourceSavingInfo resourceSavingInfo) { // The callback is performed in parallel threads, so synchronization must be implemented lock (resourceSavingSync) { string fontsFolder = Path.GetFullPath(Path.Combine(testOut, fileNameOnly + "_fonts\\")); if (!Directory.Exists(fontsFolder)) Directory.CreateDirectory(fontsFolder); // First path of this method is for saving of font if (resourceSavingInfo.ResourceType == SaveOptions.NodeLevelResourceType.Font) { string outFontFile = fontsFolder + Path.GetFileName(resourceSavingInfo.SupposedFileName); System.IO.BinaryReader fontBinaryReader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(outFontFile, fontBinaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); string fontUrl = "../" + fileNameOnly + "_fonts/" + resourceSavingInfo.SupposedFileName; return fontUrl; } resourceSavingInfo.CustomProcessingCancelled = true; return null; } } }
当上述的代码执行时,字体只能独立于输出HTML文件创建一次。
在最近的某次更新中我们引入了PDF to HTML导出时确认图片格式的功能。为了实现该功能,我们还介绍了一个新的类namedHtmlSaveOptions.RasterImagesSavingModes。下列代码片段展示了如何选择目标图像格式:
Aspose.Pdf.Document doc = new Document(@"c:\pdftest\36009.pdf"); HtmlSaveOptions options = new HtmlSaveOptions(); options.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground; // Next line is just to make view best for max amount of browsers // You can coment it out if You will options.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats; doc.Save(@"c:\pdftest\36009.html", options);
执行上述代码后,输出文件夹不会包含任何SVG文件, 只有PNG格式文件(每页一个PNG格式文件)。
Document doc = new Document("Original.pdf"); ExcelSaveOptions options = new ExcelSaveOptions(); // Set this property to true options.MinimizeTheNumberOfWorksheets = true; doc.Save("output.xls", options);
为实现该功能,这里引入了一个新的计算方法——ColumnAdjustment ,包含了值 AutoFitToContent:
string outFile = "36916.pdf"; // Added document Document doc = new Document(); Page page = doc.Pages.Add(); // Create a table object and add it to the paragraphs collection of the section Table tab1 = new Table(); page.Paragraphs.Add(tab1); // Set the column widths and default cell border of the table tab1.ColumnAdjustment = ColumnAdjustment.AutoFitToContent; tab1.ColumnWidths = "50 50 50"; tab1.DefaultCellBorder = new BorderInfo(BorderSide.All, 1F); // Prepare an array of string values to be added to table string[] data = new string[] { "Sample Text", "8.4", "Its test to set column width as per contnents" }; // Import the contents of the array created in above step tab1.ImportArray(data, 0, 0, true); // Save the resultant PDF doc.Save(outFile);
鼠标悬停在图片或文本上,可以弹出窗口注解了:
/*Declaring of parameters*/ // Unique name of annotation string name = "IMDB0145487"; // Title of popup window string title = "Spider-Man"; // Description that be in popup window string comment = "Movie produced in 2002; run length: 121"; // Path to image for that popup window will appeared on mouse over string imagePath = (TestSettings.GetInputFile("36228.jpg")); // Position of image on page of document Aspose.Pdf.Rectangle imageRect = new Aspose.Pdf.Rectangle(2, 700, 97, 840); // Position of popup on page of document Aspose.Pdf.Rectangle popupRect = new Aspose.Pdf.Rectangle(90, 610, 235, 710); /*Document creating*/ Document doc = new Document(); doc.Pages.Add(); // Page for adding of image Page page = doc.Pages[1]; /*Add image on page*/ // Load image into stream FileStream imageStream = new FileStream(imagePath, FileMode.Open); // Add image to Images collection of Page Resources page.Resources.Images.Add(imageStream); // Using GSave operator: this operator saves current graphics state page.Contents.Add(new Operator.GSave()); // Create Rectangle and Matrix objects Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { imageRect.URX - imageRect.LLX, 0, 0, imageRect.URY - imageRect.LLY, imageRect.LLX, imageRect.LLY }); // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed page.Contents.Add(new Operator.ConcatenateMatrix(matrix)); XImage ximage = page.Resources.Images[page.Resources.Images.Count]; // Using Do operator: this operator draws image page.Contents.Add(new Operator.Do(ximage.Name)); // Using GRestore operator: this operator restores graphics state page.Contents.Add(new Operator.GRestore()); /*Add text annotation*/ TextAnnotation text = new TextAnnotation(page, imageRect); text.Name = name; text.Title = title; text.Contents = comment; // This flags must be raised to suppress showing of annotation icon text.Flags = AnnotationFlags.NoView|AnnotationFlags.ReadOnly; page.Annotations.Add(text); /*Add popup annotation*/ PopupAnnotation popup = new PopupAnnotation(page, popupRect); page.Annotations.Add(popup); /*Link text and popup annotations*/ text.Popup = popup; popup.Parent = text; /*Add button*/ Field field = new ButtonField(page, imageRect); doc.Form.Add(field); /*Set ButtonField actions*/ string fieldName = field.PartialName; string openScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = true; var w = this.getField('" + fieldName + "'); w.setFocus();"; string closeScript = "var t = this.getAnnot(this.pageNum, '" + name + "'); t.popupOpen = false;"; field.Actions.OnEnter = new JavascriptAction(openScript); field.Actions.OnExit = new JavascriptAction(closeScript); /*Save document*/ doc.Save(TestSettings.GetOutputFile("36228.pdf"));
除了上述新功能外,本次更新还包含了近90个问题修复,所以说Aspose.Pdf for .NET 9.4.0是一个重要版本。相比之前的所有版本,我们称之为“更好的spose.Pdf for .NET API ” 。赶快下载最新试用版吧Aspose.Pdf for .NET 9.4.0.
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn
文章转载自:慧都控件网