티스토리 뷰

728x90
반응형

try
{
PrintDialog printDialog = new PrintDialog(); // 프린트 다이얼로그 생성

printDialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape; 
printDialog.PageRangeSelection = PageRangeSelection.AllPages;
printDialog.SelectedPagesEnabled = true;
printDialog.UserPageRangeEnabled = true;


string return_value = string.Empty;
if (Print_Queue == true)
{
Print_Queue = false;
printDialog.MaxPage = 7; // 최대페이지.

Nullable print;
print = printDialog.ShowDialog();


string panel_name = string.Empty;

if (print == true)
{
int TS, ES;
if ((bool)print_chk.IsChecked) // Checkbox 프린트, PDF인쇄중 프린트 체크박스 선택
{
TS = printDialog.PageRange.PageFrom;
ES = printDialog.PageRange.PageTo;
}
else
{
TS = 1;
ES = 7;
}

 

if (ES == 0) // 모두 인쇄 하면 마지막장이 0으로... 그래서 최대 페이지로
{
ES = 7;
}

StackPanel SP = new StackPanel();

CheckBox CBX = new CheckBox();
int print_count = 0;
int page_count = 0;

 

// pdfSharp 을 누겟에서 설치하고 사용. 버전 v1.50.4147 ( PDFsharp 작성자: empira software GmbH)
//using PdfSharp.Drawing;
//using PdfSharp.Pdf;

PdfDocument document = new PdfDocument(); // PDF Document 생성
PdfPage page = new PdfPage(); // PDF page 생성

Image _img = new Image();
for (int TST = TS-1; TST < ES; TST++)
{

var CBX_Name = print_stack_grid.Children.OfType<CheckBox>().Where(txb => txb.Name == "print" + TST).FirstOrDefault();
// print_stack_grid에서 print1~7 번까지 체크박스를 찾고

CBX = (CheckBox)CBX_Name; // CBX 에 할당.

print_count += 1;
SP = (StackPanel)MainBody_Pan.FindName("Panel" + TST.ToString()); // 메인패널에서 Panel1~7까지 찾아.

System.Windows.Rect bounds = VisualTreeHelper.GetDescendantBounds(SP);
double dpi = 96;

RenderTargetBitmap rtb = new RenderTargetBitmap((int)bounds.Width, (int)bounds.Height, dpi, dpi, System.Windows.Media.PixelFormats.Default);
DrawingVisual dv = new DrawingVisual();
using (DrawingContext dc = dv.RenderOpen())
{
VisualBrush vb = new VisualBrush(SP); 
dc.DrawRectangle(vb, null, new System.Windows.Rect(new System.Windows.Point(), bounds.Size));
}
rtb.Render(dv);


if ((bool)print_chk.IsChecked)
{
printDialog.PrintVisual(dv, "Page" + TST.ToString()); //프린트한다.
}

// PDF제작
byte[] data;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
BitmapEncoder endocder2 = new PngBitmapEncoder();
endocder2.Frames.Add(BitmapFrame.Create(rtb));
endocder2.Save(ms);
data = ms.ToArray();

BitmapImage _image = new BitmapImage();

_image.BeginInit();
_image.StreamSource = ms;
_image.EndInit();
using (XImage img = XImage.FromStream(ms))
{
page = document.AddPage();
page.Orientation = PdfSharp.PageOrientation.Landscape;

XGraphics gfx = XGraphics.FromPdfPage(page);

gfx.DrawImage(img, 0, 0, SP.Width*0.75, SP.Height*0.75);
}
}

// PDF Document 생성완료


} // for loop end

return_value = PDF_FTP(document, Member_Names, "Report"); // 제작된 PDF를 FTP로 전송


MessageBox.Show("출력되었습니다.");
}

}
return return_value;


}

catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}

 

 

// FTP전송

private string PDF_FTP(PdfDocument png, string Member_Names, string panel_name)
{
try
{
byte[] data;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
png.Save(ms);
data = ms.ToArray();

}


string serverAddress = "ftp://";
string userId = "id";
string userPass = "pass";
string dir = "/저장경로";
string Times = DateTime.Now.ToString().Replace(" ", "").Replace("-", "").Replace(":", "").Replace("/", ""); // file_addname + //DateTime.Now.ToString();


string uploadPath = serverAddress + dir + Member_Names + Times + "_" + panel_name + ".pdf";

 

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uploadPath);
// FTP 업로드한다는 것을 표시
request.Method = WebRequestMethods.Ftp.UploadFile;
// 쓰기 권한이 있는 FTP 사용자 로그인 지정
request.Credentials = new NetworkCredential(userId, userPass);

 

// RequestStream에 데이타를 쓴다
request.ContentLength = data.Length;


using (System.IO.Stream reqStream = request.GetRequestStream())
{
reqStream.Write(data, 0, data.Length);
}

// FTP Upload 실행
using (FtpWebResponse resp = (FtpWebResponse)request.GetResponse())
{
 return Member_Names + Times + "_" + panel_name + ".pdf";
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}


}

반응형
댓글