| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- using System.Runtime.InteropServices;
- using QuestPDF.Fluent;
- using QuestPDF.Helpers;
- using QuestPDF.Infrastructure;
- namespace QuestPDF.DocumentationExamples;
- [TestFixture]
- public class DocumentOperationExamples
- {
- [Test]
- public void MergeFiles()
- {
- const string prefix = "document-operation-merge";
-
- GenerateSampleDocument($"{prefix}-source-red.pdf", Colors.Red.Lighten3, 2);
- GenerateSampleDocument($"{prefix}-source-green.pdf", Colors.Green.Lighten3, 3);
- GenerateSampleDocument($"{prefix}-source-blue.pdf", Colors.Blue.Lighten3, 5);
-
- DocumentOperation
- .LoadFile($"{prefix}-source-red.pdf")
- .MergeFile($"{prefix}-source-green.pdf")
- .MergeFile($"{prefix}-source-blue.pdf")
- .Save($"{prefix}-result.pdf");
- }
-
- [Test]
- public void SelectEvenPages()
- {
- const string prefix = "document-operation-select-even-pages";
-
- GenerateSampleDocument($"{prefix}-source.pdf", Colors.Indigo.Lighten3, 11);
-
- DocumentOperation
- .LoadFile($"{prefix}-source.pdf")
- .TakePages("1-z:even")
- .Save($"{prefix}-result.pdf");
- }
- [Test]
- public void Encrypt()
- {
- const string prefix = "document-operation-encrypt";
-
- GenerateSampleDocument($"{prefix}-source.pdf", Colors.Orange.Lighten3, 7);
-
- DocumentOperation
- .LoadFile($"{prefix}-source.pdf")
- .Encrypt(new DocumentOperation.Encryption256Bit()
- {
- UserPassword = "user-password",
- OwnerPassword = "owner-password",
- AllowContentExtraction = false,
- AllowPrinting = false
- })
- .Save($"{prefix}-result.pdf");
- }
-
- [Test]
- public void AddAttachment()
- {
- const string prefix = "document-operation-add-attachment";
-
- GenerateSampleDocument($"{prefix}-source.pdf", Colors.Cyan.Lighten3, 7);
- File.WriteAllText($"{prefix}-content.txt", "Hello, World!");
-
- DocumentOperation
- .LoadFile($"{prefix}-source.pdf")
- .AddAttachment(new DocumentOperation.DocumentAttachment
- {
- FilePath = $"{prefix}-content.txt",
- AttachmentName = "Attached message"
- })
- .Save($"{prefix}-result.pdf");
- }
-
- [Test]
- public void Overlay()
- {
- const string prefix = "document-operation-overlay";
-
- GenerateSampleDocument($"{prefix}-source.pdf", Colors.Cyan.Lighten3, 7);
-
- Document
- .Create(document =>
- {
- document.Page(page =>
- {
- page.Margin(1, Unit.Centimetre);
- page.PageColor(Colors.Transparent);
-
- page.Content().Column(column =>
- {
- foreach (var i in Enumerable.Range(0, 6))
- column.Item().PageBreak();
- });
- page.Footer().AlignCenter().Text(text =>
- {
- text.DefaultTextStyle(x => x.FontSize(24).Bold().FontColor(Colors.White));
- text.Span("Page ");
- text.CurrentPageNumber();
- text.Span(" of ");
- text.TotalPages();
- });
- });
- })
- .GeneratePdf($"{prefix}-content.pdf");
-
- DocumentOperation
- .LoadFile($"{prefix}-source.pdf")
- .OverlayFile(new DocumentOperation.LayerConfiguration
- {
- FilePath = $"{prefix}-content.pdf"
- })
- .Save($"{prefix}-result.pdf");
- }
-
- private void GenerateSampleDocument(string fileName, Color pageColor, int numberOfPages)
- {
- Document
- .Create(container =>
- {
- container.Page(page =>
- {
- page.Margin(1, Unit.Centimetre);
- page.PageColor(pageColor);
-
- page.Content().Column(column =>
- {
- foreach (var pageNumber in Enumerable.Range(1, numberOfPages))
- {
- column.Item()
- .Extend()
- .AlignCenter().AlignMiddle()
- .Text($"{pageNumber}")
- .FontSize(256)
- .FontColor(Colors.White)
- .Bold();
-
- if (pageNumber != numberOfPages)
- column.Item().PageBreak();
- }
- });
- });
- })
- .GeneratePdf(fileName);
- }
- }
|