I have a problem creating a new pdf from a pdf file and tiff files:
1) I insert 1 or 2 pages from a pdf file.
2) Then I insert other pages from tiff files (some tiff have only 1 page, others 2 pages)
- If I insert only tiff files with 1 page: OK
- If I insert only tiff files with 2 pages: OK
- If I insert a tiff file with 1 page and then a tiff file with 2 page: OK
- If I insert a tiff file with 2 pages and then a tiff file with 1 page: ERROR ('not enough space for an image).
Here is the code (thanks):
Code:
private Boolean TeuthTif()
{
try
{
int numeroPagine;
// Nuovo Output per il documento
PdfDocument outputDocument = new PdfDocument();
if (_pdfPrimo == "S")
{
//devo creare il documento
// Singola pagina!
outputDocument.PageLayout = PdfPageLayout.SinglePage;
// Dati base
XFont font = new XFont("Verdana", 8, XFontStyle.Bold);
XStringFormat format = new XStringFormat();
format.Alignment = XStringAlignment.Center;
format.LineAlignment = XLineAlignment.Far;
outputDocument.PageLayout = PdfPageLayout.SinglePage;
}
else
{
//punto al documento originale e aggiungo le pagine successive
// Apro documento Ori
// Nuovo Output per il documento
PdfDocument inputDocumentOri = PdfReader.Open(_pdfOri, PdfDocumentOpenMode.Modify);
outputDocument = inputDocumentOri;
}
//inserisco il pdf Teuth
XPdfForm formTeuth = XPdfForm.FromFile(_pdf1);
numeroPagine = formTeuth.PageCount;
for (int pagina = 1; pagina <= numeroPagine; pagina++)
{
// Aggiungo una pagina sul nuovo documento
PdfPage page = outputDocument.AddPage();
page.Size = PageSize.A4;
double width = page.Width;
double height = page.Height;
XGraphics gfx;
XRect box;
gfx = XGraphics.FromPdfPage(page);
box = new XRect(0, 0, width, height);
// Disegno la pagina come immagine
formTeuth.PageNumber = pagina;
gfx.DrawImage(formTeuth, box);
}
// passo i .tif del teuth
string[] files = GetFiles();
foreach (string file in files)
{
// Open the document to import pages from it.
Bitmap bm = new Bitmap(file);
int total = bm.GetFrameCount(FrameDimension.Page);
MessageBox.Show(total.ToString());
if (total == 1)
{
Bitmap bm2 = new Bitmap(file);
XImage image = XImage.FromGdiPlusImage(bm2);
PdfPage page1 = outputDocument.AddPage();
page1.Size = PageSize.A4;
XGraphics gfxT = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
gfxT.DrawImage(image, 0, 0, page1.Width, page1.Height);
gfxT.Dispose();
image.Dispose();
page1.Close();
bm2.Dispose();
}
else
{
for (Int32 k = 0; k < total; k++)
{
Bitmap bm2 = new Bitmap(file);
bm2.SelectActiveFrame(FrameDimension.Page, k);
XImage image = XImage.FromGdiPlusImage(bm2);
PdfPage page1 = outputDocument.AddPage();
//
// Get Image width, Height and Resolution and Set output document Width and Height in Inches
//page1.Width = XUnit.FromInch(image.Width / image.HorizontalResolution);
//page1.Height = XUnit.FromInch(image.Height / image.VerticalResolution);
page1.Size = PageSize.A4;
XGraphics gfxT = XGraphics.FromPdfPage(page1, XGraphicsPdfPageOptions.Append);
//gfxT.DrawImage(image, 0, 0);
gfxT.DrawImage(image, 0, 0, page1.Width, page1.Height);
gfxT.Dispose();
image.Dispose();
page1.Close();
bm2.Dispose();
}
}
}
// Salvo il documento
outputDocument.Save(_pdfOri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Anomalia in esecuzione TifPdf", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}