2014-12-10 3 views
0

Я пытаюсь отобразить некоторые изображения в формате PDF с использованием iTextSharp.Its работает нормально, но моя проблема, некоторые изображения отображаются в масштабировании от его фактического размера, как показано ниже изображений,изображения вопрос отображает размер

enter image description here Кода я попытался на дисплей,

iTextSharp.text.Font fontH1 = new iTextSharp.text.Font(Currier, 18, iTextSharp.text.Font.BOLD); 
Document doc1 = new Document(); 
string path1 = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments); 
path1 = path1 + "\\DOC\\Mathematicsquestions.pdf"; 
iTextSharp.text.Rectangle pageSize1 = doc1.PageSize; 
PdfWriter pdf = PdfWriter.GetInstance(doc1, new FileStream(path1, FileMode.Create)); 
doc1.Open(); 
pdf.Open(); 

PdfPTable table = new PdfPTable(2); 
//actual width of table in points 
table.TotalWidth = 500f; 
//fix the absolute width of the table 
table.LockedWidth = true; 
//relative col widths in proportions - 1/3 and 2/3 
float[] widths = new float[] { 0.15f, 2.5f }; 
table.SetWidths(widths); 
table.HorizontalAlignment = 0; 
//leave a gap before and after the table 
table.SpacingBefore = 0f; 
table.SpacingAfter = 0f; 

PdfPCell cell = new PdfPCell(new Phrase("MATHEMATICS", fontH1)); 
cell.Colspan = 2; 
cell.Border = 0; 
cell.HorizontalAlignment = 1; 
table.AddCell(cell); 

PdfPCell cell1 = new PdfPCell(new Phrase(" ")); 
cell1.Colspan = 2; 
cell1.Border = 0; 
cell1.HorizontalAlignment = 1; 
table.AddCell(cell1); 

for (int i = 0; i < mach.Count; i++) 
{ 
    string temsub = mach[i].ToString(); 
    var quepaper1 = from fm in en.Entrance_jee where fm.En_Chapter == temsub select fm; 
    foreach (Entrance_jee re in quepaper1) 
    { 
     if (newsno == 0) 
     { 
      newsno = 1; 
     } 
     else 
     { 
      newsno = newsno + 1; 
     } 
     if (re.En_Isimage == true) 
     { 
      imgepath = path + re.En_Questionpage1 + ".png"; 
      imgepath2 = path + re.En_Answer + ".png"; 
      filename = System.IO.Path.GetFileName(imgepath); 
      filename2 = System.IO.Path.GetFileName(imgepath2); 
      decfile = decfile1 + "\\R1\\CF\\" + filename; 
      decfile2 = decfile1 + "\\R1\\CF\\" + filename2; 
      string status = encobj.DecryptFile(imgepath, decfile); 
      status = encobj.DecryptFile(imgepath2, decfile2); 
      if (status == "decrypted") 
      { 
       byte[] file = File.ReadAllBytes(decfile); 
       File.Delete(decfile); 
       iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(file); 
       byte[] file2 = File.ReadAllBytes(decfile2); 
       File.Delete(decfile2); 
       iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(file2); 
       //iTextSharp.text.pdf.PdfPCell imgCell1 = new iTextSharp.text.pdf.PdfPCell(); 
       //imgCell1.AddElement(new Chunk(img2, 0, 0)); 

       table.AddCell(newsno.ToString()); 
       table.AddCell(img1); 
       table.AddCell(" "); 
       table.AddCell(img2); 
      } 
     } 
     else 
     { 
      table.AddCell(newsno.ToString()); 
      table.AddCell(re.En_Questionpage1.ToString()); 
      table.AddCell(" "); 
      table.AddCell(re.En_Answer.ToString()); 
     } 
    } 
} 

doc1.Add(table); 
doc1.Close(); 

Обновлено: Вот мои Orginal изображения,

enter image description here

enter image description here

Кто-нибудь скажет мне, где я ошибаюсь?

+0

Можете ли вы поделиться оригинальными изображениями? – mkl

+0

@mkl проверить мое обновление сейчас. – MMMMS

+0

Ответ @Kami уже указывает на проблему. Сначала я был немного удивлен, что вопрос, похоже, не был увеличен, но ваши опубликованные изображения показывают, что изображение вопроса имеет довольно много белой области справа от нее; таким образом, изображение уже использует всю ширину ячейки. – mkl

ответ

0

Изображение, по-видимому, масштабируется до полной ширины таблицы. Вы должны масштабировать его до ваших точных требований к размеру.

Try что-то вроде

iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(file); 
iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(file2); 

img1.ScaleToFit(500f, 37f); 
img2.ScaleToFit(81f, 36f); 

Или же вы можете добавить контейнер внутри клетки с требуемыми размерами и добавить изображение к нему. См. Полную информацию о - http://www.mikesdotnetting.com/article/87/itextsharp-working-with-images

+0

Ничего не влияет на ваш код !. – MMMMS

Смежные вопросы