I am creating a .net (c#) application to merge several PDFs to one pdf
using iTextSharp dll.
When I open this merged file from my folder where it is saved, it opens
successfully, but when I import it in an Electronic Medical Record (EMR)
System and read, it gives file corrupt error.
If I compare the properties of this file created using iTextSharp to
another pdf file which can be imported and read in the EMR, I see
difference in the PDF Version. iTextSharp PDF Version is 1.4 while the
other file has pdf version 1.3.
This is how I am creating the merged file using itextSharp. Please let
me know what I can do to create a pdf that can be imported and read from
within the EMR system.
1. For each case
a. Read binary data in a Byte Array:
i. Byte[] FileData =
(byte[])dr["DocumentBinary"];
ii.
PDFFileName = _MergedPDFSavePath + dr["FileReportName"].ToString();
b. Write Byte Array to a file:
i. FileStream fs = new FileStream(fileName,
FileMode.Create, FileAccess.ReadWrite);
ii. BinaryWriter bw = new BinaryWriter(fs);
iii. bw.Write(buff);
2. Merge all files created above:
a. PdfReader reader = new PdfReader(ReportFiles[0]);
b. Document doc = new Document(reader.GetPageSizeWithRotation(1));
c. PdfMerge PM = new PdfMerge();
d. PdfMerge.MergeFiles(TargetFile, ReportFiles);
PDFMerge.cs code copied at the end of this message.
3. Create save as dialogue to enable the client to save merged file:
a. Response.ContentType = "application/pdf";
b. Response.AddHeader("content-disposition", "attachment; filename=" +
FName);
c. FileStream sourceFile = new FileStream(TargetFile, FileMode.Open);
d. long FileSize;
e. FileSize = sourceFile.Length;
f. byte[] getContent = new byte[(int)FileSize];
g. sourceFile.Read(getContent, 0, (int)sourceFile.Length);
h. sourceFile.Close();
i. Response.BinaryWrite(getContent);
Thanks,
Dipendra Kaur
Software Developer
CSI Laboratories
770 817 0817 X 382
PDFMerge.cs code:
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
public class PdfMerge
{
public static void MergeFiles(string destinationFile, string[]
sourceFiles)
{
try
{
int f = 0;
// we create a reader for a certain document
PdfReader reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
int n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in the
original file.");
// step 1: creation of a document-object
Document document = new
Document(reader.GetPageSizeWithRotation(1));
// step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new
FileStream(destinationFile, FileMode.Create));
// step 3: we open the document
document.Open();
PdfContentByte cb = writer.DirectContent;
PdfImportedPage page;
int rotation;
// step 4: we add content
while (f < sourceFiles.Length)
{
int i = 0;
while (i < n)
{
i++;
document.SetPageSize(reader.GetPageSizeWithRotation(i));
document.NewPage();
page = writer.GetImportedPage(reader, i);
rotation = reader.GetPageRotation(i);
if (rotation == 90 || rotation == 270)
{
cb.AddTemplate(page, 0, -1f, 1f, 0, 0,
reader.GetPageSizeWithRotation(i).Height);
}
else
{
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
//Console.WriteLine("Processed page " + i);
}
f++;
if (f < sourceFiles.Length)
{
reader = new PdfReader(sourceFiles[f]);
// we retrieve the total number of pages
n = reader.NumberOfPages;
//Console.WriteLine("There are " + n + " pages in
the original file.");
}
}
// step 5: we close the document
document.Close();
}
catch (Exception e)
{
string strOb = e.Message;
}
}
public int CountPageNo(string strFileName)
{
// we create a reader for a certain document
PdfReader reader = new PdfReader(strFileName);
// we retrieve the total number of pages
return reader.NumberOfPages;
}
}
------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of $1 Million in cash or HP Products. Visit us here for more details:
http://ad.doubleclick.net/clk;226879339;13503038;l?
http://clk.atdmt.com/CRS/go/247765532/direct/01/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/