C#를 사용하여 태그가 있는 PDF 만들기
태그가 있는 PDF를 생성한다는 것은 PDF/UA 요구 사항에 따라 문서를 검증할 수 있도록 특정 요소를 문서에 추가(또는 생성)하는 것을 의미합니다. 이러한 요소는 종종 구조 요소라고 불립니다.
다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.
태그가 있는 PDF 만들기 (간단한 시나리오)
태그가 있는 PDF 문서에서 구조 요소를 생성하기 위해 Aspose.PDF는 ITaggedContent 인터페이스를 사용하여 구조 요소를 생성하는 메서드를 제공합니다. 다음 코드 스니펫은 헤더와 단락의 2개 요소가 포함된 태그가 있는 PDF를 생성하는 방법을 보여줍니다.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument01 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement mainHeader = taggedContent . CreateHeaderElement ();
mainHeader . SetText ( "Main Header" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphElement = taggedContent . CreateParagraphElement ();
paragraphElement . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " +
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" +
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." +
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" +
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" +
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" +
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," +
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus." );
rootElement . AppendChild ( mainHeader );
rootElement . AppendChild ( paragraphElement );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument01 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement mainHeader = taggedContent . CreateHeaderElement ();
mainHeader . SetText ( "Main Header" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphElement = taggedContent . CreateParagraphElement ();
paragraphElement . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. " +
"Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet" +
"nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus." +
"Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat" +
"sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper" +
"pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus" +
"ac iaculis eget, tempus et magna. Sed non consectetur elit. Sed vulputate, quam sed lacinia luctus," +
"ipsum nibh fringilla purus, vitae posuere risus odio id massa. Cras sed venenatis lacus." );
rootElement . AppendChild ( mainHeader );
rootElement . AppendChild ( paragraphElement );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
생성 후 다음 문서를 얻을 수 있습니다:
중첩 요소가 있는 태그가 있는 PDF 만들기 (구조 요소 트리 생성)
경우에 따라 단락에 인용문을 배치하는 등 더 복잡한 구조를 생성해야 할 필요가 있습니다.
구조 요소 트리를 생성하기 위해 AppendChild 메서드를 사용해야 합니다.
다음 코드 스니펫은 태그가 있는 PDF 문서의 구조 요소 트리를 생성하는 방법을 보여줍니다:
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument02 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement header1 = taggedContent . CreateHeaderElement ( 1 );
header1 . SetText ( "Header Level 1" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphWithQuotes = taggedContent . CreateParagraphElement ();
paragraphWithQuotes . StructureTextState . Font = Aspose . Pdf . Text . FontRepository . FindFont ( "Calibri" );
paragraphWithQuotes . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
Margin = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 )
});
Aspose . Pdf . LogicalStructure . SpanElement spanElement1 = taggedContent . CreateSpanElement ();
spanElement1 . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. " );
Aspose . Pdf . LogicalStructure . QuoteElement quoteElement = taggedContent . CreateQuoteElement ();
quoteElement . SetText ( "Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa." );
quoteElement . StructureTextState . FontStyle = Aspose . Pdf . Text . FontStyles . Bold | Aspose . Pdf . Text . FontStyles . Italic ;
Aspose . Pdf . LogicalStructure . SpanElement spanElement2 = taggedContent . CreateSpanElement ();
spanElement2 . SetText ( " Sed non consectetur elit." );
paragraphWithQuotes . AppendChild ( spanElement1 );
paragraphWithQuotes . AppendChild ( quoteElement );
paragraphWithQuotes . AppendChild ( spanElement2 );
rootElement . AppendChild ( header1 );
rootElement . AppendChild ( paragraphWithQuotes );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTaggedPdfDocument02 ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
Aspose . Pdf . LogicalStructure . HeaderElement header1 = taggedContent . CreateHeaderElement ( 1 );
header1 . SetText ( "Header Level 1" );
Aspose . Pdf . LogicalStructure . ParagraphElement paragraphWithQuotes = taggedContent . CreateParagraphElement ();
paragraphWithQuotes . StructureTextState . Font = Aspose . Pdf . Text . FontRepository . FindFont ( "Calibri" );
paragraphWithQuotes . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
Margin = new Aspose . Pdf . MarginInfo ( 10 , 5 , 10 , 5 )
});
Aspose . Pdf . LogicalStructure . SpanElement spanElement1 = taggedContent . CreateSpanElement ();
spanElement1 . SetText ( "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec lectus ac sem faucibus imperdiet. Sed ut erat ac magna ullamcorper hendrerit. Cras pellentesque libero semper, gravida magna sed, luctus leo. Fusce lectus odio, laoreet nec ullamcorper ut, molestie eu elit. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia sit amet elit ac consectetur. Donec cursus condimentum ligula, vitae volutpat sem tristique eget. Nulla in consectetur massa. Vestibulum vitae lobortis ante. Nulla ullamcorper pellentesque justo rhoncus accumsan. Mauris ornare eu odio non lacinia. Aliquam massa leo, rhoncus ac iaculis eget, tempus et magna. Sed non consectetur elit. " );
Aspose . Pdf . LogicalStructure . QuoteElement quoteElement = taggedContent . CreateQuoteElement ();
quoteElement . SetText ( "Sed vulputate, quam sed lacinia luctus, ipsum nibh fringilla purus, vitae posuere risus odio id massa." );
quoteElement . StructureTextState . FontStyle = Aspose . Pdf . Text . FontStyles . Bold | Aspose . Pdf . Text . FontStyles . Italic ;
Aspose . Pdf . LogicalStructure . SpanElement spanElement2 = taggedContent . CreateSpanElement ();
spanElement2 . SetText ( " Sed non consectetur elit." );
paragraphWithQuotes . AppendChild ( spanElement1 );
paragraphWithQuotes . AppendChild ( quoteElement );
paragraphWithQuotes . AppendChild ( spanElement2 );
rootElement . AppendChild ( header1 );
rootElement . AppendChild ( paragraphWithQuotes );
// Save Tagged PDF Document
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
생성 후 다음 문서를 얻을 수 있습니다:
텍스트 구조 스타일링
태그가 있는 PDF 문서에서 텍스트 구조를 스타일링하기 위해 Aspose.PDF는 Font , FontSize , FontStyle 및 ForegroundColor 속성을 StructureTextState 클래스에서 제공합니다. 다음 코드 스니펫은 태그가 있는 PDF 문서에서 텍스트 구조를 스타일링하는 방법을 보여줍니다:
구조 요소 설명
태그가 있는 PDF 문서에서 구조 요소를 설명하기 위해 Aspose.PDF는 IllustrationElement 클래스를 제공합니다. 다음 코드 스니펫은 태그가 있는 PDF 문서에서 구조 요소를 설명하는 방법을 보여줍니다:
태그가 있는 PDF 검증
Aspose.PDF for .NET는 PDF/UA 태그가 있는 PDF 문서를 검증할 수 있는 기능을 제공합니다. PDF/UA 표준의 유효성 검사는 다음을 지원합니다:
XObjects 확인.
Actions 확인.
선택적 콘텐츠 확인.
포함된 파일 확인.
Acroform 필드 확인(자연어 및 대체 이름 및 디지털 서명 검증).
XFA 양식 필드 확인.
보안 설정 확인.
탐색 확인.
주석 확인.
다음 코드 스니펫은 태그가 있는 PDF 문서를 검증하는 방법을 보여줍니다. 해당 문제는 XML 로그 보고서에 표시됩니다.
텍스트 구조 위치 조정
다음 코드 스니펫은 태그가 있는 PDF 문서에서 텍스트 구조 위치를 조정하는 방법을 보여줍니다:
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AdjustPosition ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using ( var document = new Aspose . Pdf . Document ())
{
// Get Content for work with TaggedPdf
var taggedContent = document . TaggedContent ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
// Create paragraph
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
// Adjust position
p . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
HorizontalAlignment = Aspose . Pdf . HorizontalAlignment . None ,
Margin = new Aspose . Pdf . MarginInfo
{
Left = 300 ,
Right = 0 ,
Top = 20 ,
Bottom = 0
},
VerticalAlignment = Aspose . Pdf . VerticalAlignment . None ,
IsFirstParagraphInColumn = false ,
IsKeptWithNext = false ,
IsInNewPage = false ,
IsInLineParagraph = false
});
// Save Tagged PDF Document
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AdjustPosition ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
// Create PDF Document
using var document = new Aspose . Pdf . Document ();
// Get Content for work with TaggedPdf
var taggedContent = document . TaggedContent ;
// Set Title and Language for Document
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
// Create paragraph
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
// Adjust position
p . AdjustPosition ( new Aspose . Pdf . Tagged . PositionSettings
{
HorizontalAlignment = Aspose . Pdf . HorizontalAlignment . None ,
Margin = new Aspose . Pdf . MarginInfo
{
Left = 300 ,
Right = 0 ,
Top = 20 ,
Bottom = 0
},
VerticalAlignment = Aspose . Pdf . VerticalAlignment . None ,
IsFirstParagraphInColumn = false ,
IsKeptWithNext = false ,
IsInNewPage = false ,
IsInLineParagraph = false
});
// Save Tagged PDF Document
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}
PDF/UA-1 변환으로 태그가 있는 PDF 자동 생성
Aspose.PDF는 문서를 PDF/UA-1로 변환할 때 기본 논리 구조 마크업의 자동 생성을 가능하게 합니다. 사용자는 이 기본 논리 구조를 수동으로 개선하여 문서 내용에 대한 추가 통찰력을 제공할 수 있습니다.
논리 문서 구조를 생성하려면 Aspose.Pdf.AutoTaggingSettings 클래스의 인스턴스를 생성하고, AutoTaggingSettings.EnableAutoTagging 을 true
로 설정한 다음, PdfFormatConversionOptions.AutoTaggingSettings 속성에 할당합니다.
문서에 이미 논리 구조 태그가 있는 경우 자동 태그 생성을 활성화하면 기존 논리 구조가 파괴되고 새로운 구조가 생성됩니다.