In C# Why not showing signer name in pdf when signed by Itextsharp library?
I try to crate signed PDF through digital certificate USB Token. When i generate signature on every page that time signature cant show singer name.
Here is the code for getting the certificate:
X509Certificate2 certClient = null;
X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
string filename = #"..\List1.pdf";
st.Open(OpenFlags.MaxAllowed);
X509Certificate2Collection collection = st.Certificates;
foreach (X509Certificate2 cert in collection)
{
if (cert.Subject.ToLower().Contains("serialnumber"))
{
certClient = cert;
string username = certClient.Subject;
string startdate = certClient.GetEffectiveDateString();
string enddate = certClient.GetExpirationDateString();
}
}
st.Close();
IList<X509Certificate> chain = new List<X509Certificate>();
X509Chain x509Chain = new X509Chain();
x509Chain.Build(certClient);
foreach (X509ChainElement x509ChainElement in x509Chain.ChainElements)
{
chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
}
PdfReader inputPdf = new PdfReader(filename);
string dest = #"..\sign10a.pdf";
PdfReader reader = new PdfReader(filename);
FileStream os = new FileStream(dest, FileMode.Create, FileAccess.Write);
PdfStamper stamper = PdfStamper.CreateSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
appearance.Acro6Layers = false;
appearance.Image = null;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 10, 250, 100), reader.NumberOfPages, null);
IExternalSignature externalSignature1 = new X509Certificate2Signature(certClient, "SHA-256");
AllPagesSignatureContainer allPagesContainer = new AllPagesSignatureContainer(appearance, externalSignature1, chain);
MakeSignature.SignExternalContainer(appearance, allPagesContainer, 8192);
reader.Close();
stamper.Dispose();
And here is my multiple appearance code:
public class AllPagesSignatureContainer : IExternalSignatureContainer
{
public AllPagesSignatureContainer(PdfSignatureAppearance appearance, IExternalSignature externalSignature, ICollection<X509Certificate> chain)
{
this.appearance = appearance;
this.chain = chain;
this.externalSignature = externalSignature;
}
public void ModifySigningDictionary(PdfDictionary signDic)
{
signDic.Put(PdfName.FILTER, PdfName.ADOBE_PPKMS);
signDic.Put(PdfName.SUBFILTER, PdfName.ADBE_PKCS7_DETACHED);
PdfStamper stamper = appearance.Stamper;
PdfReader reader = stamper.Reader;
PdfDictionary xobject1 = new PdfDictionary();
PdfDictionary xobject2 = new PdfDictionary();
xobject1.Put(PdfName.N, appearance.GetAppearance().IndirectReference);
xobject2.Put(PdfName.AP, xobject1);
PdfIndirectReference PRef = stamper.Writer.PdfIndirectReference;
PdfLiteral PRefLiteral = new PdfLiteral((PRef.Number + 1 + 2 * (reader.NumberOfPages - 1)) + " 0 R");
for (int i = 1; i < reader.NumberOfPages; i++)
{
var signatureField = PdfFormField.CreateSignature(stamper.Writer);
signatureField.Put(PdfName.T, new PdfString("ClientSignature_" + i.ToString()));
signatureField.Put(PdfName.V, PRefLiteral);
signatureField.Put(PdfName.F, new PdfNumber("132"));
signatureField.SetWidget(appearance.Rect, null);
signatureField.Put(PdfName.SUBTYPE, PdfName.WIDGET);
signatureField.Put(PdfName.AP, xobject1);
signatureField.SetPage();
Console.WriteLine(signatureField);
stamper.AddAnnotation(signatureField, i);
}
}
public byte[] Sign(Stream data)
{
String hashAlgorithm = externalSignature.GetHashAlgorithm();
PdfPKCS7 sgn = new PdfPKCS7(null, chain, hashAlgorithm, false);
IDigest messageDigest = DigestUtilities.GetDigest(hashAlgorithm);
byte[] hash = DigestAlgorithms.Digest(data, hashAlgorithm);
byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS);
byte[] extSignature = externalSignature.Sign(sh);
sgn.SetExternalDigest(extSignature, null, externalSignature.GetEncryptionAlgorithm());
return sgn.GetEncodedPKCS7(hash, null, null, null, CryptoStandard.CMS);
}
PdfSignatureAppearance appearance;
ICollection<X509Certificate> chain;
IExternalSignature externalSignature;
}
Digital signature show multiple page but signer name can't display.
I hope it helps, I wrote the code from examples which found on internet. it will work but only singer name not display.
You wonder why in your visualization
there is no name after "Digitally signed by".
iTextSharp creates that text line like this:
buf.Append("Digitally signed by ");
String name = null;
CertificateInfo.X509Name x500name = CertificateInfo.GetSubjectFields((X509Certificate)signCertificate);
if (x500name != null) {
name = x500name.GetField("CN");
if (name == null)
name = x500name.GetField("E");
}
if (name == null)
name = "";
buf.Append(name).Append('\n');
(excerpt from PdfSignatureAppearance.GetAppearance())
signCertificate here is the BouncyCastle X509Certificate otherwise accessible via the PdfSignatureAppearance property Certificate.
But in your code you do not set that appearance.Certificate property. Thus, in the code above x500Name is null. Consequentially name is "" and no name is added after "Digitally signed by ".
If you want that field to be automatically filled in by iText, therefore, provide the necessary information and set appearance.Certificate. Alternatively you can generate the appearance yourself.
Related
This is code.
public void Sign(string SigReason, string SigContact, string SigLocation, bool visible)
{
string password = #"123";
PdfReader reader = new PdfReader(this.inputPDF,new System.Text.ASCIIEncoding().GetBytes(password));
//Activate MultiSignatures
PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0', null, true);
//To disable Multi signatures uncomment this line : every new signature will invalidate older ones !
//PdfStamper st = PdfStamper.CreateSignature(reader, new FileStream(this.outputPDF, FileMode.Create, FileAccess.Write), '\0');
//string pdfTemplate = Server.MapPath("~/Data/PADForm.pdf");
//string newFile = Server.MapPath("~/Data/newFile.pdf");
//signPdfFile(sourceDocument, destinationPath, stream, "123", "testing", "testing");
//PdfReader pdfReader = new PdfReader(pdfTemplate, new System.Text.ASCIIEncoding().GetBytes(password));
//PdfStamper pdfStamper = new PdfStamper(reader, new FileStream(this.outputPDF, FileMode.Create));
string imageURL = #"C:\Users\amitrs\Desktop\FSI SS\204-25.png";
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL);
// Resize image depend upon your need
jpg.ScaleToFit(140f, 120f);
//Give space before image
jpg.SpacingBefore = 10f;
//Give some space after the image
jpg.SpacingAfter = 1f;
jpg.Alignment = Element.ALIGN_LEFT;
AcroFields pdfFormFields = st.AcroFields;
pdfFormFields.SetField("DigitalSignature", "Amit");
pdfFormFields.SetField("Signature", "Amit Saini");
st.MoreInfo = this.metadata.getMetaData();
st.XmpMetadata = this.metadata.getStreamedMetaData();
PdfSignatureAppearance sap = st.SignatureAppearance;
sap.Layer2Text = "amit saini";
sap.SetCrypto(this.myCert.Akp, this.myCert.Chain, null, PdfSignatureAppearance.VERISIGN_SIGNED);
sap.Reason = SigReason;
sap.Contact = SigContact;
sap.Location = SigLocation;
if (visible)
sap.SetVisibleSignature(new iTextSharp.text.Rectangle(100, 100, 250, 300), 1, null);
st.Close();
}
i'm using iTextSharp to create a pdf document then add it as an attachment to send an email using SendGrid.
The code is working locally but after deploying the project in Azure this function stopped working for some reason. I tried to analyze the problem and i think that the document didn't fully created of attached due to the connection. I can't pin point the exact issue to solve it. Any opinions or discussion is appreciated.
Action:
public async Task<IActionResult> GeneratePDF(int? id, string recipientEmail)
{
//if id valid
if (id == null)
{
return NotFound();
}
var story = await _db.Stories.Include(s => s.Child).Include(s => s.Sentences).ThenInclude(s => s.Image).FirstOrDefaultAsync(s => s.Id == id);
if (story == null)
{
return NotFound();
}
var webRootPath = _hostingEnvironment.WebRootPath;
var path = Path.Combine(webRootPath, "dump"); //folder name
try
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 10);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
string usedFont = Path.Combine(webRootPath + "\\fonts\\", "arial.TTF");
BaseFont bf = BaseFont.CreateFont(usedFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font titleFont = new iTextSharp.text.Font(bf, 40);
iTextSharp.text.Font sentencesFont = new iTextSharp.text.Font(bf, 15);
iTextSharp.text.Font childNamewFont = new iTextSharp.text.Font(bf, 35);
PdfPTable T = new PdfPTable(1);
//Hide the table border
T.DefaultCell.BorderWidth = 0;
T.DefaultCell.HorizontalAlignment = 1;
T.DefaultCell.PaddingTop = 15;
T.DefaultCell.PaddingBottom = 15;
//Set RTL mode
T.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
//Add our text
if (story.Title != null)
{
T.AddCell(new iTextSharp.text.Paragraph(story.Title, titleFont));
}
if (story.Child != null)
{
if (story.Child.FirstName != null && story.Child.LastName != null)
{
T.AddCell(new iTextSharp.text.Phrase(story.Child.FirstName + story.Child.LastName, childNamewFont));
}
}
if (story.Sentences != null)
{
.................
}
document.Add(T);
writer.CloseStream = false;
document.Close();
byte[] bytes = memoryStream.ToArray();
var fileName = path + "\\PDF" + DateTime.Now.ToString("yyyyMMdd-HHMMss") + ".pdf";
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
memoryStream.Position = 0;
memoryStream.Close();
//Send generated pdf as attchment
// Create the file attachment for this email message.
var attachment = Convert.ToBase64String(bytes);
var client = new SendGridClient(Options.SendGridKey);
var msg = new SendGridMessage();
msg.From = new EmailAddress(SD.DefaultEmail, SD.DefaultEmail);
msg.Subject = story.Title;
msg.PlainTextContent = "................";
msg.HtmlContent = "..................";
msg.AddTo(new EmailAddress(recipientEmail));
msg.AddAttachment("Story.pdf", attachment);
try
{
await client.SendEmailAsync(msg);
}
catch (Exception ex)
{
Console.WriteLine("{0} First exception caught.", ex);
}
//Remove form root
if (System.IO.File.Exists(fileName))
{
System.IO.File.Delete(fileName);
}
}
}
catch (FileNotFoundException e)
{
Console.WriteLine($"The file was not found: '{e}'");
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine($"The directory was not found: '{e}'");
}
catch (IOException e)
{
Console.WriteLine($"The file could not be opened: '{e}'");
}
return RedirectToAction("Details", new { id = id });
}
try to edit usedfont variable as bellow :
var usedfont = Path.Combine(webRootPath ,#"\fonts\arial.TTF")
It turns out that the problem is far from iTextSharp. I did a remote debugging from this article.
Two parts of the code was causing the problem.
First, for some reason the folder "dump" was not created on Azure wwwroot folder while locally it is. so, i added these lines:
var webRootPath = _hostingEnvironment.WebRootPath;
var path = Path.Combine(webRootPath, "dump");
if (!Directory.Exists(path)) //Here
Directory.CreateDirectory(path);
Second, after debugging it shows that creating the file was failing every time. I replaced the following lines:
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
memoryStream.Position = 0;
memoryStream.Close();
With:
using (FileStream fs = new FileStream(fileName, FileMode.Create))
using (var binaryWriter = new BinaryWriter(fs))
{
binaryWriter.Write(bytes, 0, bytes.Length);
binaryWriter.Close();
}
memoryStream.Close();
Hope this post helps someone.
I am working on a PDF file, which is a secured one and an excel is attached in the PDF file.
The following is the code i tried.
static void Main(string[] args)
{
Program pgm = new Program();
pgm.EmbedAttachments();
//pgm.ExtractAttachments(pgm.pdfFile);
}
private void ExtractAttachments(string _pdfFile)
{
try
{
if (!Directory.Exists(attExtPath))
Directory.CreateDirectory(attExtPath);
byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("TFAER13052016");
//byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("Password");
PdfDictionary documentNames = null;
PdfDictionary embeddedFiles = null;
PdfDictionary fileArray = null;
PdfDictionary file = null;
PRStream stream = null;
//PdfReader reader = new PdfReader(_pdfFile);
PdfReader reader = new PdfReader(_pdfFile, password);
PdfDictionary catalog = reader.Catalog;
documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES));
if (documentNames != null)
{
embeddedFiles = (PdfDictionary)PdfReader.GetPdfObject(documentNames.Get(PdfName.EMBEDDEDFILES));
if (embeddedFiles != null)
{
PdfArray filespecs = embeddedFiles.GetAsArray(PdfName.NAMES);
for (int i = 0; i < filespecs.Size; i++)
{
i++;
fileArray = filespecs.GetAsDict(i);
file = fileArray.GetAsDict(PdfName.EF);
foreach (PdfName key in file.Keys)
{
stream = (PRStream)PdfReader.GetPdfObject(file.GetAsIndirectObject(key));
string attachedFileName = fileArray.GetAsString(key).ToString();
byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream);
System.IO.File.WriteAllBytes(attExtPath + attachedFileName, attachedFileBytes);
}
}
}
else
throw new Exception("Unable to Read the attachment or There may be no Attachment");
}
else
{
throw new Exception("Unable to Read the document");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadKey();
}
}
private void EmbedAttachments()
{
try
{
if (File.Exists(pdfFile))
File.Delete(pdfFile);
Document PDFD = new Document(PageSize.LETTER);
PdfWriter writer;
writer = PdfWriter.GetInstance(PDFD, new FileStream(pdfFile, FileMode.Create));
PDFD.Open();
PDFD.NewPage();
PDFD.Add(new Paragraph("This is test"));
PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(writer, #"C:\PDFReader\1.xls", "11.xls", null);
//PdfFileSpecification pfs = PdfFileSpecification.FileEmbedded(writer, attFile, "11", File.ReadAllBytes(attFile), true);
writer.AddFileAttachment(pfs);
//writer.AddAnnotation(PdfAnnotation.CreateFileAttachment(writer, new iTextSharp.text.Rectangle(100, 100, 100, 100), "File Attachment", PdfFileSpecification.FileExtern(writer, "C:\\test.xml")));
//writer.Close();
PDFD.Close();
Program pgm=new Program();
using (Stream input = new FileStream(pgm.pdfFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(pgm.epdfFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "Password", "secret", PdfWriter.ALLOW_SCREENREADERS);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace.ToString());
Console.ReadKey();
}
}
}
The above code contains the creation of a encrypted PDF with an excel attachment and also to extract the same.
Now the real problem is with the file which I already have as a requirement document(I cannot share the file) which also has an excel attachment like my example.
But the above code works for the secured PDF which i have created but not for the actual Secured PDF.
While debugging, I found that the Issue is with the following code
documentNames = (PdfDictionary)PdfReader.GetPdfObject(catalog.Get(PdfName.NAMES));
In which,
catalog.Get(PdfName.NAMES)
is returned as NULL, Where as the File created by me, provides the expected output.
Please guide me on the above.
TIA.
As mkl suggested, It has been attached as an Annotated attachment. But the reference which is used in the example is provided ZipFile Method is no longer supported. Hence I found an alternate code attached below.
public void ExtractAttachments(byte[] src)
{
PRStream stream = null;
string attExtPath = #"C:\PDFReader\Extract\";
if (!Directory.Exists(attExtPath))
Directory.CreateDirectory(attExtPath);
byte[] password = System.Text.ASCIIEncoding.ASCII.GetBytes("TFAER13052016");
PdfReader reader = new PdfReader(src, password);
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS);
if (array == null) continue;
for (int j = 0; j < array.Size; j++)
{
PdfDictionary annot = array.GetAsDict(j);
if (PdfName.FILEATTACHMENT.Equals(
annot.GetAsName(PdfName.SUBTYPE)))
{
PdfDictionary fs = annot.GetAsDict(PdfName.FS);
PdfDictionary refs = fs.GetAsDict(PdfName.EF);
foreach (PdfName name in refs.Keys)
{
//zip.AddEntry(
// fs.GetAsString(name).ToString(),
// PdfReader.GetStreamBytes((PRStream)refs.GetAsStream(name))
//);
stream = (PRStream)PdfReader.GetPdfObject(refs.GetAsIndirectObject(name));
string attachedFileName = fs.GetAsString(name).ToString();
var splitname = attachedFileName.Split('\\');
if (splitname.Length != 1)
attachedFileName = splitname[splitname.Length - 1].ToString();
byte[] attachedFileBytes = PdfReader.GetStreamBytes(stream);
System.IO.File.WriteAllBytes(attExtPath + attachedFileName, attachedFileBytes);
}
}
}
}
}
Please Let me Know if it can be achieved in any other way.
Thanks!!!
I have an existing pdf .I am using itextSharp to open the document and highlight keywords dynamically and when I save this into a file it works fine, but when I write it into a memory Stream and try to render it on the browser the highlights are not there.
Here is the code
public void SearchPDF()
{
//Create a new file from our test file with highlighting
string highLightFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Highlighted.pdf");
// Stream
//Bind a reader and stamper to our test PDF
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "201400699428__1_00000.pdf");
PdfReader reader = new PdfReader(testFile);
var numberOfPages = reader.NumberOfPages;
System.Globalization.CompareOptions cmp = System.Globalization.CompareOptions.None;
//Create an instance of our strategy
MemoryStream m = new MemoryStream();
//using (var fs = new FileStream(highLightFile, FileMode.Create, FileAccess.Write, FileShare.None))
//{
using (Document document = new Document(PageSize.A4))
{
PdfWriter.GetInstance(document, m);
using (PdfStamper stamper = new PdfStamper(reader, m))
{
//document.Open();
for (var currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
MyLocationTextExtractionStrategy strategyTest = new MyLocationTextExtractionStrategy("Ritual Bath", cmp);
var listener = new MyRenderListener();
ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
//Parse page 1 of the document above
using (var r = new PdfReader(testFile))
{
var ex = PdfTextExtractor.GetTextFromPage(r, currentPageIndex, strategyTest);
}
//Loop through each chunk found
foreach (var p in strategyTest.myPoints)
{
//Console.WriteLine(string.Format("Found text {0} at {1}x{2}", p.Text, p.Rect.Left, p.Rect.Bottom));
float[] quad = { p.Rect.Left, p.Rect.Bottom, p.Rect.Right, p.Rect.Bottom, p.Rect.Left, p.Rect.Top, p.Rect.Right, p.Rect.Top };
Rectangle rect = new Rectangle(p.Rect.Left,
p.Rect.Top,
p.Rect.Bottom,
p.Rect.Right);
PdfAnnotation highlight = PdfAnnotation.CreateMarkup(stamper.Writer, rect, null, PdfAnnotation.MARKUP_HIGHLIGHT, quad);
//Set the color
highlight.Color = BaseColor.YELLOW;
//Add the annotation
stamper.AddAnnotation(highlight, 1);
}
}
}
}
//}
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment; filename=download_report.pdf");
//Response.BinaryWrite(byteContent);
//Response.End();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "inline;filename=download_report.pdf");
HttpContext.Current.Response.AppendHeader("Content-Length", m.ToArray().Length.ToString());
HttpContext.Current.Response.OutputStream.Write(m.ToArray(), 0, m.ToArray().Length);
HttpContext.Current.Response.OutputStream.Flush();
HttpContext.Current.Response.OutputStream.Close();
HttpContext.Current.Response.End();
}
I have a method that fixes my document so it opens on Page Level Zoom, the bookmarks are retained but I can't seem to keep my named destinations (anchors) in my new document. I commented in my method what I thought would fix the problem, I used the code before in another method.
private static void FixZoom(string source)
{
var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));
var size = reader.GetPageSizeWithRotation(1);
using (var document = new iTextSharp.text.Document(size))
{
var path = String.Empty;
var dirName = Path.GetDirectoryName(source);
if (dirName != null)
{
path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
}
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
using (var writer = PdfWriter.GetInstance(document, stream))
{
document.Open();
var bookmarks = SimpleBookmark.GetBookmark(reader);
var cb = writer.DirectContent;
var pdfDest = new PdfDestination(PdfDestination.FIT);
var action = PdfAction.GotoLocalPage(1, pdfDest, writer);
for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
{
document.SetPageSize(reader.GetPageSizeWithRotation(pageNumber));
document.NewPage();
var page = writer.GetImportedPage(reader, pageNumber);
cb.AddTemplate(page, 0, 0);
}
//BEGIN: This is not working
var map = SimpleNamedDestination.GetNamedDestination(reader, false);
if (map.Count > 0)
{
writer.AddNamedDestinations(map, reader.NumberOfPages);
}
//END: This is not working
var pageMode = 0;
pageMode += PdfWriter.PageLayoutOneColumn;
writer.SetOpenAction(action);
writer.ViewerPreferences = pageMode;
writer.Outlines = bookmarks;
document.Close();
}
}
File.Delete(source);
File.Move(path, source);
}
}
After the comment I fixed it with this code.
private static void FixZoom(string source)
{
var reader = new PdfReader(new FileStream(source, FileMode.Open, FileAccess.Read));
var size = reader.GetPageSizeWithRotation(1);
using (var document = new iTextSharp.text.Document(size))
{
var path = String.Empty;
var dirName = Path.GetDirectoryName(source);
if (dirName != null)
{
path = Path.Combine(dirName, "Zoom" + Path.GetFileName(source));
}
using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
using (var stamper = new PdfStamper(reader, stream))
{
document.Open();
var pdfDestination = new PdfDestination(PdfDestination.FIT);
var pdfAction = PdfAction.GotoLocalPage(1, pdfDestination, stamper.Writer);
stamper.Writer.SetOpenAction(pdfAction);
document.Close();
}
}
File.Delete(source);
File.Move(path, source);
}
}