forked from VahidN/PdfReport.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergePdfFilesPdfReport.cs
84 lines (74 loc) · 3.11 KB
/
MergePdfFilesPdfReport.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PdfRpt.Core.Contracts;
using PdfRpt.Core.Helper;
namespace PdfRpt.Core.FunctionalTests
{
[TestClass]
public class MergePdfFilesPdfReport
{
[TestMethod]
public void Verify_MergePdfFilesPdfReport_Can_Be_Created()
{
var report = CreateMergePdfFilesPdfReport();
TestUtils.VerifyPdfFileIsReadable(report);
}
public string CreateMergePdfFilesPdfReport()
{
return mergeMultipleReports();
}
private string mergeMultipleReports()
{
var rpt1 = new IListPdfReport().CreateIListPdfReport();
var rpt2 = new HexDumpPdfReport().CreateHexDumpPdfReport();
var finalMergedFile = Path.Combine(TestUtils.GetOutputFolder(), "mergedFile.pdf");
var mergePdfDocuments = new MergePdfDocuments
{
DocumentMetadata = new DocumentMetadata { Author = "Vahid", Application = "PdfRpt", Keywords = "Test", Subject = "MergePdfFiles Rpt.", Title = "Test" },
InputFileStreams = new Stream[]
{
new FileStream(rpt1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read),
new FileStream(rpt2.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
},
OutputFileStream = new FileStream(finalMergedFile, FileMode.Create),
AttachmentsBookmarkLabel = "Attachment(s) ",
WriterCustomizer = importedPageInfo =>
{
addNewPageNumbersToFinalMergedFile(importedPageInfo);
}
};
mergePdfDocuments.PerformMerge();
foreach (var stream in mergePdfDocuments.InputFileStreams)
{
stream?.Dispose();
}
mergePdfDocuments.OutputFileStream?.Dispose();
return finalMergedFile;
}
private void addNewPageNumbersToFinalMergedFile(ImportedPageInfo importedPageInfo)
{
var bottomMargin = importedPageInfo.PdfDocument.BottomMargin;
var pageSize = importedPageInfo.PageSize;
var contentByte = importedPageInfo.Stamp.GetOverContent();
// hide the old footer
contentByte.SaveState();
contentByte.SetColorFill(BaseColor.White);
contentByte.Rectangle(0, 0, pageSize.Width, bottomMargin);
contentByte.Fill();
contentByte.RestoreState();
// write the new page numbers
var center = (pageSize.Left + pageSize.Right) / 2;
ColumnText.ShowTextAligned(
canvas: contentByte,
alignment: Element.ALIGN_CENTER,
phrase: new Phrase($"Page {importedPageInfo.CurrentPageNumber}/{importedPageInfo.TotalNumberOfPages}"),
x: center,
y: pageSize.GetBottom(25),
rotation: 0,
runDirection: PdfWriter.RUN_DIRECTION_LTR,
arabicOptions: 0);
}
}
}