Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
이 문서에서는 C#을 사용하여 PDF를 HTML로 변환하는 방법을 설명합니다. 다음 주제를 다룹니다.
다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와도 작동합니다.
Aspose.PDF for .NET는 다양한 파일 형식을 PDF 문서로 변환하고 PDF 파일을 다양한 출력 형식으로 변환하는 많은 기능을 제공합니다. 이 문서에서는 PDF 파일을 HTML로 변환하는 방법에 대해 설명합니다. Aspose.PDF for .NET는 InLineHtml 접근 방식을 사용하여 HTML 파일을 PDF 형식으로 변환하는 기능을 제공합니다. PDF 파일을 HTML 형식으로 변환하는 기능에 대한 많은 요청이 있었으며 이 기능을 제공했습니다. 이 기능은 XHTML 1.0도 지원합니다.
Aspose.PDF for .NET는 PDF 파일을 HTML로 변환하는 기능을 지원합니다. Aspose.PDF 라이브러리를 사용하여 수행할 수 있는 주요 작업은 다음과 같습니다:
Aspose.PDF for .NET는 소스 PDF 파일을 HTML로 변환하기 위한 두 줄의 코드를 제공합니다. SaveFormat enumeration
에는 소스 파일을 HTML로 저장할 수 있는 Html 값이 포함되어 있습니다. 다음 코드 스니펫은 PDF 파일을 HTML로 변환하는 과정을 보여줍니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoHTML()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Save the output HTML
document.Save(dataDir + "output_out.html", Aspose.Pdf.SaveFormat.Html);
}
}
여러 페이지가 있는 큰 PDF 파일을 HTML 형식으로 변환할 때 출력은 단일 HTML 페이지로 나타납니다. 매우 길어질 수 있습니다. 페이지 크기를 제어하기 위해 PDF를 HTML로 변환하는 동안 출력을 여러 페이지로 분할할 수 있습니다. 다음 코드 스니펫을 사용해 보세요.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFtoMultiPageHTML()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Instantiate HTML SaveOptions object
var htmlOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Specify to split the output into multiple pages
SplitIntoPages = true
};
// Save the output HTML
document.Save(dataDir + "MultiPageHTML_out.html", htmlOptions);
}
}
PDF를 HTML로 변환하는 동안 SVG 이미지를 저장할 폴더를 지정할 수 있습니다. HtmlSaveOption class
SpecialFolderForSvgImages property
를 사용하여 특별한 SVG 이미지 디렉토리를 지정합니다. 이 속성은 변환 중에 SVG 이미지를 저장해야 하는 디렉토리의 경로를 가져오거나 설정합니다. 매개변수가 비어 있거나 null인 경우 모든 SVG 파일은 다른 이미지 파일과 함께 저장됩니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SavePDFtoHTMLWithSVG()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Instantiate HTML save options object
var newOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Specify the folder where SVG images are saved during PDF to HTML conversion
SpecialFolderForSvgImages = dataDir
};
// Save the output HTML
document.Save(dataDir + "SaveSVGFiles_out.html", newOptions);
}
}
PDF를 HTML로 변환하는 동안 SVG 이미지를 압축하려면 다음 코드를 사용해 보세요:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SavePDFtoCompressedHTMLWithSVG()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Create HtmlSaveOptions with tested feature
var newOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Compress the SVG images if there are any
CompressSvgGraphicsIfAny = true
};
// Save the output HTML
document.Save(dataDir + "CompressedSVGHTML_out.html", newOptions);
}
}
이미지를 저장하기 위한 기본 출력 형식은 SVG입니다. 변환 중에 PDF의 일부 이미지는 SVG 벡터 이미지로 변환됩니다. 이는 느릴 수 있습니다. 대신 각 페이지에 대해 하나의 PNG 배경 파일로 이미지를 변환할 수 있습니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void PdfToHtmlSaveImagesAsPngBackground()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
// Create HtmlSaveOption with tested feature
var htmlSaveOptions = new HtmlSaveOptions();
// Option to save images in PNG format as background for each page.
htmlSaveOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground;
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
document.Save(dataDir + "imagesAsPngBackground_out.html", htmlSaveOptions);
}
}
PDF를 HTML로 변환하는 동안 이미지를 저장할 폴더를 지정할 수도 있습니다:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SavePDFtoHTMLWithSeparateImageFolder()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Create HtmlSaveOptions with tested feature
var newOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Specify the separate folder to save images
SpecialFolderForAllImages = dataDir
};
// Save the output HTML
document.Save(dataDir + "HTMLWithSeparateImageFolder_out.html", newOptions);
}
}
최근에 PDF 파일을 HTML로 변환하고 사용자가 각 페이지의 <body>
태그의 내용만 가져올 수 있는 기능을 도입해 달라는 요청을 받았습니다. 이렇게 하면 CSS, <html>
, <head>
세부정보가 포함된 하나의 파일과 다른 파일에는 <body>
내용만 포함된 모든 페이지가 생성됩니다.
이 요구 사항을 충족하기 위해 HtmlSaveOptions 클래스에 HtmlMarkupGenerationMode라는 새로운 속성이 도입되었습니다.
다음 간단한 코드 스니펫을 사용하여 출력 HTML을 페이지로 분할할 수 있습니다. 출력 페이지에서는 모든 HTML 객체가 현재 위치에 정확히 배치되어야 합니다(글꼴 처리 및 출력, CSS 생성 및 출력, 이미지 생성 및 출력), 단 출력 HTML에는 현재 태그 안에 있는 내용만 포함됩니다(현재 “body” 태그는 생략됩니다). 그러나 이 접근 방식을 사용할 때 CSS에 대한 링크는 귀하의 코드의 책임이므로, 파일을 통해 CSS를 읽고 AJAX를 통해 웹 페이지에 전송하여 jQuery로 적용할 수 있습니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToHTMLWithBodyContent()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Initialize HtmlSaveOptions
var options = new Aspose.Pdf.HtmlSaveOptions
{
// Set HtmlMarkupGenerationMode to generate only body content
HtmlMarkupGenerationMode =
Aspose.Pdf.HtmlSaveOptions.HtmlMarkupGenerationModes.WriteOnlyBodyContent,
// Specify to split the output into multiple pages
SplitIntoPages = true
};
// Save the output HTML
document.Save(dataDir + "CreateSubsequentFiles_out.html", options);
}
}
소스/입력 PDF 파일에 전경 이미지에 의해 그림자가 있는 투명 텍스트가 포함된 경우 텍스트 렌더링 문제 발생할 수 있습니다. 이러한 시나리오를 처리하기 위해 SaveShadowedTextsAsTransparentTexts 및 SaveTransparentTexts 속성을 사용할 수 있습니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToHTMLWithTransparentTextRendering()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Initialize HtmlSaveOptions
var htmlOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Enable transparent text rendering
SaveShadowedTextsAsTransparentTexts = true,
SaveTransparentTexts = true
};
// Save the output HTML
document.Save(dataDir + "TransparentTextRendering_out.html", htmlOptions);
}
}
PDF를 HTML로 변환하는 동안 PDF 문서 레이어를 별도의 레이어 유형 요소로 렌더링할 수 있습니다:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertPDFToHTMLWithLayersRendering()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
// Instantiate HTML SaveOptions object
var htmlOptions = new Aspose.Pdf.HtmlSaveOptions
{
// Enable rendering of PDF document layers separately in the output HTML
ConvertMarkedContentToLayers = true
};
// Save the output HTML
document.Save(dataDir + "LayersRendering_out.html", htmlOptions);
}
}
이 코드는 수신된 파일을 스트림에 저장하는 예제를 보여줍니다. 저장을 위해 모든 유형의 스트림을 사용할 수 있습니다. 이 코드 조각은 사용자 정의 저장 전략이 작동하는 방식을 설명합니다.
// 전체 예제 및 데이터 파일은 https://github.com/aspose-pdf/Aspose.PDF-for-.NET를 방문하십시오.
private static string _folderForReferencedResources;
private static void ConvertPDFtoHTMLWithStream()
{
// 문서 디렉토리의 경로
var dataDir = RunExamples.GetDataDir_AsposePdf();
// PDF 문서 열기
using (var doc = new Aspose.Pdf.Document(dataDir + "PDFToHTML.pdf"))
{
var streamFilePath = dataDir + "saveToStream.html";
_folderForReferencedResources = dataDir + @"/saveFolder/";
// 기존 파일 정리
if (Directory.Exists(_folderForReferencedResources))
{
Directory.Delete(_folderForReferencedResources, true);
}
// 기존 파일 정리
if (File.Exists(streamFilePath))
{
File.Delete(streamFilePath);
}
var saveOptions = new Aspose.Pdf.HtmlSaveOptions();
// 변환을 위한 사용자 정의 전략 설정
saveOptions.CustomResourceSavingStrategy = new Aspose.Pdf.HtmlSaveOptions.ResourceSavingStrategy(StrategyCustomSaveResources);
saveOptions.CustomCssSavingStrategy = new Aspose.Pdf.HtmlSaveOptions.CssSavingStrategy(StrategyCssSaving);
saveOptions.CustomStrategyOfCssUrlCreation = new Aspose.Pdf.HtmlSaveOptions.CssUrlMakingStrategy(StrategyCssNaming);
using (Stream saveStream = File.OpenWrite(streamFilePath))
{
// 문서를 스트림에 저장
doc.Save(saveStream, saveOptions);
}
}
}
private static void StrategyCssSaving(Aspose.Pdf.HtmlSaveOptions.CssSavingInfo resourceInfo)
{
// 기존 파일 정리
if (!System.IO.Directory.Exists(_folderForReferencedResources))
{
System.IO.Directory.CreateDirectory(_folderForReferencedResources);
}
// css의 경로 생성
string path = _folderForReferencedResources + Path.GetFileName(resourceInfo.SupposedURL);
var reader = new System.IO.BinaryReader(resourceInfo.ContentStream);
// 생성된 경로에 css 기록
System.IO.File.WriteAllBytes(path, reader.ReadBytes((int)resourceInfo.ContentStream.Length));
}
private static string StrategyCssNaming(Aspose.Pdf.HtmlSaveOptions.CssUrlRequestInfo requestInfo)
{
return _folderForReferencedResources + "css_style{0}.css";
}
private static string StrategyCustomSaveResources(Aspose.Pdf.SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
// 기존 파일 정리
if (!System.IO.Directory.Exists(_folderForReferencedResources))
{
System.IO.Directory.CreateDirectory(_folderForReferencedResources);
}
// 리소스의 경로 생성
var resourcePath = _folderForReferencedResources + System.IO.Path.GetFileName(resourceSavingInfo.SupposedFileName);
var binaryReader = new System.IO.BinaryReader(resourceSavingInfo.ContentStream);
// 생성된 경로에 리소스 기록
System.IO.File.WriteAllBytes(resourcePath, binaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));
var urlInHtml = _folderForReferencedResources.Replace(@"\", "/") + System.IO.Path.GetFileName(resourceSavingInfo.SupposedFileName);
// HTML에 삽입할 리소스 이름 반환
return urlInHtml;
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.