I am battling with some memorystream logic.
I have a method that receives a list of Id. uses them to pull pdfs from a webserver, and merges them into one pdf.
I want to then email this pdf (working in memory only)
private Stream GetWebReport(string selected_id)
{
var IdLst = selected_id.Split(',').ToList();
MemoryStream stream = new MemoryStream();
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
PdfReader reader = null;
try
{
document.Open();
foreach (var id in IdLst)
{
int i = Convert.ToInt32(id);
string invoice_url = string.Concat("http://specialurl/", id);
var urlpdf = new System.Net.WebClient().OpenRead(invoice_url);
reader = new PdfReader(urlpdf);
pdf.AddDocument(reader);
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
if (document != null)
{
document.Close();
}
}
return stream;
}
but when I try use the resulting stream for an email
var mem = GetWebReport(selected_id);
mem.Seek(0, SeekOrigin.Begin);
Attachment att = new Attachment(mem, "Report for you", "application/pdf");
I get told:
System.ObjectDisposedException: 'Cannot access a closed Stream.'
So I am sure that my itextsharp logic is good (When I use a filestream I get the correct results).
I am sure that my logic in passing streams is what is faulty
Use
PdfCopy pdf = new PdfCopy(document, stream);
pdf.CloseStream = false;
This will keep the stream open after closing the pdf to be used elsewhere.
Related
Using iTextSharp, I was able to read a base64 string and convert certain pages into local files using a FileStream. I want to do the same without saving to the local filesystem, using MemoryStream and only returning the selected pages to the calling function as a base64 string.
// function takes reader and start and end page and destination file path as parameter.
public static void ExtractPages(PdfReader pdfReader, string sourcePdfPath,
string outputPdfPath, int startPage, int endPage)
{
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage = null;
try
{
reader = pdfReader;
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
// old code to save selected pdf into local file system.
FileStream fileStream = new FileStream(outputPdfPath, FileMode.Create);
// memory stream created but not been used yet!
MemoryStream memoryStream = new MemoryStream();
pdfCopyProvider = new PdfCopy(sourceDocument, fileStream);
sourceDocument.Open();
// save selected page into local file system
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
#region TestMemoryStream
// TODO: New code to be added and return selected page data as bas64 string
#endregion
sourceDocument.Close();
reader.Close();
}
catch (Exception ex)
{
throw ex;
}
}
I was able to sort out the issue, my modified code is below.
reader = pdfReader;
sourceDocument = new Document(reader.GetPageSizeWithRotation(startPage));
//FileStream fileStream = new FileStream(outputPdfPath, FileMode.Create);
MemoryStream memoryStream = new MemoryStream();
pdfCopyProvider = new PdfCopy(sourceDocument, memoryStream);
sourceDocument.Open();
for (int i = startPage; i <= endPage; i++)
{
importedPage = pdfCopyProvider.GetImportedPage(reader, i);
pdfCopyProvider.AddPage(importedPage);
}
sourceDocument.Close();
reader.Close();
byte[] content1 = memoryStream.ToArray();
// var bas64 = Convert.ToBase64String(content1);
string bas64New = Convert.ToBase64String(content1);
I'm using iTextSharp 5.x. I'm trying to merge two pdfs and preserve the isTagged flag. When I remove copy.SetTagged(); the result pdf contains both pdfs which is great. When adding the copy.SetTagged() is get an exception
Exception -->System.ObjectDisposedException: Cannot access a closed file.
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.get_Position()
Here is the code
List<string> filesToMerge = new List<string> { "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/coverPage.pdf", "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/49W7a.pdf" };
string outputFileName = "C:/dev/dcs/wp-cla-dcs/Hex/Docs/metadata/results.pdf";
using (FileStream outFS = new FileStream(outputFileName, FileMode.Create))
using (Document document = new Document())
// using (PdfCopy copy = new PdfCopy(document, outFS))
using (PdfCopy copy = new PdfSmartCopy(document, outFS))
{
{
copy.SetTagged();
// Set up the iTextSharp document
document.Open();
foreach (string pdfFile in filesToMerge)
{
using (var reader = new PdfReader(pdfFile))
{
copy.AddDocument(reader);
copy.FreeReader(reader);
}
}
}
}
despite #bruno-lowagie's comment, I have had better results doing this with with iText5.
Uisng iText7, PdfMerger left several contents untagged (all were tagged in the source document). PdfCopy in iText5 however worked just fine, only needed to manually add Xmp metadata, title, lang, etc:
public static void CombineMultiplePDFs(string[] fileNames, string outFile)
{
var lang = "en";
var title = "My new title";
// step 1: creation of a document-object
Document document = new Document();
// step 2: we create a writer that listens to the document
FileStream newFileStream = new FileStream(outFile, FileMode.Create);
PdfCopy writer = new PdfCopy(document, newFileStream);
writer.SetTagged();
writer.PdfVersion = PdfWriter.VERSION_1_7;
writer.AddViewerPreference(PdfName.DISPLAYDOCTITLE, new PdfBoolean(true));
writer.Info.Put(PdfName.TITLE, new PdfString(title));
writer.CreateXmpMetadata();
// step 3: we open the document
document.Open();
// set meta data
document.AddLanguage(lang);
document.AddTitle(title);
// keep an array of all open readers so they can be closed again.
var readers = new PdfReader[fileNames.Length];
for (var fi = 0; fi < fileNames.Length; fi++)
{
// we create a reader for a certain document
var fileName = fileNames[0];
PdfReader reader = new PdfReader(fileName);
readers[fi] = reader;
reader.ConsolidateNamedDestinations();
// step 4: we add content
for (int i = 1; i <= reader.NumberOfPages; i++)
{
// IMPORTANT: the third param is is "KeepTaggedPdfStructure"
PdfImportedPage page = writer.GetImportedPage(reader, i, true);
writer.AddPage(page);
}
}
// step 5: we close the document and writer
writer.Close();
document.Close();
// close readers only after document is lcosed
foreach (var r in readers)
{
r.Close();
}
}
I am working on this feature which takes a pdf from a database and signs the PDF with an image of the signature which needs to be retrieved from the database. I am using iTextSharp for this, but somehow it is not working and is corrupting my PDF in the database.
This is the code of my controller
public ActionResult Approve(int? id)
{
ApplicationUser users = db.Users.Find(User.Identity.GetUserId());
Reports reports = db.Reports.Find(id);
if (reports == null || users == null) return View();
byte[] content = reports.Content;
byte[] signature = users.Signature;
iTextSharp.text.Image sigImg = iTextSharp.text.Image.GetInstance(signature);
PdfReader reader = new PdfReader(content);
using (MemoryStream ms = new MemoryStream())
{
PdfStamper stamper = new PdfStamper(reader, ms);
sigImg.SetAbsolutePosition(0f,0f);
sigImg.ScalePercent(90.0f); // 100.0f == same size
//Give some space after the image
sigImg.SpacingAfter = 1f;
sigImg.Alignment = Element.ALIGN_BOTTOM;
PdfContentByte over = stamper.GetOverContent(1);
over.AddImage(sigImg);
reports.Content = ms.ToArray();
content = reports.Content;
ms.Flush();
db.SaveChanges();
if(stamper!= null)
stamper.Close();
if(reader!= null)
reader.Close();
return File(content, "application/pdf");
// Clean up
}
}
What am I doing wrong here?
You retrieve the contents of the MemoryStream
reports.Content = ms.ToArray();
before you close the PdfStamper
if(stamper!= null)
stamper.Close();
which means the MemoryStream does not yet contain the complete stamped PDF.
Thus, change the order of commands, in particular close the stamper before retrieving the bytes from ms.
As an aside: Why do you check stamper!= null? It obviously cannot be null there...
I'm trying to use codes below to merge the pdf files in a folder and output into a new file but apparently the generated file seems corrupted.
public Boolean MergeForm(String destinationFile, String sourceFolder)
{
try
{
using (MemoryStream stream = new MemoryStream())
using (Document doc = new Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
foreach (var file in Directory.GetFiles(sourceFolder))
{
reader = new PdfReader(file);
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
}
using (FileStream streamX = new FileStream(destinationFile, FileMode.Create))
{
stream.WriteTo(streamX);
}
}
return true;
}
catch (Exception)
{
return false;
}
}
Can anyone spot on where's the problem? Thank you.
Can anyone spot on where's the problem?
Your main problem is that you use the contents of the MemoryStream before the Document and PdfCopy have finished creating the PDF (during the Dispose at the end of the using block). Thus, you save an incomplete PDF file as a result.
Doing it like this instead should work:
using (MemoryStream stream = new MemoryStream())
{
using (Document doc = new Document())
{
PdfCopy pdf = new PdfCopy(doc, stream);
pdf.CloseStream = false;
doc.Open();
PdfReader reader = null;
PdfImportedPage page = null;
foreach (var file in Directory.GetFiles(sourceFolder))
{
reader = new PdfReader(file);
for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}
pdf.FreeReader(reader);
reader.Close();
}
}
using (FileStream streamX = new FileStream(destinationFile, FileMode.Create))
{
stream.WriteTo(streamX);
}
}
BTW, you also see here that I did not put PdfCopy into a using block. This is because the Document implicitly closes the PDFCopy when it is disposed. First disposing the PdfCopy and then the Document (which tries to close the PdfCopy again), therefore, is not necessary and can result in hiding exceptions thrown from within the block by other exceptions occurring in this closing circus.
Furthermore I needed to add the pdf.CloseStream = false, otherwise the memory stream would have been closed when the PdfCopy is closed.
That been said,
Of course you should also use AddDocument instead of iterating over the document pages yourself as already explained by #Bruno.
Your memory footprint would decrease if you immediately wrote to the file stream instead of the memory stream.
I am trying top open a PDF file using iTextSharp, add a dataset to the file to prepopulate data, then save it to a stream so that I can display it to the users. I don't want to save it locally to a file. I keep getting the error "Cannot access a closed Stream." I can't figure out which stream is wrong.
Here is my code:
public FileStreamResult PushDataIntoPDFStream(string filename)
{
var reader = new PdfReader(Path.Combine(Server.MapPath(path), filename));
var xml = #"<?xml version=""1.0"" encoding=""UTF-8""?>
<form1>
<firstName>test</firstName>
<lastName>user</lastName>
<driveCar>0</driveCar>
<gender>1</gender>
<birthdate>2011-08-12</birthdate>
<numPets>4</numPets>
</form1>";
using (var outstream = new MemoryStream())
{
using (var stamper = new PdfStamper(reader, outstream))
{
var bytes = System.Text.Encoding.UTF8.GetBytes(xml);
using (var ms = new MemoryStream(bytes))
{
stamper.AcroFields.Xfa.FillXfaForm(ms);
}
}
return new FileStreamResult(outstream, "application/pdf")
{
FileDownloadName = "file.pdf";
};
}
}
The PdfStamper has a .CloseStream property on its internal output stream; try setting it to false:
using (var stamper = new PdfStamper(reader, outstream))
{
stamper.Writer.CloseStream = false;