ABCPDF 8 - 如何创建分层PDF

时间:2013-08-01 19:43:12

标签: abcpdf

我需要使用ABCPDF来创建分层PDF文件。我见过水印的例子,但我需要将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;
    }

1 个答案:

答案 0 :(得分:1)

在使用iTextSharp处理我遇到的其他一些PDF图层问题之后,我决定探索如何使用iTextSharp创建一个新的分层PDF文件,其中图层是现有PDF文件中的页面。下面的代码是我提出的。该代码假设每个源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)
        {

        }