2013-08-01 1 views
0

계층화 된 PDF 파일을 만들려면 ABCPDF를 사용해야합니다. 난 워터 마크에 대한 예제를 봤지만 PDF를 두 번째 레이어로 만들어야합니다. 내 코드는 아래와 같습니다. 그것을 실행할 때, 나는 단지 하나의 레이어를 볼 수 있습니다. 내가 뭘 잘못하고 있니?ABCPDF 8 - 계층화 된 PDF를 작성하는 방법

감사합니다.

 WebSupergoo.ABCpdf8.Doc artworkDoc = new WebSupergoo.ABCpdf8.Doc(); 
     artworkDoc.SetInfo(0, "License", _License); 

     WebSupergoo.ABCpdf8.Doc cutDoc = new WebSupergoo.ABCpdf8.Doc(); 
     cutDoc.SetInfo(0, "License", _License); 

     // Attempt to read in Artwork File 
     try 
     { 
      artworkDoc.Read(ArtworkPath); 
     } 
     catch (Exception ex) 
     { 
      Exception noartwork = new Exception("Problem with Artwork File: " + ex.ToString()); 
      throw noartwork; 
     } 

     // Attempt to read in cut File 
     try 
     { 
      cutDoc.Read(cutPath); 
     } 
     catch (Exception ex) 
     { 
      Exception nocut = new Exception("Problem with cut File: " + ex.ToString()); 
      throw nocut; 
     } 

     WebSupergoo.ABCpdf8.Doc outputDoc = artworkDoc; 
     outputDoc.SetInfo(0, "License", _License); 

     // Attempt to merge artwork and cut files into output Document 
     try 
     { 
      outputDoc.PageNumber = 1; 
      outputDoc.Layer = outputDoc.LayerCount + 1; 
      outputDoc.AddImageDoc(cutDoc, 1, outputDoc.Rect); 
     } 
     catch (Exception ex) 
     { 
      Exception problem = new Exception("Problem appending cut and artwork files to output: " + ex.ToString()); 
      throw problem; 
     } 

     // Attempt to save the output Document to the specified output path 
     try 
     { 
      outputDoc.Save(OutputPath); 
      artworkDoc.Clear(); 
      cutDoc.Clear(); 
      outputDoc.Clear(); 
     } 
     catch (Exception ex) 
     { 
      Exception invalidOutput = new Exception("Unable to write output file: " + ex.ToString()); 
      throw invalidOutput; 
     } 

     return true; 
    } 
+0

이러한 레이어는 실제로 선택적 콘텐츠 그룹 (OCG)으로 알려져 있습니다. 당신이 ABCpdf에게 그들과 거래하기위한 코드를 요청하면, 당신에게 그것을 보낼 것입니다. – OnceUponATimeInTheWest

답변

1

내가 가진 다른 PDF 계층 문제에 대한 iTextSharp를 사용하여 의지 후에, 나는 레이어는 기존의 PDF 파일의 페이지가 어디에 새로운 레이어 PDF 파일을 만들 수 iTextSharp를 사용하는 방법을 탐구하기로 결정했다. 아래 코드는 내가 생각해 낸 것입니다. 코드는 소스 PDF 파일 각각에 새 PDF에 레이어로 추가 할 페이지가 하나만 있다고 가정합니다. 저를 여기에 게시하는 것이 다른 사람들에게 약간의 시간을 절약하는 데 도움이 될 것이라는 점을 이해하는 데 다소 시간이 걸렸습니다.

 try 
     { 
      iTextSharp.text.pdf.PdfReader artwork = new iTextSharp.text.pdf.PdfReader(@”c:\artwork.pdf”); 
      iTextSharp.text.pdf.PdfReader cut = new iTextSharp.text.pdf.PdfReader(@”c:\cut.pdf”); 

      //get size of the cut PDF so the new layered PDF will be sized the same. 
      iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(cut.GetPageSize(1).Width, cut.GetPageSize(1).Height); 

      //the artwork PDF needs to be sized the same as the cut pdf 
      if (artwork.GetPageSize(1).Width != pageSize.Width || artwork.GetPageSize(1).Height != pageSize.Height) 
      { 
       string resizedFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(textBox1.Text),System.IO.Path.GetFileNameWithoutExtension(textBox1.Text) + "_resized.pdf"); 

       iTextSharp.text.Document resizedArtwork = new iTextSharp.text.Document(pageSize); 
       iTextSharp.text.pdf.PdfWriter resizedWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(resizedArtwork, new System.IO.FileStream(resizedFile, System.IO.FileMode.Create)); 

       iTextSharp.text.pdf.PdfImportedPage importedPage = resizedWriter.GetImportedPage(artwork, 1); 

       resizedArtwork.Open(); 
       iTextSharp.text.pdf.PdfContentByte resizedContentByte = resizedWriter.DirectContent; 
       resizedContentByte.AddTemplate(importedPage, 0, 0); 
       resizedArtwork.Close(); 

       //close reader associated with non-resized document 
       artwork.Close(); 

       //set reader to resized document 
       artwork = new iTextSharp.text.pdf.PdfReader(resizedFile); 



      } 

      //create a new PDF 
      string outputFileName = @”c:\layered.pdf”; 
      iTextSharp.text.Document newPDF = new iTextSharp.text.Document(pageSize); 


      //create writer to output new PDF's contents to 
      iTextSharp.text.pdf.PdfWriter pdfWriter = iTextSharp.text.pdf.PdfWriter.GetInstance(newPDF, new System.IO.FileStream(outputFileName,System.IO.FileMode.Create)); 

      //grab the first page from the artwork and cut PDF. These will become new layers in the new PDF 
      iTextSharp.text.pdf.PdfImportedPage artworkImportedPage = pdfWriter.GetImportedPage(artwork, 1); 
      iTextSharp.text.pdf.PdfImportedPage cutImportedPage = pdfWriter.GetImportedPage(cut, 1); 

      newPDF.Open(); 

      iTextSharp.text.pdf.PdfContentByte pdfContentByte = pdfWriter.DirectContent; 

      pdfContentByte.BeginLayer(new iTextSharp.text.pdf.PdfLayer("artwork",pdfWriter)); 
      pdfContentByte.AddTemplate(artworkImportedPage, 0, 0); 
      pdfContentByte.EndLayer(); 


      pdfContentByte.BeginLayer(new iTextSharp.text.pdf.PdfLayer("cut", pdfWriter)); 
      pdfContentByte.AddTemplate(cutImportedPage, 0, 0); 
      pdfContentByte.EndLayer(); 

      newPDF.Close(); 
      pdfWriter.Close(); 
      artwork.Close(); 
      cut.Close(); 

     } 
     catch (Exception ex) 
     { 

     }