This is a 10 page black and white document. I need to convert the document to an image format (tiff, jpeg, png, etc.) without losing resolution, make some simple modifications to the images, then convert them back to PDF. The problem that I am having is the 88k Original PDF is becoming a 2,733k multipage tiff and then, when converting back, a 9,456k PDF. All of the images look the same at the same zoom levels, and the page dimensions are equal (8.5x11).
What setting am I missing when doing these conversions back and forth that is creating these monster files? Below is the code that I am using.
- Code: Select all
#region PDf to Tiff
private void btnPDFtoTiff_Click(object sender, EventArgs e)
{
PdfDocument document = new PdfDocument();
document.LoadFromFile(txtSource.Text);
JoinTiffImages(SaveAsImage(document), @"C:\Workspace\Result.tiff", EncoderValue.CompressionLZW);
}
private static Image[] SaveAsImage(PdfDocument document)
{
Image[] images = new Image[document.Pages.Count];
for (int i = 0; i < document.Pages.Count; i++)
{
images[i] = document.SaveAsImage(i, 300, 300);
}
return images;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
public static void JoinTiffImages(Image[] images, string outFile, EncoderValue compressEncoder)
{
//use the save encoder
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(3);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compressEncoder);
ep.Param[2] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
Image pages = images[0];
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (Image img in images)
{
if (frame == 0)
{
pages = img;
//save the first frame
pages.Save(outFile, info, ep);
}
else
{
//save the intermediate frames
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
pages.SaveAdd(img, ep);
}
if (frame == images.Length - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
}
#endregion
#region Tiff to PDF
private void btnTiffToPDF_Click(object sender, EventArgs e)
{
ConvertImagetoPDF(txtSource.Text);
}
public static void ConvertImagetoPDF(String ImageFilename)
{
using (PdfDocument pdfDoc = new PdfDocument())
{
Image image = Image.FromFile(ImageFilename);
Image[] img = SplitImages(image, ImageFormat.Png);
for (int i = 0; i < img.Length; i++)
{
PdfImage pdfImg = PdfImage.FromImage(img[i]);
float width = pdfImg.Width *0.3f;
float height = pdfImg.Height *0.3f;
SizeF fPage = PdfPageSize.Letter;
PdfMargins margin = new PdfMargins();
margin.All = 0;
PdfPageBase page = pdfDoc.Pages.Add(fPage, margin);
pdfDoc.PageSettings.Margins.All = 0;
float widthFitRate = pdfImg.PhysicalDimension.Width / page.Canvas.ClientSize.Width;
float heightFitRate = pdfImg.PhysicalDimension.Height / page.Canvas.ClientSize.Height;
float fitRate = Math.Max(widthFitRate, heightFitRate);
float fitWidth = pdfImg.PhysicalDimension.Width / fitRate;
float fitHeight = pdfImg.PhysicalDimension.Height / fitRate;
page.Canvas.DrawImage(pdfImg, 0, 0, fitWidth, fitHeight);
}
string PdfFilename = @"C:\Workspace\Converted.pdf";
pdfDoc.SaveToFile(PdfFilename);
System.Diagnostics.Process.Start(PdfFilename);
}
}
public static Image[] SplitImages(Image image, ImageFormat format)
{
Guid guid = image.FrameDimensionsList[0];
FrameDimension dimension = new FrameDimension(guid);
int pageCount = image.GetFrameCount(dimension);
Image[] frames = new Image[pageCount];
for (int i = 0; i < pageCount; i++)
{
using (MemoryStream buffer = new MemoryStream())
{
image.SelectActiveFrame(dimension, i);
image.Save(buffer, format);
frames[i] = Image.FromStream(buffer);
}
}
return frames;
}
#endregion