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 the
following:
Properties shown are as follows (file A):
Title:
Author:
Subject:
Keywords:
Created: Wednesday, July 28, 2010, 8:53:59 AM
Modified: Wednesday, July 28, 2010, 8:53:59 AM
Application:
PDF Producer: iTextSharp 5.0.2(c)1T3XT BVBA
Fast web View: No
PDF version: 1.4
In the other pdf file (B) which can be imported and opened in my EMR,
the properties are as follows:
Title: Accession Result
Author: CSI Labs, Inc.
Subject:
Keywords:
Created: Tuesday, May 18, 2010, 6:28:05 PM
Modified:
Application: APVX
PDF Producer: eTelenext
Fast web View: No
PDF version: 1.3
How can I set the properties same as in file B shown above?
Is there some other setting that may be causing this problem?
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.
To make sure I tried importing the original pdf files saved from the
binary data and successfully opened in the EMR. But I can not open the
merged file created using iTextSharp.
Please help. It is urgent.
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));
PdfWriter writer =
PdfWriter.GetInstance(document, new FileStream(destinationFile,
FileMode.Create));
writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_3); // I added
this code to set pdf version, but the properties still show me version
1.4. In the debugger I saw the control going through this line of code:
// 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/
------------------------------------------------------------------------------
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://p.sf.net/sfu/dev2dev-palm
_______________________________________________
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/