C#를 사용하여 태그가 있는 PDF 만들기
태그가 있는 PDF를 생성하는 것은 PDF/UA 요구 사항에 따라 문서를 검증할 수 있도록 문서에 특정 요소를 추가(또는 생성)하는 것을 의미합니다. 이러한 요소를 일반적으로 구조 요소라고 합니다.
다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.
태그가 있는 PDF 만들기 (간단한 시나리오)
태그가 있는 PDF 문서에서 구조 요소를 생성하기 위해 Aspose.PDF는 ITaggedContent 인터페이스를 사용하여 구조 요소를 생성하는 방법을 제공합니다. 다음 코드 스니펫은 헤더와 단락의 2개 요소를 포함하는 태그가 있는 PDF를 생성하는 방법을 보여줍니다.
.NET Core 3.1
Copy
private static void CreateTaggedPdfDocument01 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document = new Aspose . Pdf . Document ())
{
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
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 );
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
Copy
private static void CreateTaggedPdfDocument01 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document = new Aspose . Pdf . Document ();
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
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 );
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
생성 후 다음 문서를 얻을 수 있습니다:
중첩 요소가 있는 태그가 있는 PDF 만들기 (구조 요소 트리 생성)
일부 경우에는 단락에 인용문을 배치하는 등 더 복잡한 구조를 생성해야 합니다.
구조 요소 트리를 생성하기 위해 AppendChild 메서드를 사용해야 합니다.
다음 코드 스니펫은 태그가 있는 PDF 문서의 구조 요소 트리를 생성하는 방법을 보여줍니다:
.NET Core 3.1
Copy
private static void CreateTaggedPdfDocument02 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document = new Aspose . Pdf . Document ())
{
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
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 );
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
}
.NET 8
Copy
private static void CreateTaggedPdfDocument02 ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document = new Aspose . Pdf . Document ();
Aspose . Pdf . Tagged . ITaggedContent taggedContent = document . TaggedContent ;
var rootElement = taggedContent . RootElement ;
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 );
document . Save ( dataDir + "TaggedPdfDocument_out.pdf" );
}
생성 후 다음 문서를 얻을 수 있습니다:
텍스트 구조 스타일링
태그가 있는 PDF 문서에서 텍스트 구조의 스타일을 지정하기 위해 Aspose.PDF는 Font , FontSize , FontStyle 및 ForegroundColor 속성을 제공합니다. 다음 코드 스니펫은 태그가 있는 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
Copy
private static void AdjustPosition ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using ( var document = new Aspose . Pdf . Document ())
{
var taggedContent = document . TaggedContent ;
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
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
});
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}
}
.NET 8
Copy
private static void AdjustPosition ( )
{
var dataDir = RunExamples . GetDataDir_AsposePdf_WorkingDocuments ();
using var document = new Aspose . Pdf . Document ();
var taggedContent = document . TaggedContent ;
taggedContent . SetTitle ( "Tagged Pdf Document" );
taggedContent . SetLanguage ( "en-US" );
var p = taggedContent . CreateParagraphElement ();
taggedContent . RootElement . AppendChild ( p );
p . SetText ( "Text." );
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
});
document . Save ( dataDir + "AdjustTextPosition_out.pdf" );
}