Reputation: 7164
I'm reading a PDF file for writing a string on it like this :
public ActionResult Index(HttpPostedFileBase file)
{
byte[] pdfbytes = null;
BinaryReader rdr = new BinaryReader(file.InputStream);
pdfbytes = rdr.ReadBytes((int)file.ContentLength);
PdfReader myReader = new PdfReader(pdfbytes);
and I'm trying to pass a new file to FileStream
like this :
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
But I don't know how to pass the copied new file to fs
object. Can you help me with that? Thanks.
Upvotes: 0
Views: 1968
Reputation: 155
Try this. This program copies all pdf files from one location to another.
protected void Button1_Click(object sender, EventArgs e)
{
string sourceDirectory = @"D:\project training\source";
string targetDirectory = @"D:\project training\destiny";
Copy(sourceDirectory, targetDirectory);
}
public static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
Directory.CreateDirectory(target.FullName);
foreach (FileInfo fi in source.GetFiles())
{
if (fi.Extension.Equals(".pdf"))
{
fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
}
}
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
Upvotes: 0
Reputation: 7696
Here is example which is reading existing pdf file, copying it to new one and adding new string line:
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string originalFile = "c:\\Users\\Admin\\Desktop\\receipt mod 3.pdf";
string copyOfOriginal = "c:\\Users\\Admin\\Desktop\\newFile.pdf";
using (var reader = new PdfReader(originalFile))
{
using (var fileStream = new FileStream(copyOfOriginal, FileMode.Create, FileAccess.Write))
{
var document = new Document(reader.GetPageSizeWithRotation(1));
var writer = PdfWriter.GetInstance(document, fileStream);
document.Open();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
var importedPage = writer.GetImportedPage(reader, i);
var contentByte = writer.DirectContent;
contentByte.BeginText();
contentByte.SetFontAndSize(baseFont, 12);
var LineString = "Hello World!";
contentByte.ShowTextAligned(10,LineString,50,50,0);
contentByte.EndText();
contentByte.AddTemplate(importedPage, 0, 0);
}
document.Close();
writer.Close();
}
}
}
}
}
Upvotes: 1
Reputation: 1543
If you have access to updated byte array pass it to File.WriteAllBytes
. Or you might have an instance of PdfDocument or PdfWriter which usually allow saving the document to file on disk too. Hope it helps!
Upvotes: 1