With the following code, I create a PDF document from a tif file, if necessary, but when I save the file and some of its pages have color like ink, image or something, the pages turn out completely black. Am I doing something wrong?
public PdfLoadedDocument GetPDF(FileInfo FOrigen, String tempfile = "", bool EliminaORigen = false)
{
PdfLoadedDocument _pdfloadedDocument;
try
{
if(FOrigen.Extension.ToLower() == ".pdf")
System.IO.File.Copy(FOrigen.FullName, tempfile);
else
{
PdfDocument _pdfDocument = new PdfDocument();
_pdfDocument.PageSettings.Margins.All = 0;
//Get the image stream and draw frame by frame
using (var tiffImage = new PdfBitmap(FOrigen.FullName))
{
int frameCount = tiffImage.FrameCount;
for (int i = 0; i < frameCount; i++)
{
//Add pages to the document
var page = _pdfDocument.Pages.Add();
//Getting page size to fit the image within the page
SizeF pageSize = page.GetClientSize();
//Selecting frame in TIFF
tiffImage.ActiveFrame = i;
//Draw TIFF frame
page.Graphics.DrawImage(tiffImage, 0, 0, pageSize.Width, pageSize.Height);
}
tiffImage.Dispose();
}
_pdfDocument.Compression = PdfCompressionLevel.Best;
_pdfDocument.Save(tempfile);
_pdfDocument.Close();
}
_pdfloadedDocument = new PdfLoadedDocument(tempfile);
if (EliminaORigen && File.Exists(FOrigen.FullName))
File.Delete(FOrigen.FullName);
return _pdfloadedDocument;
}
catch(Exception)
{
if (EliminaORigen && File.Exists(FOrigen.FullName))
File.Delete(FOrigen.FullName);
throw;
}
}
-----------------------------------
private static void SaveOpenpdf(PdfLoadedDocument _pdfloadedDocument, String temppdf)
{
_pdfloadedDocument.Compression = PdfCompressionLevel.Best;
PdfCompressionOptions options = new PdfCompressionOptions
{
OptimizeFont = true,
OptimizePageContents = true,
CompressImages = true,
ImageQuality = 10
};
_pdfloadedDocument.CompressionOptions = options;
_pdfloadedDocument.Save(temppdf);
_pdfloadedDocument?.Close();
}