2017-12-06 1 views
0

winform에서 인쇄하려고하는데 문서를 인쇄 할 때 빈 시트가 나타납니다. ? C#에 저장된 파일 인쇄

private PrintDocument printDocument1 = new PrintDocument(); 
    private string stringToPrint; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 


    private void button1_Click(object sender, EventArgs e) 
    { 
     ReadPrint(); 
     printDocument1.Print(); 
    } 

    private void ReadPrint() 
    { 
     string docName = "ejemplo.pdf"; 
     string docPath = @"C:\dir1\"; 
     printDocument1.DocumentName = docName; 
     using (FileStream stream = new FileStream(docPath + docName, FileMode.Open, FileAccess.Read)) 
     using (StreamReader reader = new StreamReader(stream)) 
     { 
      stringToPrint = reader.ReadToEnd(); 
     } 
    } 

    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     int charactersOnPage = 0; 
     int linesPerPage = 0; 


     e.Graphics.MeasureString(stringToPrint, this.Font, 
      e.MarginBounds.Size, StringFormat.GenericTypographic, 
      out charactersOnPage, out linesPerPage); 


     e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, 
      e.MarginBounds, StringFormat.GenericTypographic); 


     stringToPrint = stringToPrint.Substring(charactersOnPage); 

     e.HasMorePages = (stringToPrint.Length > 0); 
    } 

    private void printButton_Click(object sender, EventArgs e) 
    { 
     LeerArchivo(); 
     printDocument1.Print(); 
    } 

내가 그것을하거나 파일 또는 몇 가지 예제 코드를 인쇄하는 다른 방법을 수정하는 방법이 있는지 알고 싶습니다

: 나는 인쇄하려고 할있는 이 코드인가? 관련 stringToPrint에서

: enter image description here

+1

당신은 문자열로 (이진입니다) PDF 데이터를로드하기 위해 노력하고 있습니다. 그런 식으로 작동하지 않습니다. –

+0

@JoelCoehoorn, 예, PDF 파일, 그렇다면이 경우 어떻게됩니까? – Danilo

답변

0

당신이 stringToPrint가 비어 있거나 null이 아닌지 확인이 있습니까? 나는 똑같은 것을 사용하고 있으며 완벽하게 작동합니다. 인쇄 할 문서가 비어 있는지 확인하려는 경우 인쇄용 PrintPreviewDialog를 추가해야합니다. 먼저 변수를 확인하십시오. 텍스트는 x 및 y 좌표로 표시되는 위치 매개 변수 새로운 포인트에 대한

e.Graphics.DrawString("SomeString", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(580, 510)); 

e.Graphics.DrawString("SomeString1", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(700, 510)); 

()이 그 것이다.

+0

질문을 편집하십시오, stringToPrint에는 사진에 따라 자료가, google에서 보는 부호를 베끼십시오. 경로에 액세스 할 수있는 권한이 없다는 오류가있어서 FileAccess.Read가 추가 된 차이점 – Danilo

+0

편집을 참조하십시오. 도움이 될 수도 있습니다. –

1

Vb.net에는 PrintForm 메서드가 있지만 C#에는 Windows Form을 인쇄하기위한 inbuilt 메서드가 없습니다.

런타임시 C# .net에서 Windows 양식을 인쇄하려면 다음을 수행하십시오. 기본 개념은 런타임 중에 jpeg 형식으로 Form의 화면 이미지를 캡쳐하고 Print 버튼을 클릭하는 것과 같은 이벤트에 동일한 내용을 인쇄하는 것을 포함합니다.

print