我的PDF语法坏了!如何将图像放入PDF?

时间:2014-08-03 12:32:46

标签: c# pdf windows-phone-8

我目前正在尝试使用c#在没有Windows手机外部库帮助的情况下自行编码PDF。我目前有这个代码,它会发出一个我可以使用PowerTools检索的PDF文件。文本有效,但每当我尝试放入图像时它都会完全失败。我可以检索文件但其中没有图像。我有点不知所措,但我的问题是:这段代码出了什么问题?如何正确地将图像文件流式传输到PDF文件?

编辑:将我的代码更新为最新版本;正确指向pdf对象。

var stream = await System.IO.WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(pdfFile);
StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.UTF8);
List<long> xrefs = new List<long>();

        //Writing the actual PDF
        writer.WriteLine("%");
        writer.WriteLine("%PDF-1.5");
        writer.WriteLine("%");

        //1
        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);
        writer.WriteLine("");
        writer.WriteLine("%The PDF catalog");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("<<");
        writer.WriteLine("  /Type /Catalog");
        writer.WriteLine("  /Pages 2 0 R");
        writer.WriteLine(">>");
        writer.WriteLine("endobj");

        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);

        //2
        //Declare the page list
        writer.WriteLine("");
        writer.WriteLine("%The page list");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("<<");
        writer.WriteLine("  /Type /Pages");
        writer.WriteLine("  /Kids [3 0 R]");
        //writer.WriteLine("  /Resources 3 0 R");
        writer.WriteLine("/Count 1");
        writer.WriteLine(">>");
        writer.WriteLine("endobj");

        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);


        //3
        //Declare the page object
        writer.WriteLine("");
        writer.WriteLine("%Actual page, with references to all objects it uses (image and resources and whatnot)");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("<<");
        writer.WriteLine("  /Type /Page");
        writer.WriteLine("  /Parent 2 0 R");
        writer.WriteLine("  /Resources 4 0 R");
        writer.WriteLine("  /MediaBox [0 0 " + currentImage.PixelWidth + " " + currentImage.PixelHeight + "]");
        //612 792
        writer.WriteLine("  /Contents 6 0 R");
        writer.WriteLine(">>");
        writer.WriteLine("endobj");

        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);

        //4
        //Declare the stuff this PDF has (Or something?)
        writer.WriteLine("");
        writer.WriteLine("%Resources this pdf uses");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("<<");
        writer.WriteLine("/ProcSet [/PDF /ImageC]");
        writer.WriteLine("/XObject << Im1 5 0 R >>");
        writer.WriteLine(">>");
        writer.WriteLine("endobj");

        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);



        //Writing the actual image stream
        writer.WriteLine("stream");

        byte[] jpegData = new byte[8192];
        string streamString = "";
        using (MemoryStream imageStream = new MemoryStream())
        {
            WriteableBitmap wBitmap = new WriteableBitmap(currentImage);

            wBitmap.SaveJpeg(imageStream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
            imageStream.Seek(0, SeekOrigin.Begin);
            jpegData = imageStream.ToArray();

            foreach (byte b in jpegData)
            {
                streamString += b + " ";
            }
        }



        //5
        writer.WriteLine("");
        writer.WriteLine("%image declaration");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("<<");
        writer.WriteLine("  /Type /XObject"); //Specify the XOBject
        writer.WriteLine("  /Subtype /Image"); //It is an image
        writer.WriteLine("  /Width " + currentImage.PixelWidth); //The dimensions of the image
        writer.WriteLine("  /Height " + currentImage.PixelHeight);
        writer.WriteLine("   /ColorSpace /DeviceRGB");
        writer.WriteLine("  /BitsPerComponent 8");
        writer.WriteLine("  /Length " + streamString.Length);
        writer.WriteLine("  /Filter /DCTDecode");
        writer.WriteLine(">>");

        writer.Flush();
        stream.Flush();

        writer.Write(streamString);

        writer.Flush();

        writer.WriteLine("");
        writer.WriteLine("endstream");
        writer.WriteLine("endobj");

        writer.Flush();
        stream.Flush();
        xrefs.Add(stream.Position);

        //6
        writer.WriteLine("");
        writer.WriteLine("%the placing of the image");
        writer.WriteLine(xrefs.Count + " 0 obj");
        writer.WriteLine("  <<");
        writer.WriteLine("      /Length 8192");
        writer.WriteLine("  >>");
        writer.WriteLine("          stream");
        writer.WriteLine("          q");
        writer.WriteLine("              " + 256 + " 0 0 " + 256 + " 0 0 cm");
        writer.WriteLine("              /Im1 Do");
        writer.WriteLine("          Q");
        writer.WriteLine("          endstream");
        writer.WriteLine("endobj");
        //writer.WriteLine(">>");

        writer.Flush();
        stream.Flush();
        //xrefs.Add(stream.Position);

        //Closing
        //PDF-XREFS. This part of the PDF is an index table into every object #1..#7 that we defined.

        long xref_pos = stream.Position;
        writer.WriteLine("xref");
        writer.WriteLine("1 " + (xrefs.Count));

        foreach (long xref in xrefs)
        {
            writer.WriteLine("{0:0000000000} {1:00000} n", xref, 0);
        }

        // PDF-TRAILER. Every PDF ends with this trailer.
        writer.WriteLine("trailer");
        writer.WriteLine("<<");
        writer.WriteLine("  /Size " + xrefs.Count);
        writer.WriteLine("  /Root 1 0 R");
        writer.WriteLine(">>");
        writer.WriteLine("startxref");
        writer.WriteLine(xref_pos);
        writer.WriteLine("%%EOF");

        writer.Dispose();

1 个答案:

答案 0 :(得分:1)

希望这可以帮到你......

using System;  
using System.Drawing;  
using Spire.Pdf;  
using Spire.Pdf.Graphics;  

namespace DrawImage  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            //Create PDF and Add Page  
            PdfDocument doc = new PdfDocument();  
            PdfPageBase page = doc.Pages.Add();  

            //Draw Image  
            PdfImage image = PdfImage.FromFile(@"D:\Images\Spring's Coming.png");  
            float width = image.Width * 0.75f;  
            float height = image.Height * 0.75f;  
            float x = (page.Canvas.ClientSize.Width - width) / 2;  

            page.Canvas.DrawImage(image, x, 60, width, height);  

            //Save and Launch   
            doc.SaveToFile("InsertImage.pdf");  
            doc.Close();  
            System.Diagnostics.Process.Start("InsertImage.pdf");  
        }  
    }  
}