// ExStart:PrefixForFontsHelper private static string CustomResourcesProcessing(SaveOptions.ResourceSavingInfo resourceSavingInfo) { //----------------------------------------------------------------------------- // It's just example of possible realization of cusstom processing of resources // referenced in result HTML //----------------------------------------------------------------------------- // 1) In this case we need only do something special // with fonts, so let's leave processing of all other resources // to converter itself if (resourceSavingInfo.ResourceType != SaveOptions.NodeLevelResourceType.Font) { resourceSavingInfo.CustomProcessingCancelled = true; return(""); } // If supplied font resource, process it ourselves // 1) Write supplied font with short name to desired folder // You can easily do anythings - it's just one of realizations _fontNumberForUniqueFontFileNames++; string shortFontFileName = (_fontNumberForUniqueFontFileNames.ToString() + Path.GetExtension(resourceSavingInfo.SupposedFileName)); string outFontPath = _desiredFontDir + "\\" + shortFontFileName; System.IO.BinaryReader fontBinaryReader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(outFontPath, fontBinaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); // 2) Return the desired URI with which font will be referenced in CSSes string fontUrl = "http://localhost:255/document-viewer/GetFont/" + shortFontFileName; return(fontUrl); }
private static string CustomSaveOfFontsAndImages(SaveOptions.ResourceSavingInfo resourceSavingInfo) { System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream); byte[] resourceAsBytes = reader.ReadBytes((int)resourceSavingInfo.ContentStream.Length); if (resourceSavingInfo.ResourceType == SaveOptions.NodeLevelResourceType.Font) { Console.WriteLine("Font processed with handler. Length of content in bytes is " + resourceAsBytes.Length.ToString()); // Here You can put code that will save font to some storage, f.e database MemoryStream targetStream = new MemoryStream(); targetStream.Write(resourceAsBytes, 0, resourceAsBytes.Length); } else if (resourceSavingInfo.ResourceType == SaveOptions.NodeLevelResourceType.Image) { Console.WriteLine("Image processed with handler. Length of content in bytes is " + resourceAsBytes.Length.ToString()); // Here You can put code that will save image to some storage, f.e database MemoryStream targetStream = new MemoryStream(); targetStream.Write(resourceAsBytes, 0, resourceAsBytes.Length); } // We should return URI bt which resource will be referenced in CSS(for font) // or HTML(for images) // This is very siplistic way - here we just return file name or resource. // You can put here some URI that will include ID of resource in database etc. // - this URI will be added into result CSS or HTML to refer the resource return(resourceSavingInfo.SupposedFileName); }
private static string Strategy_11_CUSTOM_SAVE_OF_FONTS_AND_IMAGES(SaveOptions.ResourceSavingInfo resourceSavingInfo) { if (!Directory.Exists(_folderForReferencedResources_34748)) { Directory.CreateDirectory(_folderForReferencedResources_34748); } string path = _folderForReferencedResources_34748 + Path.GetFileName(resourceSavingInfo.SupposedFileName); // The first path of this method is for saving fonts System.IO.BinaryReader contentReader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(path, contentReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); string urlThatWillBeUsedInHtml = "file:///" + _folderForReferencedResources_34748.Replace(@"\", "/") + Path.GetFileName(resourceSavingInfo.SupposedFileName); return(urlThatWillBeUsedInHtml); }
// ExStart:PrefixForURLsHelper private static string Custom_processor_of_embedded_images(SaveOptions.ResourceSavingInfo resourceSavingInfo) { // ____________________________________________________________________________ // This sample method saving strategy method saves image-files in some folder // (including raster image files that are exctracted from that SVGs) // Then it returns specific custom artificial path // to be used as value of 'src' or 'data' relevant attribute in generated host-SVG(or HTML) // ____________________________________________________________________________ //--------------------------------------------------------- // 1) All other files(f.e. fonts) will be processed with converter itself cause for them flag // resourceSavingInfo.CustomProcessingCancelled is set to 'true' //--------------------------------------------------------- if (!(resourceSavingInfo is HtmlSaveOptions.HtmlImageSavingInfo)) { resourceSavingInfo.CustomProcessingCancelled = true; return(""); } //--------------------------------------------------------- // 1) Create target folder if not created yet //--------------------------------------------------------- string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); string outDir = dataDir + @"output\36297_files\"; string outPath = outDir + Path.GetFileName(resourceSavingInfo.SupposedFileName); if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } //--------------------------------------------------------- // 3) Write supplied image to that folder //--------------------------------------------------------- System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(dataDir, reader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); //--------------------------------------------------------- // 4) Return customized specific URL to be used to refer // just created image in parent SVG (or HTML) //--------------------------------------------------------- HtmlSaveOptions.HtmlImageSavingInfo asHtmlImageSavingInfo = resourceSavingInfo as HtmlSaveOptions.HtmlImageSavingInfo; if (asHtmlImageSavingInfo.ParentType == HtmlSaveOptions.ImageParentTypes.SvgImage) { return("http://localhost:255/" + resourceSavingInfo.SupposedFileName); } else { return("http://localhost:GetImage/imageID=" + resourceSavingInfo.SupposedFileName); } }
// ExStart:SpecifyPrefixForImagesHelper private static string SavingTestStrategy_1(SaveOptions.ResourceSavingInfo resourceSavingInfo) { // This sample method saving strategy method saves only svg-files in some folder and returns specific path // To be used as value of 'src' or 'data' relevant attribute in generated HTML // All other files will be processed with converter itself cause for them flag // ResourceSavingInfo.CustomProcessingCancelled is set to 'true' // The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); if (!(resourceSavingInfo is HtmlSaveOptions.HtmlImageSavingInfo)) { resourceSavingInfo.CustomProcessingCancelled = true; return(""); } HtmlSaveOptions.HtmlImageSavingInfo asHtmlImageSavingInfo = (HtmlSaveOptions.HtmlImageSavingInfo)resourceSavingInfo; if ((asHtmlImageSavingInfo.ImageType != HtmlSaveOptions.HtmlImageType.Svg) && (asHtmlImageSavingInfo.ImageType != HtmlSaveOptions.HtmlImageType.ZippedSvg) ) { resourceSavingInfo.CustomProcessingCancelled = true; return(""); } string outFile = dataDir + "SpecifyImages_out.html"; string imageOutFolder = Path.GetFullPath(Path.GetDirectoryName(outFile) + @"\35956_svg_files\"); // ImageOutFolder="C:\AsposeImagesTests\"; if (!Directory.Exists(imageOutFolder)) { Directory.CreateDirectory(imageOutFolder); } string outPath = imageOutFolder + Path.GetFileName(resourceSavingInfo.SupposedFileName); System.IO.BinaryReader reader = new BinaryReader(resourceSavingInfo.ContentStream); System.IO.File.WriteAllBytes(outPath, reader.ReadBytes((int)resourceSavingInfo.ContentStream.Length)); return("/document-viewer/GetImage?path=CRWU-NDWAC-Final-Report-12-09-10-2.pdf&name=" + resourceSavingInfo.SupposedFileName); }