专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C# PDF操作之-PDF转HTML

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:59:05       共计:3624 浏览
特别说明:需引用Aspose.PDF.dll

代码案例:

    using System.IO;
    using System;
    using Aspose.Pdf;
     
    namespace Aspose.Pdf.Examples.CSharp.AsposePDF.DocumentConversion
    {
        public class PDFToHTML
        {
            public static void Run()
            {
                // ExStart:PDFToHTML
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                // Open the source PDF document
                Document pdfDocument = new Document(dataDir + "PDFToHTML.pdf");
     
                // Save the file into MS document format
                pdfDocument.Save(dataDir + "output_out.html", SaveFormat.Html);
                // ExEnd:PDFToHTML
            }
            public static void ExcludeFontResources()
            {
                // ExStart:ExcludeFontResources
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                string inFile = dataDir + "PDFToHTML.pdf";
                string outMainHtmlFile = dataDir + "ExcludeFontResources.pdf";
                // Initialize htmlOptions
                HtmlSaveOptions htmlOptions = new HtmlSaveOptions
                {
                    ExplicitListOfSavedPages = new[] { 1 },
                    FixedLayout = true,
                    CompressSvgGraphicsIfAny = false,
                    SaveTransparentTexts = true,
                    SaveShadowedTextsAsTransparentTexts = true,
                    //FontSavingMode = HtmlSaveOptions.FontSavingModes.DontSave,
                    ExcludeFontNameList = new[] { "ArialMT", "SymbolMT" },
                    DefaultFontName = "Comic Sans MS",
                    UseZOrder = true,
                    LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss,
                    PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.NoEmbedding,
                    RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground,
                    SplitIntoPages = false
                };
     
                Document pdfDocument = new Document(inFile);
                // Save
                pdfDocument.Save(outMainHtmlFile, htmlOptions);
                // ExEnd:ExcludeFontResources
            }
            public static void MultiPageHTML()
            {
                // ExStart:MultiPageHTML
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                // Open the source PDF document
                Document pdfDocument = new Document(dataDir + "PDFToHTML.pdf");
     
                // Instantiate HTML SaveOptions object
                HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
     
                // Specify to split the output into multiple pages
                htmlOptions.SplitIntoPages = true;
     
                // Save the document
                pdfDocument.Save(@"MultiPageHTML_out.html", htmlOptions);
                // ExEnd:MultiPageHTML
            }
            public static void SaveSVGFiles()
            {
                // ExStart:SaveSVGFiles
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                // Load the PDF file
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
     
                // Instantiate HTML save options object
                HtmlSaveOptions newOptions = new HtmlSaveOptions();
     
                // Specify the folder where SVG images are saved during PDF to HTML conversion
                newOptions.SpecialFolderForSvgImages = dataDir;
     
                // Save the output file
                doc.Save(dataDir + "SaveSVGFiles_out.html", newOptions);
                // ExEnd:SaveSVGFiles
            }
            public static void CompressSVGImages()
            {
                
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                // Load the PDF file
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
                // ExStart:CompressSVGImages
                // Create HtmlSaveOption with tested feature
                HtmlSaveOptions newOptions = new HtmlSaveOptions();
     
                // Compress the SVG images if there are any
                newOptions.CompressSvgGraphicsIfAny = true;
                // ExEnd:CompressSVGImages
                // Save the output file
                doc.Save(dataDir + "CompressSVGImages_out.html", newOptions);
                
            }
            public static void SpecifyingImageFolder()
            {
                
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                // Load the PDF file
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
                // ExStart:SpecifyingImageFolder
                // Create HtmlSaveOption with tested feature
                HtmlSaveOptions newOptions = new HtmlSaveOptions();
     
                // Specify the separate folder to save images
                newOptions.SpecialFolderForAllImages = dataDir;
                // ExEnd:SpecifyingImageFolder
                // Save the output file
                doc.Save(dataDir + "SpecifyingImageFolder_out.html", newOptions);
                
            }
            public static void CreateSubsequentFiles()
            {
                // ExStart:CreateSubsequentFiles
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
                
                HtmlSaveOptions options = new HtmlSaveOptions();
                // This is the tested setting
                options.HtmlMarkupGenerationMode = HtmlSaveOptions.HtmlMarkupGenerationModes.WriteOnlyBodyContent;
                options.SplitIntoPages = true;
     
                doc.Save(dataDir + "CreateSubsequentFiles_out.html", options);
                // ExEnd:CreateSubsequentFiles
            }
            public static void TransparentTextRendering()
            {
                // ExStart:TransparentTextRendering
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
                HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
                htmlOptions.SaveShadowedTextsAsTransparentTexts = true;
                htmlOptions.SaveTransparentTexts = true;
                doc.Save(dataDir + "TransparentTextRendering_out.html", htmlOptions);
                // ExEnd:TransparentTextRendering
            }
            public static void LayersRendering()
            {
                // ExStart:LayersRendering
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                Document doc = new Document(dataDir + "PDFToHTML.pdf");
                // Instantiate HTML SaveOptions object
                HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
     
                // Specify to render PDF document layers separately in output HTML
                htmlOptions.ConvertMarkedContentToLayers = true;
     
                // Save the document
                doc.Save(dataDir + "LayersRendering_out.html", htmlOptions);
                // ExEnd:LayersRendering
            }
            public static void CreatingHtmlWithFullContentWidth()
            {
                //ExStart: CreatingHtmlWithFullContentWidth
                // The path to the documents directory.
                string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion();
     
                HtmlSaveOptions saveOptions = new HtmlSaveOptions();
                saveOptions.FixedLayout = (false);
                saveOptions.FlowLayoutParagraphFullWidth = true;
                Document doc = new Document(dataDir + "FlowLayoutParagraphFullWidth.Pdf");
                doc.Save(dataDir + "FlowLayoutParagraphFullWidth_out.html", saveOptions);
                //ExEnd: CreatingHtmlWithFullContentWidth
            }
            public static void CenterAlignText()
            {
                //ExStart: CenterAlignText
                HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
     
                // init MarginPartStyle with margin in 20 points
                var commonMargin = new SaveOptions.MarginPartStyle(20);
                // init MarginPartStyle with margin value auto
                var autoMargin = new SaveOptions.MarginPartStyle(true);
     
                // set commonMargin to every page side
                htmlOptions.PageMarginIfAny = new HtmlSaveOptions.MarginInfo(commonMargin);
     
                // set horizontal page align to center
                htmlOptions.PageMarginIfAny.LeftMarginIfAny = autoMargin;
                htmlOptions.PageMarginIfAny.RightMarginIfAny = autoMargin;
                //ExEnd: CenterAlignText
            }
        }
    }
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:C# 如何将PDF转为Image、SVG、XPS、HTML | ·下一条:C#PDF转Word

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有