Last active
April 2, 2017 23:34
-
-
Save lruckman/510ebfd3461240c313b7ec4252c6087e to your computer and use it in GitHub Desktop.
iTextSharp Split document and Bates Stamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static Action<iTextSharp.text.Rectangle, PdfImportedPage, PdfSmartCopy, int> shrinkForBateStamp = | |
(pageSizeWithRotation, page, copier, pageNumber) => | |
{ | |
// shrink percentage | |
const float shrinkPercentage = .95f; | |
var stamp = copier.CreatePageStamp(page); | |
var w = page.Width > page.Height ? page.Height : page.Width; | |
var h = page.Width > page.Height ? page.Width : page.Height; | |
var offsetX = (w * (1 - shrinkPercentage)) / 2; | |
var offsetY = (h * (1 - shrinkPercentage)) / 2; | |
// Shrink the page content by x percentage and center the contents. Page itself remains the same size. | |
stamp.GetUnderContent() | |
.SetLiteral($"\nq {shrinkPercentage} 0 0 {shrinkPercentage} {offsetX} {offsetY} cm\nq\n"); | |
var cb = stamp.GetOverContent(); | |
cb.SetLiteral("\nQ\nQ\n"); | |
// Get bottom right points | |
var point = GetRightBottom(page.Rotation, pageSizeWithRotation, 10); | |
// Add the page number to the bottom right | |
ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT | |
, new Phrase($"{pageNumber:00000}", _baseFont), point.X, point.Y, page.Rotation); | |
// mark contents as altered | |
stamp.AlterContents(); | |
}; | |
private static Point GetRightBottom(int pageRotation, iTextSharp.text.Rectangle rectangle, int margin) | |
{ | |
switch (pageRotation) | |
{ | |
case 0: | |
return new Point((int)rectangle.Right - margin, (int)rectangle.Bottom + margin); | |
case 90: | |
return new Point((int)rectangle.Right - margin, (int)rectangle.Top - margin); | |
case 180: | |
return new Point((int)rectangle.Left + margin, (int)rectangle.Top - margin); | |
case 270: | |
return new Point((int)rectangle.Left + margin, (int)rectangle.Bottom + margin); | |
default: | |
throw new ArgumentException($"Unsupported pageRotation value of {pageRotation}", nameof(pageRotation)); | |
} | |
} | |
private static readonly iTextSharp.text.Font _baseFont = | |
new iTextSharp.text.Font(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 8); | |
public static void Split(PdfReader srcReader, string outFilePath, int startPage, int stopPage | |
, Action<iTextSharp.text.Rectangle, PdfImportedPage, PdfSmartCopy, int> onPage = null) | |
{ | |
using (var destStream = new FileStream(outFilePath, FileMode.Create)) | |
{ | |
// create our destination document for iTextSharp to build our PDF | |
using (var destDocument = new Document()) | |
{ | |
// get our copier so we can copy pages from our source reader | |
using (var copier = GetCopier(destDocument, destStream)) | |
{ | |
copier.RotateContents = false; | |
// open our itext document so we can start writing to it. | |
destDocument.Open(); | |
// loop through and get our pages | |
for (var pageNumber = startPage; pageNumber <= stopPage; pageNumber++) | |
{ | |
// add the page to our copier which in turn will write it to our dest document | |
var page = copier.GetImportedPage(srcReader, pageNumber); | |
var pageSizeWithRotation = srcReader.GetPageSizeWithRotation(pageNumber); | |
// run an page logic here | |
onPage?.Invoke(pageSizeWithRotation, page, copier, pageNumber); | |
copier.AddPage(page); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment