Converting Multiple Images to PDF with iTextSharp - c#

I am trying to convert a series of images taken from OpenFileDialog() to one PDF using iTextSharp in C#. Here is what I'm running. When I select files I get the error: "System.NotSupportedException: 'Stream does not support reading.'"
on the line :
var image = iTextSharp.text.Image.GetInstance(imageStream);
I am wondering what this error means, or what I could fix to resolve the issue. Thanks.
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private OpenFileDialog ofd = new OpenFileDialog { Multiselect = true, Filter = "Image files | * .jpg;*.jpeg;*.png;" };
// Open file button
private void button1_Click(object sender, EventArgs e)
{
// Run code only if a file is selected
if (ofd.ShowDialog() == DialogResult.OK)
{
Document doc = new Document();
using (var stream = new FileStream("test.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
{
foreach (String file in ofd.SafeFileNames)
{
using (var imageStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
PdfWriter.GetInstance(doc, stream);
doc.Open();
var image = iTextSharp.text.Image.GetInstance(imageStream);
doc.Add(image);
doc.Close();
}
}
}
}
}
}
}
Here is the main code:
namespace WindowsFormsApp2
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

Try the code below, I've moved the opening and closing of doc outside of the foreach loop. Calling doc.Close() closes the parent file stream, so it's important to only call doc.Close() once all the images have been added.
using (var stream = File.Create("test.pdf"))
using (var doc = new Document())
using (var pdfWriter = PdfWriter.GetInstance(doc, stream))
{
doc.Open();
foreach (var file in ofd.SafeFileNames)
{
using (var imageStream = File.OpenRead(file))
{
var image = Image.GetInstance(imageStream);
doc.Add(image);
}
}
doc.Close();
}

Related

Pdf Merge with itext sharp

I am trying to create a desktop application combining existing pdf files into one.
I found some code that helps me with my design and selecting the files and merging them but my code creates pdf files and then sends the new file to the desktop. I need my code to grab existing pdf files and merging them together to create a file with those files together and have it sent to my desktop. Attached is my code, please let me know what I need to fix. I am very new to C# I understand basics but I am unsure where to change things and how to in this certain area.
namespace mergePdf
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnMerge_Click(object sender, EventArgs e)
{
//Folder that we'll work from
string workingFolder =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string pdf1 = Path.Combine(workingFolder, "pdf1.pdf");//PDF
with solid red background color
string pdf2 = Path.Combine(workingFolder, "pdf2.pdf");//PDF
with text
string pdf3 = Path.Combine(workingFolder, "pdf3.pdf");
//Merged PDF
//Create a basic PDF filled with red, nothing special
using (FileStream fs = new FileStream(pdf1, FileMode.Create,
FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc,
fs))
{
doc.Open();
PdfContentByte cb = writer.DirectContent;
cb.SetColorFill(BaseColor.RED);
cb.Rectangle(0, 0, doc.PageSize.Width,
doc.PageSize.Height);
cb.Fill();
doc.Close();
}
}
}
//Create a basic PDF with a single line of text, nothing
special
using (FileStream fs = new FileStream(pdf2, FileMode.Create,
FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc,
fs))
{
doc.Open();
doc.Add(new Paragraph("This is a test"));
doc.Close();
}
}
}
//Create a basic PDF
using (FileStream fs = new FileStream(pdf3, FileMode.Create,
FileAccess.Write, FileShare.None))
{
using (Document doc = new Document(PageSize.LETTER))
{
using (PdfWriter writer = PdfWriter.GetInstance(doc,
fs))
{
doc.Open();
//Get page 1 of the first file
PdfImportedPage imp1 = writer.GetImportedPage(new
PdfReader(pdf1), 1);
//Get page 2 of the second file
PdfImportedPage imp2 = writer.GetImportedPage(new
PdfReader(pdf2), 1);
//Add the first file to coordinates 0,0
writer.DirectContent.AddTemplate(imp1, 0, 0);
//Since we don't call NewPage the next call will
operate on the same page
writer.DirectContent.AddTemplate(imp2, 0, 0);
doc.Close();
}
}
}
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
textBoxPdfFile1Path.Text =
System.IO.Path.Combine(Application.StartupPath,
#"C:\Users\jesse\Downloads");
textBoxPdfFile2Path.Text =
System.IO.Path.Combine(Application.StartupPath,
#"C:\Users\jesse\Downloads");
}
private void btnSelectFile1_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
if (fd.ShowDialog() == DialogResult.OK)
{
textBoxPdfFile1Path.Text = fd.FileName;
}
}
private void btnSelectFile2_Click(object sender, EventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
if (fd.ShowDialog() == DialogResult.OK)
{
textBoxPdfFile2Path.Text = fd.FileName;
}
}
}
}
I expect the output to combine existing files into one file which will be sent to my desktop. Right now it creates two pdf sample files and combines them but I have no idea how to select from existing instead.
Using the below code should do what you want as far merging the documents.
To get a list of the actual paths to the individual pdfs is up to you on how to do.
Once the FileStream is closed your document should be created at the specified 'newPdfPath' path.
using (FileStream stream = new FileStream(newPdfPath, FileMode.Create))
{
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
PdfReader reader = null;
document.Open();
foreach (var item in listOfPathsToPDFs)
{
reader = new PdfReader(item);
pdf.AddDocument(reader);
reader.Close();
}
document.Close();
}

ITextSharp Save PDF without Fields

I want to save a PDF file in a flatted mode. I have tried all the online solution but never works in my code.
I have a file generated with ITextSharp and Acrofields. When i try to set the option pdf.FormFlattening = true; it doesn't work for my solution.
The file is generated from an "Adobe Static PDF form" created with Adobe Livecycle Designer
This is my function:
using (var streamPdf = new MemoryStream())
{
PdfStamper pdf;
using (var pdfReader = new PdfReader(Application.StartupPath + PathToFile + Template))
using (pdf = new PdfStamper(pdfReader, streamPdf))
{
AcroFields pdfFormFields = pdf.AcroFields;
foreach (var field in fields)
{
pdfFormFields.SetField(field.FieldName, field.FieldValue.Trim());
}
}
foreach (var de in pdfReader.AcroFields.Fields)
{
pdfFormFields.SetFieldProperty(de.Key.ToString(),"setfflags",PdfFormField.FF_READ_ONLY,null);
}
pdf.FormFlattening = true;
pdf.AcroFields.GenerateAppearances = true;
pdf.Close();
_pdf = streamPdf.ToArray();
}
string filename = "name.pdf";
using (var fs = new FileStream(filename, FileMode.Create))
{
fs.Write(_pdf, 0, _pdf.Count());
fs.Close();
}
The file generated is a static PDF and i need to have a PDF without Fields.
Thanks to all

Assign/write Title for pdf files in a directory

Hi I have a C# code snippet which would assign title to the pdf file.But I am trying to do the same for each and every pdf files in a directory.Can any one help me...?
Following is the code snippet
"
PdfReader pdfReader = new PdfReader(filePath);
using (FileStream fileStream = new FileStream(newFilePath, FileMode.Create, FileAccess.Write))
{
string title = pdfReader.Info["Title"] as string;
Trace.WriteLine("Existing title: " + title);
PdfStamper pdfStamper = new PdfStamper(pdfReader, fileStream);
Hashtable newInfo = pdfReader.Info;
newInfo["Title"] = "New title";
pdfStamper.MoreInfo = newInfo;
pdfReader.Close();
pdfStamper.Close();
}
"
Try this code, hope that works for you...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inputFile = Path.Combine(workingFolder, "Input.pdf");
string outputFile = Path.Combine(workingFolder, "Output.pdf");
PdfReader reader = new PdfReader(inputFile);
using(FileStream fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)){
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
Dictionary<String, String> info = reader.Info;
info.Add("Title", "New title");
stamper.MoreInfo = info;
stamper.Close();
}
}
this.Close();
}
}
}

Remove metadata from existing Pdf using iTextsharp

I created a pdf and added a metadata into it and also encrypted it uisng iTextsharp library.
Now I want to remove the encryption from the pdf. I successfully did so using iTextSharp but was not able to remove the metadata that I added.
Can anyone please giude me how can I remove the metadata. Its urgent.
Thanks.
When removing meta data it is easiest to work directly with the PdfReader object. Once you do that you can write that back to disk. The code below is a full working C# 2010 WinForms application targeting iTextSharp 5.1.2.0. It first creates a PDF with some meta data, then it modifies an in-memory version of the PDF using a PdfReader, and finally writes the changes to disk. See the code for additional comments.
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
//File with meta data added
string InputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
//File with meta data removed
string OutputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Output.pdf");
//Create a file with meta data, nothing special here
using (FileStream FS = new FileStream(InputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document Doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(Doc, FS)) {
Doc.Open();
Doc.Add(new Paragraph("Test"));
//Add a standard header
Doc.AddTitle("This is a test");
//Add a custom header
Doc.AddHeader("Test Header", "This is also a test");
Doc.Close();
}
}
}
//Read our newly created file
PdfReader R = new PdfReader(InputFile);
//Loop through each piece of meta data and remove it
foreach (KeyValuePair<string, string> KV in R.Info) {
R.Info.Remove(KV.Key);
}
//The code above modifies an in-memory representation of the PDF, we need to write these changes to disk now
using (FileStream FS = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document Doc = new Document()) {
//Use the PdfCopy object to copy each page
using (PdfCopy writer = new PdfCopy(Doc, FS)) {
Doc.Open();
//Loop through each page
for (int i = 1; i <= R.NumberOfPages; i++) {
//Add it to the new document
writer.AddPage(writer.GetImportedPage(R, i));
}
Doc.Close();
}
}
}
this.Close();
}
}
}

Loading RTF file with images to FlowDocument in console application

I am creating simple console application where I need to load RTF file to FlowDocument for further work.
I am using this code for loading file to FlowDocument:
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".rtf";
dlg.Filter = "RichText Files (*.rtf)|*.rtf";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
FlowDocument flowDocument = new FlowDocument();
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
textRange.Load(fileStream, DataFormats.Rtf);
}
}
If I do this in WPF application and show the document in flowDocumentPageViewer, everything is OK. But if I try to load file in console application, I get exception: Unrecognized structure in data format Rich Text Box, parameter name stream.
And for some reason, this exception appears only, if there is image in document.
Any ideas what's wrong or better how to solve it?
Problem was in using System.Windows namesapce for DataFormats. With System.Windows.Forms its functional.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Documents;
namespace RtfTest
{
class Program
{
[STAThread]
static void Main(string[] args)
{
DoRead();
}
private static void DoRead()
{
// Create OpenFileDialog
OpenFileDialog dlg = new OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".rtf";
dlg.Filter = "RichText Files (*.rtf)|*.rtf";
// Display OpenFileDialog by calling ShowDialog method
var result = dlg.ShowDialog();
try
{
if (result == DialogResult.OK)
{
// Open document
string filename = dlg.FileName;
FlowDocument flowDocument = new FlowDocument();
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
textRange.Load(fileStream, DataFormats.Rtf);
}
}
}
catch (Exception)
{
throw;
}
}
}
}

Categories

Resources