【问题标题】:how to embed an image in pdf?如何在pdf中嵌入图像?
【发布时间】:2012-02-08 07:29:11
【问题描述】:

我正在从电子邮件的内容中生成一个 pdf 文件,并且该电子邮件包含一个图像。但我想在该pdf中添加静态图像。在pdf中添加静态图像的方法是什么?

【问题讨论】:

    标签: iphone image pdf-generation xcode4.2


    【解决方案1】:

    这取决于您如何创建 PDF。通常(在绘制到 CGPDFContext 时)您使用普通的 Quartz 绘制函数来添加图像。

    【讨论】:

    • 感谢重播。我使用了与 Hardik 建议的相同的代码。但我收到错误 - “ : CGContextDrawImage: invalid context ”。请提供解决方案。谢谢。
    • 您应该首先提供如何从邮件中创建 PDF 的代码。
    【解决方案2】:

    你可以这样做:

    -(void) CreatePdf
    {
       NSInteger currentY = HEIGHT; 
    
      NSString *logoFileName = @"logo.jpg";
    
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
      NSString *saveDirectory = [paths objectAtIndex:0];
    
      NSString *logoFilePath = [saveDirectory stringByAppendingPathComponent:logoFileName];
    
      currentY = [self getAbsoluteY:currentY :70];
    
      UIImage *logoImage = [UIImage imageWithContentsOfFile:logoFilePath];
    
      CGContextDrawImage(pdfContext, CGRectMake(paddingLeft, currentY, 100, 70), [logoImage CGImage]);
    
    }
    
    -(NSInteger)getAbsoluteY:(NSInteger)currY: (NSInteger)space 
    {
       return (currY - space);
    }
    

    【讨论】:

    • 感谢重播。我用过你的代码。但我收到错误“: CGContextDrawImage: invalid context 0x34b1c8d”
    • pdfContext 的值应该是多少?因为我在上下文中遇到错误,即无效的上下文。
    【解决方案3】:

    我用 PS 和 Word 解决了一个类似的问题。这个简单的脚本会打开 Word 并将图像插入到新文档中。然后您可以手动将文档另存为 PDF 或其他格式。这也可以自动化,但我更喜欢让 Word 保持打开状态以检查它并在保存之前进行少量编辑。

    这个脚本对于摆脱旧杂志很有用。只需将要保留的页面扫描到单个文件夹中的图像文件,运行脚本,然后将文档保存为 Kindle 的 PDF。

    $letterWidth = 612
    $letterHeight = 792
    $topMargin = 0
    $bottomMargin = 0
    $leftMargin = 0
    $rightMargin = 0
    
    function Main([string] $dir)
    {
      $files = dir $dir
      $doc, $selection = OpenWordDoc
    
      foreach ($file in $files)
      {
        $par = $doc.Paragraphs.Add()
        $par.SpaceAfter = 0
        $par.Alignment = 1
        $pic = $par.Range.InlineShapes.AddPicture($file.FullName)
        ScaleImage $pic
      }
    }
    
    function ScaleImage($pic)
    {
      $hScale = ($letterWidth - $leftMargin - $rightMargin) / $pic.Width
      $vScale = ($letterHeight - $topMargin - $bottomMargin) / $pic.Height
      $scale = [Math]::Min($hScale, $vScale) * 100
      $pic.ScaleHeight = $pic.ScaleWidth = $scale
    }
    
    function OpenWordDoc()
    {
      $word = new-object -ComObject "word.application"
      $word.Visible = $True
      $doc = $word.documents.Add()
      $doc.PageSetup.TopMargin = $topMargin
      $doc.PageSetup.BottomMargin = $bottomMargin
      $doc.PageSetup.LeftMargin = $leftMargin
      $doc.PageSetup.RightMargin = $rightMargin
      $doc, $word.Selection
    }
    
    . Main $args[0]
    

    【讨论】:

      【解决方案4】:

      【讨论】:

      • 我认为 OP 正在讨论以编程方式生成 PDF,而不是通过 GUI 界面。
      猜你喜欢
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多