2014-09-10 4 views
0

내 이미지를 기존 PDF에 삽입하려고합니다. 나는이 간단한 코드로 이것을 수행하고있다 :IiTextSharp가 내 이미지의 크기를 조정하고 품질을 좋지 않게 만드는 이유는 무엇입니까?

private void insertBarCodesToPDF(System.Drawing.Image barcode) 
    { 
     PdfContentByte conent = mPdfStamper.GetOverContent(2); 
     byte[] barcodeArray = (byte[]) new ImageConverter().ConvertTo(barcode, typeof(byte[])); 

     iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(barcodeArray); 
     iTextSharp.text.Rectangle rect = mPdfStamper.Reader.GetPageSize(2); 
     image.SetAbsolutePosition(rect.Right - BARCODE_ONLISTPOSITION_X, rect.Top - BARCODE_ONLISTPOSITION_Y); 
     conent.AddImage(image); 
     mPdfStamper.Close(); 
     mPdfReader.Close(); 
    } 

그러나 추가 후 나는 매우 나쁜 자격과 크기로 이미지를 얻는다. 이 체크 아웃 :

이 내가() 메소드 저장 사용하여 어딘가에 내 이미지를 저장하면 내가 무엇을 얻을 내가 필요하고 여기

What I need

그리고 것은 내가 PDF에서 무엇을 얻을

enter image description here

무엇이 문제입니까 ?? 어떤 아이디어?

P. 이미지의 원래 크기는 138x60이고 해상도는 72dpi로 설정됩니다.

+1

래스터 대신 벡터 바코드를 사용할 수 없습니까? 또는 디스크에서 가져온 정적 파일입니다. 직접 생성 할 수 없습니까? – rufanov

+2

* 해상도를 72dpi로 설정 * * 96dpi 디스플레이에 표시되거나 300dpi로 인쇄되는 경우 72dpi 이미지가 반드시 좋지 않을 수 있습니다. 즉, 추가 분석을 위해 이러한 바코드로 샘플 PDF를 공유 할 수 있습니까? – mkl

답변

1

System.Drawing.Bitmap을 만들면 절대 해상도가 고정되어있어 의도 한 해상도로 표시되지 않을 경우 항상 이상하게 보일 수 있습니다. 위에서 @mkl이 말한 것처럼 소스 이미지의 유효 DPI를 높여 볼 수 있습니다. .Net 관점에서 보면 "DPI"라는 개념을 무시하고 모든 것을 큰 규모로 수행 할 수 있습니다. 나는 690x300을주는 5로 당신의 차원을 곱하기로 시작하고 그것이 괜찮아 보이는지 볼 것입니다. 당신의 새로운 큰 이미지를 추가 할 때 유효 DPI가 들어 오면 당신은 어떤을 축소해야합니다.

image.ScaleToFit(138, 60); 

당신이이 유일한 솔루션입니다 바코드 생성 코드를 사용하여 부착하는 경우. 그러나 @rufanov가 말했듯이, 방법이 더 좋음 방법은 실제 벡터 기반 바코드와 iText 배송을 실제로 사용하는 것입니다. barcdoe는 ITF 바코드로 표시되므로 iTextSharp.text.pdf.BarcodeInter25 만 사용할 수 있습니다. 아래 코드는 PDF와 함께 제공되는 기본 Helvetica가 키릴 문자를 지원하지 않으므로 "Arial Unicode MS"글꼴을 사용하여 바코드를 그립니다. 그러나이 글꼴을 사용하려는 글꼴로 변경할 수 있습니다. 코드와 일치하도록 사각형을 다시 조정해야하지만 그렇지 않으면 완벽하게 작동하고 확장되어야합니다.

//We need a font that supports Cyrillic glyphs 
var fontFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); 

//Create an iText font that uses this font 
var bf = BaseFont.CreateFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

//Create our barcode 
var B = new iTextSharp.text.pdf.BarcodeInter25(); 

//Set the font 
B.Font = bf; 

//Set the text, you might need to play with the whitespace 
B.Code = "693000 78 00700 4"; 

//Generate an iTextSharp image which is vector-based 
var img = B.CreateImageWithBarcode(writer.DirectContent, BaseColor.BLACK, BaseColor.BLACK); 

//Shrink the image to fit specific bounds 
img.ScaleToFit(100, 100); 

//The barcode above doesn't support drawing text on top but we can easily do this 
//Also, the OP is using a PdfStamper so this easily works with that path, too 

//Create a ColumnText object bound to a canvas. 
//For a PdfStamper this would be something like mPdfStamper.GetOverContent(2) 
var ct = new ColumnText(writer.DirectContent); 

//Set the boundaries of the object 
ct.SetSimpleColumn(100, 400, 300, 600); 

//Add our text using our specified font and size 
ct.AddElement(new Paragraph("ПОЧТА РОССИИ", new iTextSharp.text.Font(bf, 10))); 

//Add our barcode 
ct.AddElement(img); 

//Draw the barcode onto the canvas 
ct.Go();