Method of Transforming TIF Image to PDF File by C
- 2021-07-06 11:41:03
- OfStack
This paper describes the method of C # to convert TIF image to PDF file with an example. Share it for your reference. The specific implementation method is as follows:
Here's how to use TIFtoPDF. This tool can combine multiple TIF image files into one PDF file
TIFtoPDF. rar file click here to download.
The file Program. cs is as follows:
using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;
namespace TIFtoPDF
{
class Program
{
// Will be multiple tif Files merged into 1 A pdf Documents
private static void tifToPdf(IEnumerable<string> arr, string sFilePdf)
{
FileInfo _toFile = new FileInfo(sFilePdf);
// Create 1 Document objects
Document doc = new Document(PageSize.A3, 0, 0, 0, 0);
int pages = 0;
FileStream fs=new FileStream(sFilePdf,FileMode.OpenOrCreate);
// Define the output location and load the document object into the output object
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
// Open a document object
doc.Open();
foreach(string sFileTif in arr)
{
PdfContentByte cb = writer.DirectContent;
RandomAccessFileOrArray ra = new RandomAccessFileOrArray(sFileTif);
int comps = TiffImage.GetNumberOfPages(ra);
for (int c = 0; c < comps; ++c)
{
Image img = TiffImage.GetTiffImage(ra, c + 1);
if (img != null)
{
img.ScalePercent(7200f / img.DpiX, 7200f / img.DpiY);
doc.SetPageSize(new Rectangle(img.ScaledWidth, img
.ScaledHeight));
img.SetAbsolutePosition(0,0);
cb.AddImage(img);
doc.NewPage();
++pages;
}
}
ra.Close();// Shut down
}
// Close the document object and free the resource
doc.Close();
}
public static void Main(string[] args)
{
tifToPdf(new string[]{@"C:\test.tif"},@"C:\test.pdf");
}
}
}
I hope this article is helpful to everyone's C # programming.