2012-05-02 3 views
1

다음 코드는 스트림으로 pdf를 생성합니다. 이것은 잘 작동하지만 지금 다음 요구 사항이 있습니다.페이지 번호 추가 및 itext로 스트림에서 가로 A4 만들기

1) make page landscape : 다른 예제를 보면 문서 객체에 속성을 추가합니다. 하지만이 인스 트림을하고 있어요. 그러면이 속성을 어떻게 추가할까요?

2) 페이지 번호를 추가하십시오. 페이지 당 행 수가 x가되도록 그리드에 항목을 넣어야합니다. 페이지 바닥 글에 페이지 번호가 있습니다. Itext sharp로 어떻게 이런 종류의 기능을 습득 할 수 있습니까?

public static void Create(ICollection<Part> parts, string path) 
{ 
    PdfReader reader = new PdfReader(path); 

    var pageWidth = 500; 
    byte[] bytes; 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (PdfStamper stamper = new PdfStamper(reader, ms)) 
     { 
      PdfContentByte cb = stamper.GetOverContent(1); 

        //Flush the PdfStamper's buffer 
      stamper.Close(); 
      //Get the raw bytes of the PDF 
      bytes = ms.ToArray(); 
      var now = String.Format("{0:d-M-yyyy}", DateTime.Now); 
      var pdfName = string.Format("{0}_factory_worksheet", now).Replace("%", "").Replace(" ", "_"); 

      var context = HttpContext.Current; 
      context.Response.ContentType = "application/pdf"; 
      context.Response.AddHeader("content-disposition", "attachment;filename=" + pdfName); 
      context.Response.Buffer = true; 
      context.Response.Clear(); 
      context.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length); 
      context.Response.OutputStream.Flush(); 
      context.Response.End(); 
     } 
    } 

} 
+0

처음부터 PDF를 생성하지 않으므로 실제로 기존 부품에서 부품을 가져오고 있습니까? –

답변

1

난 정말 당신이 그것을 처리하는 방법을 모르는 C#하지만 논리적 흐름은 다음과 같이 될 것입니다 :
사용 PdfDictionary, 당신의 PDF 파일은 여러 페이지를 포함 degree.Assuming 90 독자 콘텐츠를 회전 PdfReader reader = 새로운 PdfReader (경로); (; pgCnt < = reader.getNumberOfPages(); INT pgCnt = 1 pgCnt ++)에 대한
{
// 논리 페이지 번호
추가 회전 &을 구현하기}

당신을 가정 (현재 회전을 얻을하려면 세로 모드를 사용하는 & 가로 모드로 변환) 사용
int rttnPg = reader.getPageRotation (pgCnt);
도 해당 페이지의 PdfDictionary를 가져옵니다.
pgDctnry = reader.getPageN (i);
은 지금 90도 사용
pgDctnry.put (PdfName.ROTATE 새로운 PdfNumber (rttnPg + 90))을 회전 (I는 pgDctnry로서 그 변수의 이름); 현재 it.Now을하고 있습니다로
지금 현재 페이지
pgCntntBt = 스탬퍼 .getOverContent (pgCnt)의 (내가 pgCntntBt로 이름을 여기) 페이지 번호 콘텐츠 극복 추가하는 것이 PdfStamper를 사용하여 바인딩;
rctPgSz = rdrPgr.getPageSizeWithRotation (pgCnt);
pgCntntBt.beginText();
bfUsed = // 표시 할 텍스트에 사용되는 기본 글꼴. 글꼴 크기 설정
pgCntntBt.setFontAndSize (bfUsed, 8.2f); txtPg = String.format (pgTxt + "% d/% d", pgCnt, totPgCnt);
pgCntntBt.showTextAligned (2, txtPg, // 너비 넣기, // 높이 넣기, // 회전);
pgCntntBt.endText();

실제로 당신이 의미하는 것을 이해하지 못합니다 : "페이지 당 행 수가 x가되도록 그리드에 항목을 넣어야합니다. 페이지 바닥 글에 페이지 번호가 표시됩니다". 이제 stamper를 닫아 outputstream에서 지 웁니다.

관련 문제