Markdown 기능 작업
이 항목에서는 Aspose.Words를 사용하여 Markdown 기능을 구현하는 방법에 대해 설명합니다. Markdown는 HTML로 쉽게 변환할 수 있는 일반 텍스트의 형식을 지정하는 간단한 방법입니다. Aspose.Words는 다음 Markdown 기능을 지원합니다
- 제목
- 인용부호
- 수평 규칙
- 대담한 강조
- 이탤릭체 강조
Markdown 기능 구현은 대부분 Aspose.Words API의 CommonMark
사양을 따르며 모든 기능은 해당 스타일 또는 직접 형식으로 표시됩니다. 의미하는 것은
- 볼드체와 이탤릭체는
Font.Bold
,Font.Italic
로 표시됩니다 - 제목은 제목 1 - 제목 6 스타일의 단락입니다
- 인용문은 스타일 이름에 “인용문"이 붙은 문단입니다
- 수평규칙(HorizontalRule)은 수평규칙 형태의 문단입니다.
강조점이 있는 Markdown 문서
이 섹션에서는 아래와 같이 강조된 markdown 문서를 생성하는 방법을 보여줍니다
Markdown treats asterisks (*) and underscores (_) as indicators of emphasis.
You can write **bold** or *italic* text.
You can also write ***BoldItalic*** text.
다음 코드 조각을 사용하여 위에 제공된 markdown 문서를 생성할 수 있습니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Markdown treats asterisks (*) and underscores (_) as indicators of emphasis."); | |
builder.Write("You can write "); | |
builder.Font.Bold = true; | |
builder.Write("bold"); | |
builder.Font.Bold = false; | |
builder.Write(" or "); | |
builder.Font.Italic = true; | |
builder.Write("italic"); | |
builder.Font.Italic = false; | |
builder.Writeln(" text. "); | |
builder.Write("You can also write "); | |
builder.Font.Bold = true; | |
builder.Font.Italic = true; | |
builder.Write("BoldItalic"); | |
builder.Font.Bold = false; | |
builder.Font.Italic = false; | |
builder.Write("text."); | |
builder.Document.Save("EmphasesExample.md"); |
제목이 있는 Markdown 문서
이 섹션에서는 아래와 같은 제목을 사용하여 markdown 문서를 생성하는 방법을 보여줍니다
The following produces headings:
# Heading1
## Heading2
### Heading3
#### Heading4
##### Heading5
###### Heading6
# **Bold Heading1**
다음 코드 조각을 사용하여 위에 제공된 markdown 문서를 생성할 수 있습니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// By default Heading styles in Word may have bold and italic formatting. | |
// If we do not want text to be emphasized, set these properties explicitly to false. | |
builder.Font.Bold = false; | |
builder.Font.Italic = false; | |
builder.Writeln("The following produces headings:"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 1"]; | |
builder.Writeln("Heading1"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 2"]; | |
builder.Writeln("Heading2"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 3"]; | |
builder.Writeln("Heading3"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 4"]; | |
builder.Writeln("Heading4"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 5"]; | |
builder.Writeln("Heading5"); | |
builder.ParagraphFormat.Style = doc.Styles["Heading 6"]; | |
builder.Writeln("Heading6"); | |
// Note, emphases are also allowed inside Headings: | |
builder.Font.Bold = true; | |
builder.ParagraphFormat.Style = doc.Styles["Heading 1"]; | |
builder.Writeln("Bold Heading1"); | |
doc.Save(dataDir + "HeadingsExample.md"); |
블록 인용문이 포함된 Markdown 문서
이 섹션에서는 아래와 같이 블록 인용문을 사용하여 markdown 문서를 생성하는 방법을 보여줍니다
We support blockquotes in Markdown:
>*Lorem*
>*ipsum*
>The quotes can be of any level and can be nested:
>>>Quote level 3
>>>
>>>>Nested quote level 4
>
>*Back to first level*
>### Headings are allowed inside Quotes
>
다음 코드 조각을 사용하여 위에 제공된 markdown 문서를 생성할 수 있습니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("We support blockquotes in Markdown:"); | |
builder.ParagraphFormat.Style = doc.Styles["Quote"]; | |
builder.Writeln("Lorem"); | |
builder.Writeln("ipsum"); | |
builder.ParagraphFormat.Style = doc.Styles["Normal"]; | |
builder.Writeln("The quotes can be of any level and can be nested:"); | |
Style quoteLevel3 = doc.Styles.Add(StyleType.Paragraph, "Quote2"); | |
builder.ParagraphFormat.Style = quoteLevel3; | |
builder.Writeln("Quote level 3"); | |
Style quoteLevel4 = doc.Styles.Add(StyleType.Paragraph, "Quote3"); | |
builder.ParagraphFormat.Style = quoteLevel4; | |
builder.Writeln("Nested quote level 4"); | |
builder.ParagraphFormat.Style = doc.Styles["Quote"]; | |
builder.Writeln(); | |
builder.Writeln("Back to first level"); | |
Style quoteLevel1WithHeading = doc.Styles.Add(StyleType.Paragraph, "Quote Heading 3"); | |
builder.ParagraphFormat.Style = quoteLevel1WithHeading; | |
builder.Write("Headings are allowed inside Quotes"); | |
doc.Save(dataDir + "QuotesExample.md"); |
수평선이 있는 Markdown 문서
이 섹션에서는 아래와 같이 수평선을 사용하여 markdown 문서를 생성하는 방법을 보여줍니다
We support Horizontal rules (Thematic breaks) in Markdown:
-----
다음 코드 조각을 사용하여 위에 제공된 markdown 문서를 생성할 수 있습니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Insert a horizontal rule shape into the document."); | |
builder.InsertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.Save(dataDir); |
Markdown 문서 읽기
다음 코드 조각은 markdown 문서를 읽는 방법을 보여줍니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// This is Markdown document that was produced in example of UC3. | |
Document doc = new Document(dataDir + "QuotesExample.md"); | |
// Let's remove Heading formatting from a Quote in the very last paragraph. | |
Paragraph paragraph = doc.FirstSection.Body.LastParagraph; | |
paragraph.ParagraphFormat.Style = doc.Styles["Quote"]; | |
doc.Save(dataDir + "QuotesModifiedExample.md"); |
Markdown 저장 옵션 지정
Aspose.Words API는 문서를 Markdown 형식으로 저장하는 동안 추가 옵션을 지정할 수 있는 MarkdownSaveOptions 클래스를 제공합니다.
다음 코드 예제에서는 다양한 Markdown 저장 옵션을 지정하는 방법을 보여줍니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
builder.Writeln("Some text!"); | |
// specify MarkDownSaveOptions | |
MarkdownSaveOptions saveOptions = (MarkdownSaveOptions)SaveOptions.CreateSaveOptions(SaveFormat.Markdown); | |
builder.Document.Save(dataDir + "TestDocument.md", saveOptions); |
Markdown로 내보내는 동안 테이블 내부의 콘텐츠를 정렬하는 방법
Aspose.Words API는 Markdown 문서로 내보내는 동안 테이블의 내용을 정렬하기 위한 정렬 방향을 정의하는 TableContentAlignment 열거형을 제공합니다. 다음 코드 예제에서는 테이블 내부의 콘텐츠를 정렬하는 방법을 보여줍니다.
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
// Create a new table with two cells. | |
builder.InsertCell(); | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right; | |
builder.Write("Cell1"); | |
builder.InsertCell(); | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.Write("Cell2"); | |
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); | |
// Makes all paragraphs inside table to be aligned to Left. | |
saveOptions.TableContentAlignment = TableContentAlignment.Left; | |
builder.Document.Save(dataDir + "left.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Right. | |
saveOptions.TableContentAlignment = TableContentAlignment.Right; | |
builder.Document.Save(dataDir + "right.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned to Center. | |
saveOptions.TableContentAlignment = TableContentAlignment.Center; | |
builder.Document.Save(dataDir + "center.md", saveOptions); | |
// Makes all paragraphs inside table to be aligned automatically. | |
// The alignment in this case will be taken from the first paragraph in corresponding table column. | |
saveOptions.TableContentAlignment = TableContentAlignment.Auto; | |
builder.Document.Save(dataDir + "auto.md", saveOptions); |