0

我需要使用 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;
    }
4

1 回答 1

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)
        {

        }
于 2013-08-16T15:53:01.220 回答