原創|使用教程|編輯:龔雪|2014-07-23 09:46:21.000|閱讀 606 次
概述: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@fc6vip.cn
文章轉載自:慧都控件網